Courseiva

PCEP Computer Programming and Python Fundamentals Practice Question

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

⚠ Common exam trap

The PCEP exam often tests the misconception that reading a file in chunks with a while loop is the best approach, when in fact the idiomatic for loop over the file object is simpler, safer, and the recommended pattern in Python for line-by-line processing.

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

✓

Process the file line by line using a for loop

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.

Answer analysis

Option-by-option breakdown

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

  • ✗

    Increase the memory allocation

    Why it's wrong here

    Raising the allocation does not change the script's peak resident set, so a file larger than available RAM still exhausts memory; the fix is streaming or chunked reading. It is tempting because memory limits genuinely cause failures elsewhere, and adding RAM is the correct remedy when a workload's working set legitimately exceeds the host's capacity.

  • ✗

    Use a while loop to read chunks

    Why it's wrong here

    A while loop reading chunks can work, but without a defined chunk size and explicit termination it risks unbounded reads or an infinite loop; the idiomatic fix is iterating the file object line by line. It is tempting because chunked reading does bound memory, and would be correct when fixed-size binary blocks must be processed.

  • ✗

    Read the entire file into memory and split

    Why it's wrong here

    Reading the whole file into memory and splitting reproduces the original out-of-memory failure, since memory use still scales with file size. It is tempting because splitting feels like chunking, and would be correct for small files where random access to all records is genuinely required.

  • ✓

    Process the file line by line using a for loop

    Why this is correct

    Iterating with a for loop reads one line at a time, so only a single line resides in memory rather than the whole file. This directly resolves the out-of-memory constraint by keeping peak memory proportional to line length.

About these practice questions

This PCEP question is part of Courseiva's 482-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.