PCEP-30-02: Control Flow

Try 10 focused PCEP-30-02 questions on Control Flow, with explanations, then continue with IT Mastery.

On this page

Open the matching IT Mastery practice page for timed mocks, topic drills, progress tracking, explanations, and full practice.

Try PCEP-30-02 on Web View full PCEP-30-02 practice page

Topic snapshot

FieldDetail
Exam routePCEP-30-02
Topic areaBlock 2: Control Flow - Conditional Blocks and Loops
Blueprint weight29%
Page purposeFocused sample questions before returning to mixed practice

How to use this topic drill

Use this page to isolate Block 2: Control Flow - Conditional Blocks and Loops for PCEP-30-02. Work through the 10 questions first, then review the explanations and return to mixed practice in IT Mastery.

PassWhat to doWhat to record
First attemptAnswer without checking the explanation first.The fact, rule, calculation, or judgment point that controlled your answer.
ReviewRead the explanation even when you were correct.Why the best answer is stronger than the closest distractor.
RepairRepeat only missed or uncertain items after a short break.The pattern behind misses, not the answer letter.
TransferReturn to mixed practice once the topic feels stable.Whether the same skill holds up when the topic is no longer obvious.

Blueprint context: 29% of the practice outline. A focused topic score can overstate readiness if you recognize the pattern too quickly, so use it as repair work before timed mixed sets.

Sample questions

These questions are original IT Mastery practice items aligned to this topic area. They are designed for self-assessment and are not official exam questions.

Question 1

Topic: Block 2: Control Flow - Conditional Blocks and Loops

A student wants to handle digits later and uses pass as placeholder logic. What is the exact output of this code?

text = ""

for ch in "a1b":
    if ch.isdigit():
        pass
    text += ch

print(text)

Options:

  • A. ab

  • B. A syntax error occurs

  • C. a1b

  • D. 1

Best answer: C

Explanation: pass is a placeholder statement that does nothing. Because the concatenation line is outside the if block, it runs on every loop iteration, so all three characters are added and the output is a1b.

pass is a null statement: it makes an empty block legal, but it does not skip, stop, or change the surrounding control flow. In this loop, when ch is '1', the condition ch.isdigit() is true, so Python executes pass and then continues to the next line in the loop body.

  • ch = 'a' → append 'a'
  • ch = '1' → execute pass, then append '1'
  • ch = 'b' → append 'b'

So text becomes a1b. The common mistake is to confuse pass with continue, but continue would skip the append and pass would not.

  • Skipping the digit would require continue; pass leaves execution unchanged.
  • Keeping only 1 ignores that the append line runs for every character in the loop.
  • Syntax error is incorrect because pass is valid Python and is commonly used for placeholder blocks.

Question 2

Topic: Block 2: Control Flow - Conditional Blocks and Loops

A beginner wants this program to add the numbers 1 through 4 and print 10, but it prints 6 instead.

total = 0

for n in range(1, 4):
    total += n

print(total)

What is the best explanation for the wrong output?

Options:

  • A. range(1, 4) stops before 4, so only 1, 2, and 3 are added.

  • B. total is reset to 0 during each loop iteration.

  • C. print(total) must be indented inside the loop.

  • D. The loop runs one extra time because range(1, 4) includes 4.

Best answer: A

Explanation: The key idea is how range(start, stop) works in a for loop. Here, range(1, 4) includes 1 and stops before 4, so the program adds only 1 + 2 + 3, which equals 6.

In Python, range(start, stop) includes the start value but excludes the stop value. That means range(1, 4) generates these loop values: 1, 2, and 3.

So the program does this:

  • start with total = 0
  • add 1 - total becomes 1
  • add 2 - total becomes 3
  • add 3 - total becomes 6

After the loop ends, print(total) displays 6. If the goal is to include 4, the loop would need a stop value of 5. Indenting print(total) would change when the total is shown, but not which numbers range(1, 4) produces.

  • Reset confusion fails because total = 0 appears once before the loop, not inside it.
  • Range misunderstanding fails because the stop value in range() is excluded, not included.
  • Indentation mix-up fails because moving print(total) inside the loop would print intermediate totals, not fix the missing 4.

Question 3

Topic: Block 2: Control Flow - Conditional Blocks and Loops

Consider this program:

def increase(value):
    while value < 10:
        value += 2
    return value

print(increase(5))

What causes the while loop inside increase() to stop?

Options:

  • A. The loop stops only when value is exactly 10.

  • B. value < 10 becomes false after value changes to 11.

  • C. return value immediately ends the loop before the next check.

  • D. print(increase(5)) stops the loop after the function call begins.

Best answer: B

Explanation: A while loop stops when its condition evaluates to False. In increase(5), the values become 5, 7, 9, and then 11, so the loop ends because 11 < 10 is false; the return runs afterward.

A while loop in a function follows the same rule as anywhere else: it repeats only while its condition is True. Here, the condition is value < 10, and the function starts with value = 5. Each loop pass adds 2, so the value changes from 5 to 7, then 9, then 11.

  • 5 < 10 is True, so the loop runs.
  • 7 < 10 is True, so the loop runs again.
  • 9 < 10 is True, so the loop runs again.
  • 11 < 10 is False, so the loop stops.

After the condition becomes false, execution moves to return value. The key takeaway is that a while loop stops when its test is false, not because return or print() exists elsewhere in the program.

  • The option claiming return value stops the loop mixes up loop control with function exit; return is reached only after the loop has already ended.
  • The option requiring value to be exactly 10 is wrong because the value can skip over 10 and still make the condition false.
  • The option blaming print(increase(5)) is wrong because print() runs after the function call finishes, not during the loop test.

Question 4

Topic: Block 2: Control Flow - Conditional Blocks and Loops

A ticket kiosk should show extra messages only when seats are available. Consider this code:

seats_available = 4

if seats_available > 0:
    print("Choose a seat")
    print("Continue payment")

print("Session ended")

When this program runs, which TWO statements are executed because the if condition is true?

Options:

  • A. An else block runs

  • B. The indented block is skipped

  • C. print("Session ended")

  • D. A SyntaxError occurs

  • E. print("Choose a seat")

  • F. print("Continue payment")

Correct answers: E and F

Explanation: The condition seats_available > 0 is true because 4 > 0. In a simple if statement, Python executes the indented block when the condition is true, so both indented print() calls run because of that branch. The unindented line is not part of the if branch.

When Python reaches an if statement, it evaluates the condition first. Here, seats_available > 0 becomes true because 4 is greater than 0. Since the condition is true, Python runs the statements indented directly under the if.

  • Check the condition.
  • If it is True, execute the indented block.
  • Then continue with the next unindented statement.

In this code, the two indented print() lines belong to the true branch. The key idea is indentation: it shows which statements are controlled by the if and which statements are outside that branch.

  • The option with print("Session ended") is outside the indented block, so it runs afterward but not because the if condition was true.
  • The option claiming the indented block is skipped confuses true and false conditions; skipping happens when the condition is false.
  • The option about an else block is incorrect because no else appears in the code.
  • The option claiming a SyntaxError occurs is incorrect because the if statement and indentation are valid.

Question 5

Topic: Block 2: Control Flow - Conditional Blocks and Loops

A program already has a known list of scores and must add each score to total exactly once. Which line should replace ????

scores = [10, 8, 9, 7]
total = 0

???
    total += score

Options:

  • A. for score in range(len(scores)):

  • B. while len(scores) > 0:

  • C. if score in scores:

  • D. for score in scores:

Best answer: D

Explanation: Use a for loop when you already have a sequence and need to process each element one by one. for score in scores: gives score the actual list values, so the body adds 10, 8, 9, and 7 to total.

In Python, a for loop is the standard way to process each item in a known sequence such as a list, tuple, or string. With for score in scores:, the variable score takes the values 10, 8, 9, and 7 in order, and the indented block runs once for each value. That matches the requirement exactly.

Using range(len(scores)) produces index numbers, not the list values. An if statement checks a condition only once, so it does not iterate through the sequence. The while condition shown stays true as long as the list is unchanged and still does not assign a value to score.

When you need each value from an existing sequence, iterate over the sequence directly with for item in sequence:.

  • range(len(scores)) loops over positions 0 to 3, so it would add indexes instead of score values.
  • The if option checks one condition only once and never sets score for repeated processing.
  • The while option does not step through the list items and leaves score undefined.

Question 6

Topic: Block 2: Control Flow - Conditional Blocks and Loops

A beginner wants this loop to accumulate the sum of all numbers in prices, but it currently keeps only the last number. Which change correctly fixes the loop logic?

prices = [4, 7, 2]
total = 0

for price in prices:
    total = price

print(total)

Options:

  • A. Replace print(total) with print(sum(prices))

  • B. Replace total = price with total += price

  • C. Replace total = price with total = total + 1

  • D. Replace total = price with price += total

Best answer: B

Explanation: To accumulate a sum, the loop must add each element to an existing running total. total += price preserves the previous total and updates it on every iteration, so total ends with the sum of the list.

An accumulator is a variable that keeps a running result while a for loop visits each item in a sequence. Here, total is initialized once before the loop, but total = price overwrites the running total each time, so after the loop total holds only the last list element.

  • Start with total = 0
  • Add 4 to get 4
  • Add 7 to get 11
  • Add 2 to get 13

Using total += price follows the standard accumulation pattern: keep the old total and add the current item. A change that only prints a result without repairing the loop does not fix the loop logic itself.

  • Wrong variable Updating price changes the loop variable, not the running total that is printed.
  • Counts items Adding 1 each time counts how many elements were visited instead of summing their values.
  • Bypasses the loop Printing sum(prices) can show the total, but it does not repair the broken accumulation inside the loop.

Question 7

Topic: Block 2: Control Flow - Conditional Blocks and Loops

A beginner writes this function:

def label_score(score):
    if score < 50:
        # add low-score message later
    return "checked"

print(label_score(40))

The program fails before the call runs. Which change fixes the problem while keeping the if branch as a temporary placeholder?

Options:

  • A. Add pass inside the if block.

  • B. Add continue inside the if block.

  • C. Keep only the comment; the block is already valid.

  • D. Add break inside the if block.

Best answer: A

Explanation: Python requires every if block to contain at least one real statement. A comment does not count, so pass is the correct placeholder when the branch should stay empty for now.

Python block headers such as if, for, and while must be followed by an indented statement. In this function, the line under if score < 50: is only a comment, and comments are ignored by the parser. That means the if body is actually empty, so Python raises an error before print(label_score(40)) can run.

def label_score(score):
    if score < 50:
        pass
    return "checked"

pass is a special placeholder statement that does nothing, but it satisfies Python’s syntax rules. It is the standard fix when you want to leave conditional or loop logic unfinished temporarily.

  • continue works only inside a loop, so it cannot be used to fill a plain if block.
  • break also requires a loop and is invalid in this function’s if branch.
  • The comment-only choice fails because comments do not count as executable statements in a block.

Question 8

Topic: Block 2: Control Flow - Conditional Blocks and Loops

A student says this program should print Password too short for the shown value, but Python stops with a syntax error at else:. They want You can log in to appear only when the password is accepted. What is the best fix?

password = "cat"

if len(password) >= 8:
    print("Password accepted")
print("You can log in")
else:
    print("Password too short")

Options:

  • A. Indent print("You can log in") under the if.

  • B. Change else: to elif len(password) < 8:.

  • C. Add parentheses around len(password) >= 8.

  • D. Remove the colon after the if condition.

Best answer: A

Explanation: In Python, indentation determines which statements belong to a conditional block. Because print("You can log in") is not indented, it sits outside the if block, so else no longer directly follows its matching if.

Python uses indentation to define the statements controlled by if, elif, and else. Only the indented lines after if len(password) >= 8: belong to that block. Here, print("You can log in") is aligned at the left margin, so Python treats the if block as finished before it reaches else:.

That creates a syntax problem, because else must align with its matching if and come immediately after that conditional block. If You can log in should happen only when the password is accepted, it must be indented so it is controlled by the if.

Changing the condition or adding parentheses does not fix the block structure.

  • Remove the colon makes the if statement invalid instead of repairing the conditional structure.
  • Use elif instead still leaves a top-level statement between the branches, so the syntax problem remains.
  • Add parentheses changes nothing about indentation or which statements are controlled by the if.

Question 9

Topic: Block 2: Control Flow - Conditional Blocks and Loops

A student wants this program to print 5, 3, 1 and then stop, but it enters an infinite loop.

n = 5
while n > 0:
    if n % 2 == 0:
        continue
    print(n)
    n -= 1

Which single change fixes the control flow and keeps the intended output?

Options:

  • A. Change while n > 0 to while n >= 0.

  • B. Add n -= 1 before continue in the if block.

  • C. Move n -= 1 to the top of the loop.

  • D. Replace continue with break.

Best answer: B

Explanation: The loop becomes infinite because continue skips the update when n is even. In a while loop, the control variable must change on every repeating path, so decrementing n before continuing fixes the loop and preserves the odd-number output.

In a while loop, the value used in the condition must be updated on every path that repeats the loop. Here, n starts at 5, prints once, and becomes 4. On the next iteration, n % 2 == 0 is true, so continue sends control back to the top of the loop before n -= 1 runs. That leaves n stuck at 4, so while n > 0 stays true forever.

  • If n is even, decrement it before continue.
  • If n is odd, print it and then decrement it.

With that change, n moves from 5 to 0, and the program prints 5, 3, 1 as intended. The closest distractor stops the loop, but it does not preserve the required output.

  • Replacing continue with break stops at the first even value, so the program ends too early.
  • Changing the condition to n >= 0 does not help because n still gets stuck at 4.
  • Moving the decrement to the top changes the output by skipping the initial 5.

Question 10

Topic: Block 2: Control Flow - Conditional Blocks and Loops

A beginner writes this function to add odd numbers below a limit. What is printed?

def total_from(limit):
    total = 0
    for n in range(1, limit, 2):
        total += n
    return total

print(total_from(7))

Options:

  • A. 16

  • B. 21

  • C. 9

  • D. None

Best answer: C

Explanation: The loop uses range(1, 7, 2), so it visits 1, 3, and 5; the stop value 7 is not included. The function returns 9, and the outer print() prints that returned value.

This question depends on tracing a for loop with range(start, stop, step). In Python, the stop value is excluded, so range(1, 7, 2) generates 1, 3, and 5.

  • Start with total = 0
  • Add 1 - total becomes 1
  • Add 3 - total becomes 4
  • Add 5 - total becomes 9

After the loop ends, the function uses return total, so the call total_from(7) evaluates to 9. Then the outer print() displays that value. The closest mistake is treating 7 as included in the range(), but Python stops before it.

  • Including 7 fails because the stop value in range(1, 7, 2) is excluded.
  • Adding every number ignores the step of 2, so the loop does not visit even numbers.
  • Expecting None would make sense only without a return value, but the function explicitly returns total.

Continue with full practice

Use the PCEP-30-02 Practice Test page for the full IT Mastery route, mixed-topic practice, timed mock exams, explanations, and web/mobile app access.

Try PCEP-30-02 on Web View PCEP-30-02 Practice Test

Free review resource

Read the PCEP-30-02 Cheat Sheet on Tech Exam Lexicon, then return to IT Mastery for timed practice.

Revised on Thursday, May 14, 2026