Skip to content
QuiznExam

Python Control Statements Quiz – 20 Questions with Answers

Test your knowledge of control statements in Python with 20 questions covering if, elif, else, nested conditions, for loops, while loops, break, continue, pass, and loop control.

20
Questions
15 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. Which keyword is used to execute a block of code when a condition is true in Python?

    • A. if
    • B. when
    • C. check
    • D. condition
    Show answer

    A. if

    The if statement executes its indented block when its condition evaluates to True.

  2. 2. Which keyword is used to test another condition when the preceding if or elif condition is false?

    • A. else if
    • B. elif
    • C. elseif
    • D. otherwise
    Show answer

    B. elif

    The elif keyword allows Python to test another condition after a preceding if or elif condition is false.

  3. 3. Which keyword executes a block when all preceding if and elif conditions are false?

    • A. otherwise
    • B. default
    • C. else
    • D. fallback
    Show answer

    C. else

    The else block executes when none of the preceding if or elif conditions evaluates to True.

  4. 4. What is the output of the following code? x = 10; if x > 5: print("Yes")

    • A. Yes
    • B. No
    • C. 10
    • D. Error
    Show answer

    A. Yes

    The condition x > 5 is true because 10 is greater than 5, so the print statement executes.

  5. 5. Which Python loop is commonly used to iterate over the elements of a sequence?

    • A. for
    • B. repeat
    • C. loop
    • D. iterate
    Show answer

    A. for

    The for loop iterates over items in an iterable such as a list, tuple, string, or range.

  6. 6. Which loop continues executing as long as its condition remains true?

    • A. for
    • B. while
    • C. repeat
    • D. until
    Show answer

    B. while

    A while loop repeatedly executes its body while its condition evaluates to True.

  7. 7. What is the output of this code? for i in range(3): print(i)

    • A. 1 2 3
    • B. 0 1 2
    • C. 0 1 2 3
    • D. 3 2 1
    Show answer

    B. 0 1 2

    range(3) produces the values 0, 1, and 2, so those values are printed.

  8. 8. Which statement immediately terminates the nearest enclosing loop?

    • A. stop
    • B. exit
    • C. break
    • D. terminate
    Show answer

    C. break

    The break statement immediately exits the nearest enclosing for or while loop.

  9. 9. Which statement skips the rest of the current loop iteration and continues with the next iteration?

    • A. skip
    • B. continue
    • C. next
    • D. pass
    Show answer

    B. continue

    The continue statement skips the remaining statements in the current iteration and proceeds to the next iteration.

  10. 10. What does the pass statement do in Python?

    • A. Terminates the program
    • B. Skips the entire loop
    • C. Acts as a placeholder and performs no operation
    • D. Repeats the current iteration
    Show answer

    C. Acts as a placeholder and performs no operation

    The pass statement does nothing when executed and is commonly used where Python syntax requires a statement.

  11. 11. What is the output of this code? x = 15; if x > 20: print("A"); else: print("B")

    • A. A
    • B. B
    • C. 15
    • D. Nothing
    Show answer

    B. B

    The condition x > 20 is false because x is 15, so the else block executes.

  12. 12. Which statement correctly represents a nested conditional in Python?

    • A. An if statement inside another if statement
    • B. An if statement without a condition
    • C. An else statement before an if statement
    • D. Two unrelated loops
    Show answer

    A. An if statement inside another if statement

    A nested conditional occurs when one conditional statement is placed inside the block of another conditional statement.

  13. 13. What is the output of this code? for i in range(5): if i == 3: break; print(i)

    • A. 0 1 2
    • B. 0 1 2 3
    • C. 1 2 3
    • D. 0 1 2 3 4
    Show answer

    A. 0 1 2

    The loop prints 0, 1, and 2. When i becomes 3, break terminates the loop before 3 is printed.

  14. 14. What is the output of this code? for i in range(5): if i == 2: continue; print(i)

    • A. 0 1 2 3 4
    • B. 0 1 3 4
    • C. 1 2 3 4
    • D. 0 2 4
    Show answer

    B. 0 1 3 4

    When i equals 2, continue skips the print statement for that iteration, while the other values are printed.

  15. 15. Which function is commonly used with a for loop to generate a sequence of integers?

    • A. range()
    • B. sequence()
    • C. numbers()
    • D. iterate()
    Show answer

    A. range()

    The range() function produces an iterable sequence of integers commonly used with for loops.

  16. 16. What happens if the condition of a while loop is initially False?

    • A. The loop body executes once
    • B. The loop body does not execute
    • C. The loop runs forever
    • D. Python automatically changes the condition
    Show answer

    B. The loop body does not execute

    A while loop checks its condition before each iteration, so a false initial condition means its body is skipped.

  17. 17. What keyword can be used with a loop to execute a block when the loop completes normally without encountering break?

    • A. finally
    • B. else
    • C. after
    • D. complete
    Show answer

    B. else

    Python loops can have an else clause that executes when the loop finishes normally rather than through a break statement.

  18. 18. What is the output of this code? for i in range(3): print(i) else: print("Done")

    • A. 0 1 2
    • B. Done
    • C. 0 1 2 Done
    • D. 0 1 Done
    Show answer

    C. 0 1 2 Done

    The loop prints 0, 1, and 2 and then completes normally, so its else block prints Done.

  19. 19. Which operator is commonly used to combine multiple conditions so that all specified conditions must be true?

    • A. or
    • B. and
    • C. not
    • D. xor
    Show answer

    B. and

    The and operator produces True when all of its operands are true.

  20. 20. Which operator reverses a Boolean condition in Python?

    • A. reverse
    • B. invert
    • C. not
    • D. opposite
    Show answer

    C. not

    The not operator reverses a Boolean value, turning True into False and False into True.