PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Easy

Question 5

Q174 - I/O


Assuming that the open() invocation has gone successfully, the following snippet will:



  • A

    read the file line by line.

  • B

    read the file character by character.

  • C

    read the whole file at once.

  • D

    cause an exception.

Reveal correct answer

Correct answer: A

Explanation

Topic: open()


Try it yourself:

Explanation:

The for loop will iterate through the file line by line.
The mode 'rt' stands for read & text.


Q174 (Please refer to this number, if you want to write me about this question.)

A. The code snippet iterates over the file object returned by the open() function using a for loop, which reads the file line by line. Each iteration of the loop will print a single line from the file.

B. The code snippet does not read the file character by character. Instead, it reads the file line by line due to the iteration over the file object. Reading the file character by character would require a different approach, such as using the read() method with a specified number of characters to read.

C. The code snippet does not read the entire file at once. Instead, it reads the file line by line due to the iteration over the file object. This approach is memory-efficient, especially for large files, as it does not load the entire file into memory.

D. The code snippet does not contain any operations that would cause an exception. As long as the file exists and can be successfully opened, the code will execute without any errors.

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need