Question:

Consider the following code and specify the correct order of the statements to be written:
Code:
(A) f.write(”CUET EXAMINATION”)
(B) f=open(”CUET.TXT”, ”w”)
(C) print(”Data is Written Successfully”)
(D) f.close()
Choose the correct answer from the options given below:

Updated On: May 28, 2025
  • (A), (B), (C), (D)
  • (B), (A), (C), (D)
  • (B), (D), (C), (A)
  • (B), (D), (A), (C)
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Approach Solution - 1

To determine the correct order of statements for file operations in Python, it is essential to follow the logical sequence of opening a file, performing the required operations (like writing), providing feedback if necessary, and finally closing the file. Here's a breakdown:

  1. Open the File: Use the open() function to create or open a file for writing. This step must come first to set up the file handler (f) that is used in subsequent operations.
  2. Write to the File: The write() method is used after the file has been opened to write data into the file.
  3. Print Status: Once the writing operation is complete, a status message indicating success can be printed. This informs the user that the operation was successful.
  4. Close the File: The close() method is called to close the file securely. This ensures all resources are freed and data is properly saved.

Applying this logical order to the given options, we find:

  • (B) f=open(”CUET.TXT”, ”w”): Step 1, opening the file for writing.
  • (A) f.write(”CUET EXAMINATION”): Step 2, writing the given string to the file.
  • (C) print(”Data is Written Successfully”): Step 3, printing a success message.
  • (D) f.close(): Step 4, closing the file.

Therefore, the correct order of the statements is:

(B), (A), (C), (D)
Was this answer helpful?
0
0
Hide Solution
collegedunia
Verified By Collegedunia

Approach Solution -2

The correct order of the statements to write data to a file is (B), (A), (C), (D).

Additional Context:

  • File Handling Sequence:
    1. (B) Open file: Create/overwrite "CUET.TXT" in write mode
    2. (A) Write data: Insert the string "CUET EXAMINATION"
    3. (C) Print confirmation: User feedback message
    4. (D) Close file: Release system resources

Correct Answer: (2) (B), (A), (C), (D).

Was this answer helpful?
0
0