Carefully analyze the following Python program. The objective of the program…

2026

Carefully analyze the following Python program. The objective of the program is to read the complete content of the text file bengal.txt, replace every occurrence of the word “Calcutta” with “Kolkata” and then write the modified content back into the same file. However, certain parts of the program are missing and have been marked as Task A, Task B, Task C and Task D. Complete the missing statements so that the program works correctly without any logical or syntactical errors.

try:
    __________________________ as File:   # Task A

    Text = __________________________     # Task B

    NewText = Text.replace("Calcutta", "Kolkata")

    __________________________ as File:   # Task C

    __________________________             # Task D

except:
    print('File I/O Error!')

(A) Write the missing statement for Task A to open the file in read mode.

(B) Write the missing statement for Task B to read the complete content of the file and store it in the identifier Text.

(C) Write the missing statement for Task C to reopen the file in write mode so that the updated content can be stored.

(D) Write the missing statement for Task D to write the entire modified content stored in NewText into the file.

Attempted by 72 students.

Show answer & explanation

As per the given program structure, the missing statements should be filled carefully without repeating the already provided part (as File:).

(A) Task A (Open file in read mode):

with open("bengal.txt", "r")

(B) Task B (Read complete content):

File.read()

(C) Task C (Open file in write mode):

with open("bengal.txt", "w")

(D) Task D (Write modified content):

File.write(NewText)

Explanation:

  • File.read() reads the entire file content.

  • replace() modifies the text.

  • The file is reopened in write mode ("w") to overwrite the content.

  • File.write(NewText) writes the updated data back to the file.

Final Answers:

  • Task A: with open("bengal.txt", "r")

  • Task B: File.read()

  • Task C: with open("bengal.txt", "w")

  • Task D: File.write(NewText)

Explore the full course: Hpsc Pgt Computer Science