Python Control Flow Quiz

Quiz 7 10 questions

Are you ready?

10 questions to solve

Instructions
  1. This quiz goes to full-screen once you press the Start button.
  2. At the end of the quiz, you are able to review all the questions that you answered wrong and see their explanations.
What is the problem in the code below?
for i in range(10):
    print(i)
The code shown here is perfectly valid. Hence, the correct choice is (C).
What is the problem in the code below?
for in range(10):
    print(i)
The header of a for loop begins with the for keyword followed by the name of a variable that'll be assigned each item in the given sequence. This variable is also known as a loop variable. Here the loop variable is missing, which leads to an error.

Hence, the correct choice is (B). For more info on how to define a for loop, refer to Python for loop.
What will the following code print?
for i in range(-10, 0):
    print(i, end=' ')
range(-10, 0) denotes the sequence -10 -9 -8 -7 -6 -5 -4 -3 -2 -1. It begins at -10 and progresses by adding 1 to the previous term, and goes all the way upto the number -1 (the integer just before 0).

Hence, the correct choice is (B). For more details on range(), please refer to Python for loop — the range() function.
What does the following code print?
x = 10

if type(x) == 'int':
    if x % 2 == 0:
        print(1)
    else:
        print(2)
else:
    print(3)
x is an integer, likewise type(x) returns a reference to the int class. It doesn't return the string 'int', so consequently the condition in line 3 fails, shifting execution to its corresponding else block in line 9. What gets printed ultimately is 3, and this goes with choice (C).
What does the following code print?

There's no typo in the code.

x = 15

if x % 2 == 0 OR x % 3 == 0:
    print(1)
else:
    print(2)
right away by seeing the code above, we can spot the mistyped word OR. There is no such keyword in Python, likewise it leads to an error. The correct casing for the word is or. Hence, the correct choice is (C).
What does the following code print?
x = 30

if x % 2 == 0 and x % 3 == 0:
    print(1)
else:
    print(2)
x = 30 is both, divisible by 2 and by 3. Consequently, the condition in line 3 evaluates to True, ultimately printing 1 to the shell. Hence, the correct choice is (A).
What's the problem in the code below?
a = 20

if a == 10:
    print(1)
else:
    print(2)
elif a == 20:
    print(3)
elif must come after an if keyword and before an else keyword. Here, it comes after an else, which is invaid in Python. Hence, the correct description of the problem in the code above is (A). For more details, refer to Python Conditional Statements.
What does the following code print?
x = 0

while x == 0:
    print(x)
Initially, the condition x == 0 evaluates to True, hence the while loop executes for the first time. Then, x never changes from inside while, likewise the header of the loop keeps on evaluating to True and executing the loop. This is commonly referred to as an infinite loop — a loop that never ends.

Hence, the correct choice is (B). For more details on infinite loops, refer to Python while loop.
What does the following code print?
x = 1

while x == 0:
    print(x)
Initially, the loop's condition x == 0 evaluates to False, likewise execution never goes into the loop. This goes with choice (B). For more details, refer to Python while loop.
What does the following code print?
x = 0

while x == 0 or x == 1:
    print(x, end=' ')
    x += 1
Initially, x == 0 or x == 1 evaluates to True, likewise the loop executes for the first time. Then x is incremented to 1. Once again x == 0 or x == 1 evaluates to True, executing the loop for the second time. Then x becomes 2 and so the loop's condition evaluates to False.

Altogether two prints are made to the shell, giving the output 0 1, which goes with choice (B). For more details, refer to Python while loop.