Courseiva
Computer Programming and Python FundamentalshardMultiple ChoiceObjective-mapped

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

    This is a temporary workaround and does not address the root issue; the script remains inefficient.

  • Use a while loop to read chunks

    Why it's wrong here

    While possible, a for loop over the file object is more Pythonic and equally efficient.

  • Read the entire file into memory and split

    Why it's wrong here

    This would worsen memory usage, causing the same or worse problem.

  • Process the file line by line using a for loop

    Why this is correct

    This reads each line as needed, keeping memory usage low.

About these practice questions

This PCEP question is part of Courseiva's 498-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.