Courseiva

PCEP · topic practice

Control Flow, Loops, Lists and Logic practice questions

This PCEP domain covers Python's control flow and core data structures: if/elif/else, while and for loops, break and continue, lists, and basic boolean logic. Questions present short code snippets or scenario tasks and ask you to predict output, choose the correct loop, or select a working list comprehension.

Courseiva uses original exam-style practice questions designed for learning and revision. The goal is to understand the concepts, recognise exam patterns, and improve through explanations — not memorise copied exam dumps.

Editorial oversight:Johnson Ajibi· MSc IT Security, IEEE Senior Member
20 questionsDomain: Control Flow, Loops, Lists and Logic

What the exam tests

What to know about Control Flow, Loops, Lists and Logic

You must read short Python snippets and correctly state their output or pick code that solves a stated task. The single most important thing is tracking list contents and loop variables step by step, especially around break, continue, and range bounds.

Predicting output of for and while loops, including break, continue, and else clauses

Using list methods like append, insert, remove, sort, and slice indexing correctly

Writing and reading list comprehensions with conditions and expressions

Applying boolean operators and, or, not and comparison chains in if/elif/else logic

Watch out for

Common Control Flow, Loops, Lists and Logic exam traps

  • ▸Assuming a for loop's else clause runs after break; it only runs when the loop ends without break.
  • ▸Forgetting that range() excludes the stop value, causing off-by-one errors in loops and slices.
  • ▸Confusing list methods that mutate in place with expressions that return a new list, like sorted() versus sort().

Practice set

Control Flow, Loops, Lists and Logic questions

20 questions · select your answer, then reveal the explanation

A developer writes a loop to sum numbers from 1 to 10. The code outputs 55, but the expected sum is 55. However, the loop uses a range that includes 0. Which range should be used to achieve the correct sum?

A programmer needs to iterate over a list of strings and print each string in uppercase. Which loop correctly accomplishes this?

A list contains strings and numbers: items = ['apple', 10, 'banana', 20]. A programmer wants to create a new list that contains only the strings. Which approach is correct?

A programmer needs to check if at least one element in a list of booleans flags is True. Which expression correctly does this?

What is the output of the code?

Exhibit

Refer to the exhibit.

for i in range(5):
    if i == 3:
        continue
    print(i, end=' ')

A developer runs main.py and gets True. They then modify config.py by adding `allowed_ports.append(22)` and run main.py again without restarting the interpreter. What is the output?

Exhibit

Refer to the exhibit.

# config.py
allowed_ports = [80, 443, 8080]

def check_port(port):
    if port in allowed_ports:
        return True
    else:
        return False

# main.py
from config import check_port
print(check_port(80))

You are writing a script to parse a log file. Each line in the log contains a timestamp and a message separated by a colon. You need to extract only the messages that contain the word 'ERROR'. The script uses a list comprehension to filter lines. However, the script crashes with a 'ValueError: not enough values to unpack' when processing some lines. The code is:

lines = ['2024-01-01 12:00:00: INFO: all good', '2024-01-01 12:01:00: ERROR: something wrong'] errors = [msg for timestamp, msg in line.split(': ') for line in lines if 'ERROR' in msg]

What is the correct fix?

Match each exception type to its description.

Drag a concept onto its matching description — or click a concept then click the description.

Concepts
Matches

Raised when a function receives an argument of correct type but inappropriate value

Raised when an operation is applied to an object of inappropriate type

Raised when a sequence subscript is out of range

Raised when a mapping key is not found in a dictionary

Raised when division or modulo operation is performed with zero as divisor

Match each Python concept to its description.

Drag a concept onto its matching description — or click a concept then click the description.

Concepts
Matches

A name that references a value in memory

A block of reusable code that performs a specific task

A file containing Python definitions and statements

A collection of modules organized in directories

A blueprint for creating objects

A junior developer needs to write code that processes a list of student scores and stops processing when a score of 100 is encountered, as that score represents a perfect score that should be treated separately. Which loop construct is most appropriate?

A data analyst has a list of temperature readings in Celsius and wants to create a new list containing only readings that are valid (>= -273.15 and <= 1000). Which code correctly creates the filtered list?

A developer is implementing a toggle switch feature. The variable flag starts as False. Which code snippet correctly toggles the flag and performs an action based on the new value each time the code is run? (Assume action1 is performed when flag is True, action2 when False.)

Which code correctly and efficiently sums only positive numbers from a list?

Which three of the following expressions evaluate to True? (Choose three.)

Refer to the exhibit. What is the output?

Exhibit

numbers = [5, 2, 8, 1, 9]
for i in range(len(numbers)):
    for j in range(0, len(numbers)-i-1):
        if numbers[j] > numbers[j+1]:
            numbers[j], numbers[j+1] = numbers[j+1], numbers[j]
print(numbers[0])

A developer needs to write a loop that prints all even numbers from a list. They attempt: for num in numbers: if num % 2 == 0: print(num). However, they want a more efficient approach using list comprehension. Which alternative achieves the same result?

A developer writes: result = [x**2 for x in range(10) if x % 2 == 0]. What does this code produce?

A team observes that the following code prints 'Found' even when the item is not in the list. Code: for item in mylist: if item == target: print('Found'); break; else: print('Not found'). Which modification ensures it correctly prints 'Not found' only if the item is not present?

Which TWO of the following are valid ways to iterate over a list in reverse order? (Choose two.)

What is the output of the code?

Exhibit

Refer to the exhibit.
nums = [1, 2, 3, 4, 5]
for n in nums:
    if n == 3:
        break
    print(n)
else:
    print('Done')

Free account

Track your progress over time

Create a free account to save your results and see which topics improve across sessions.

Focused Control Flow, Loops, Lists and Logic sessions

Start a Control Flow, Loops, Lists and Logic only practice session

Every question in these sessions is drawn from the Control Flow, Loops, Lists and Logic domain — nothing else.

Related practice questions

Related PCEP topic practice pages

Move into related areas when this topic feels solid.

Frequently asked questions

What does the PCEP exam test about Control Flow, Loops, Lists and Logic?
You must read short Python snippets and correctly state their output or pick code that solves a stated task. The single most important thing is tracking list contents and loop variables step by step, especially around break, continue, and range bounds.
How should I use these practice questions?
Select your answer before revealing the explanation. Then read why each option is right or wrong — this active recall approach builds retention far faster than re-reading notes.
Can I practise just Control Flow, Loops, Lists and Logic questions in a focused session?
Yes — the session launcher on this page draws every question from the Control Flow, Loops, Lists and Logic domain. Use a 10-question session first to gauge your baseline, then move to 20 or 30 once the weak spots are clear.
Where can I practise other PCEP topics?
Use the topic links above to move to related areas, or go back to the PCEP question bank to see all topics.
Are these real exam questions or dumps?
These are original practice questions written to test the same concepts the PCEP exam covers. They are not copied from any real exam or dump site.