Python Practice Test: Control Flow, If Statements and Loops

  • If Statements and Loops
  • 10 questions
  • 13 minutes

Ten questions that ask you to trace short pieces of code. Tip: write down the value of each variable after every loop pass.

Tap an option to mark your answer. You can change your mind, or tap the same option again to leave the question blank. The correct answers and explanations appear when you finish the test.Tap an option to answer; tap it again to leave the question blank.

Advertisement
Advertisement
  1. Question 1

    total = 0
    for i in range(1, 5):
        total += i
    print(total)

    What does the code print?

  2. Question 2

    n = 0
    while True:
        n += 1
        if n == 3:
            break
    print(n)

    What does the code print?

  3. Question 3

    for i in range(5):
        if i % 2 == 0:
            continue
        print(i)

    What is the output?

  4. Question 4

    x = 15
    if x > 10:
        print("A")
    elif x > 5:
        print("B")
    else:
        print("C")

    What does the code print?

  5. Question 5

    print(list(range(2, 10, 3)))

    What does the code print?

  6. Question 6

    for n in [1, 2, 3]:
        if n == 5:
            break
    else:
        print("no break")

    What does the code print?

  7. Question 7

    print(not (3 > 2 and 1 > 2))

    What does the code print?

  8. Question 8

    count = 0
    for i in range(3):
        for j in range(2):
            count += 1
    print(count)

    What does the code print?

  9. Question 9

    if 4 > 3:
    print("yes")

    What happens when this code runs?

  10. Question 10

    x = 7
    print("even" if x % 2 == 0 else "odd")

    What does the code print?

Questions you leave blank are not counted as wrong.

Advertisement

More Python tests

All Python Programming tests
13:00

Do you want to finish the test?