Courseiva

PCEP Computer Programming and Python Fundamentals Practice Question

A programmer needs to read a file line by line and process each line. Which of the following is the most memory-efficient and Pythonic approach?

⚠ Common exam trap

The PCEP exam often tests the distinction between using a `with` statement for guaranteed file closure versus relying on implicit garbage collection, and the misconception that reading the entire file at once is acceptable for small files, ignoring the principle of memory efficiency.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

with open('file.txt') as f: for line in f: print(line)

It uses a `with` statement to ensure the file is properly closed after the block, and iterating directly over the file object reads one line at a time without loading the entire file into memory. This is both memory-efficient and Pythonic, as it leverages the file object's built-in iterator.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✓

    with open('file.txt') as f: for line in f: print(line)

    Why this is correct

    Iterating directly over the file object yields one line at a time, so only a single line is held in memory. The with statement also guarantees the file is closed, satisfying the memory-efficiency and Pythonic requirements simultaneously.

  • ✗

    lines = open('file.txt').read().split('\n')

    Why it's wrong here

    read() loads the entire file into one string, then split('\n') builds a full list, so memory scales with file size. It is tempting because splitting on '\n' appears equivalent to line iteration, and it suits small files needing indexed access.

  • ✗

    content = open('file.txt').read().splitlines()

    Why it's wrong here

    Reading the whole file into memory before splitting defeats line-by-line processing; memory use scales with file size. It is tempting because splitlines() returns a tidy list of lines, which suits small files where random access to every line is needed.

  • ✗

    for line in open('file.txt'): print(line)

    Why it's wrong here

    Iterating the file object streams lines lazily, but the file is never closed, leaking a handle until garbage collection. It is tempting because the syntax is concise and works in scripts, yet the correct approach uses a with statement to guarantee closure.

About these practice questions

Courseiva writes every PCEP question from scratch — 482 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more way this is tested on PCEP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. A Python script processes a large file and runs out of memory. Which solution is most appropriate?

hard
  • A.Increase the memory allocation
  • B.Use a while loop to read chunks
  • C.Read the entire file into memory and split
  • ✓ D.Process the file line by line using a for loop

Why D: Reading a file line by line with a for loop in Python processes one line at a time, keeping only the current line in memory. This avoids loading the entire file into RAM, which is the root cause of the memory exhaustion when dealing with large files.

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.