Courseiva
Question 350 of 498
Functions, Tuples, Dictionaries and ExceptionsmediumMultiple ChoiceObjective-mapped

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

A team is building a configuration parser that reads a file containing key=value pairs. They use a dictionary to store the configuration. The parser function `load_config(filename)` opens the file, reads line by line, splits on '=', and populates a dictionary. Some lines have comments starting with '#'. The developer wants to ensure that the dictionary is not polluted with comment lines. They write: `if line.startswith('#'): continue`. However, after parsing, the dictionary contains an entry with key '#' because some lines have no '=' sign. For example, a line like `#comment` is being added as a key with value None. The developer wants to fix this. Which modification should be made?

⚠ Common exam trap

Python Institute often tests the misconception that checking for a comment marker alone is sufficient, when the real issue is that any line without an '=' sign (including comments) will be incorrectly parsed as a key with no value.

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

Check `if '=' not in line: continue` before splitting.

The core issue is that lines without an '=' sign (like `#comment`) are still processed by the split, causing the entire line to become a key with no value. By explicitly checking `if '=' not in line: continue` before splitting, the developer ensures that only lines containing a key-value separator are added to the dictionary, effectively filtering out comment lines and any other malformed lines.

Answer analysis

Option-by-option breakdown

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

  • Use `key, value = line.split('=', 1)` and catch ValueError if less than two parts.

    Why it's wrong here

    Unpacking would raise ValueError if no '=', but catching it would skip the line. Actually this could work, but not the best because it relies on exception handling for control flow.

  • Strip the line of whitespace before checking for comments.

    Why it's wrong here

    Stripping does not prevent lines without '='.

  • Check `if '=' not in line: continue` before splitting.

    Why this is correct

    Explicitly skip lines without '='.

  • Replace `startswith('#')` with `line.lstrip().startswith('#')` and also skip empty lines.

    Why it's wrong here

    Does not handle lines without '='.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Last reviewed: Jun 30, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.