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
Uses context manager and iterates lazily.
- ✗
lines = open('file.txt').read().split('\n')
Why it's wrong here
Loads entire file and splits, memory heavy.
- ✗
content = open('file.txt').read().splitlines()
Why it's wrong here
Loads whole file into memory and splits into list.
- ✗
for line in open('file.txt'): print(line)
Why it's wrong here
Works but does not close file; not fully Pythonic.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCEP question from scratch — 498 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 →
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.