Courseiva
Control Flow, Loops, Lists and LogicmediumMultiple ChoiceObjective-mapped

PCEP Control Flow, Loops, Lists and Logic Practice Question

A company stores employee data as a list of dictionaries. Each dictionary has keys 'name' and 'age'. Which code correctly counts employees older than 30?

⚠ Common exam trap

Python Institute often tests the distinction between creating a filtered list and counting elements, so the trap here is that option D looks correct but produces a list instead of a numeric count, which is a subtle but critical difference.

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

count = 0 for emp in employees: if emp['age'] > 30: count += 1

It uses a simple `for` loop to iterate directly over each dictionary in the `employees` list, checks if the value of the `'age'` key is greater than 30, and increments the counter accordingly. This is the most Pythonic and readable approach for counting elements that satisfy a condition.

Answer analysis

Option-by-option breakdown

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

  • count = 0 for i in range(len(employees)): if employees[i]['age'] <= 30: count += 1

    Why it's wrong here

    Condition uses <= 30, counting employees 30 or younger.

  • count = 0 i = 0 while i < len(employees): if employees[i]['age'] > 30: count += 1

    Why it's wrong here

    Missing i increment causes infinite loop.

  • count = 0 for emp in employees: if emp['age'] > 30: count += 1

    Why this is correct

    Correctly increments count for each employee over 30.

  • count = [emp for emp in employees if emp['age'] > 30]

    Why it's wrong here

    Returns a list, not the count.

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.