Exit Chapter 08 ยท ๐Ÿ”€ Flow of Control โ€” Quiz

Playing as a guest? to earn XP and track your progress.

Question 1 of 59 0 correct so far

๐Ÿ”€ Flow of Control Quiz

Q1.

Which three control structures does Python support for the flow of control?

Explanation

Python supports sequence (statements one after another), selection (if statements) and iteration (for and while loops).

Q2.

Which statement does nothing and is used as a placeholder?

Explanation

pass is an empty statement: it keeps the syntax valid where a statement is required but no action is needed.

Q3.

What happens if you write 'if x > 5:' and leave nothing indented under it?

Explanation

Python does not allow an empty body after a colon; use pass as a placeholder.

Q4.

Which of the following is a compound statement?

Explanation

A compound statement (if, for, while) has a header ending with a colon and an indented body.

Q5.

In Python, how is a block of statements defined?

Explanation

Indentation defines a block (4 spaces is standard); wrong indentation causes an error or changes the logic.

Q6.

What is printed when the input is 15?

age = int(input()) if age >= 18: print("You can vote!") print("Thank you")

Explanation

15 >= 18 is False, so the if block is skipped; the last print is outside the if and always runs.

Q7.

In an if-else statement:

Explanation

if-else is a two-way decision: the if block runs when the condition is True, otherwise the else block runs.

Q8.

Which condition correctly tests for a leap year?

Explanation

A leap year is divisible by 400, OR divisible by 4 but not by 100.

Q9.

Using the leap-year rule, which of these is NOT a leap year?

Explanation

1900 is divisible by 100 but not by 400, so it is not a leap year; 2000, 2024 and 1996 are.

Q10.

In an if-elif-else chain, which block runs?

Explanation

Conditions are checked top to bottom, and only the first True block runs; else runs if none is True.

Q11.

In the grade program (>=90 A, >=75 B, >=60 C, >=33 D, else E), what grade is printed for 82?

Explanation

82 >= 90 is False; 82 >= 75 is True, so the grade is B.

Q12.

If 'if marks >= 33:' is checked BEFORE 'marks >= 90' in a grading program, what grade does 95 marks get?

Explanation

Python stops at the first True condition; 95 >= 33 is True, so the grade is wrongly D. Check from highest to lowest.

Q13.

Which keyword does Python use instead of 'else if'?

Explanation

elif is short for 'else if'; writing else if on one line is not valid Python.

Q14.

How many if, elif and else parts can an if-elif-else statement have?

Explanation

There is exactly one if at the start, any number of elif blocks, and at most one else at the end.

Q15.

What does the nested-if program (positive even/odd, negative, zero) print for the input 0?

Explanation

0 > 0 is False and 0 < 0 is False, so the else block prints 'Zero'.

Q16.

What numbers does range(5) generate?

Explanation

With one argument, start defaults to 0 and stop (5) is excluded.

Q17.

What is list(range(1, 10, 2))?

Explanation

It starts at 1, jumps by 2, and stops before 10.

Q18.

What is list(range(10, 0, -2))?

Explanation

It counts down by 2 from 10; the stop value 0 is excluded.

Q19.

What is list(range(5, 2))?

Explanation

With the default step of +1, you cannot go up from 5 to 2, so nothing is generated.

Q20.

What is list(range(2, 11, 3))?

Explanation

It starts at 2 and adds 3 each time: 2, 5, 8; the next value, 11, is excluded.

Q21.

What is list(range(3, 20, 4))?

Explanation

Start at 3 and add 4: 3, 7, 11, 15, 19 (19 is less than 20).

Q22.

What is list(range(20, 3, -5))?

Explanation

Count down by 5 from 20 while staying above 3: 20, 15, 10, 5.

Q23.

What is len(range(3, 15))?

Explanation

With step 1, the number of values in range(a, b) is b โˆ’ a = 15 โˆ’ 3 = 12.

Q24.

Which range should be used to print the numbers 1 to 10?

Explanation

The stop value is excluded, so range(1, 11) gives 1 to 10.

Q25.

What happens with range(1, 10, 0)?

Explanation

A step of 0 raises a ValueError.

Q26.

What happens with range(1.5, 5)?

Explanation

range() accepts only integers; a float argument gives a TypeError.

Q27.

The for loop is described as a:

Explanation

The for loop repeats its block once for each item of a sequence such as a range, string or list.

Q28.

What is the output of this code?

for ch in "PYTHON": print(ch, end="-")

Explanation

Each character is followed by '-' (the end value), including the last one.

Q29.

What is the output of this code?

for i in range(3): print(i * i, end=" ")

Explanation

i takes the values 0, 1, 2, so the squares are 0, 1, 4.

Q30.

What does the factorial program print for input 6?

fact = 1 for i in range(1, n + 1): fact *= i

Explanation

6! = 1 ร— 2 ร— 3 ร— 4 ร— 5 ร— 6 = 720.

Q31.

When is a while loop preferred over a for loop?

Explanation

A while loop repeats as long as a condition is True, which suits cases where the count is unknown (such as summing digits).

Q32.

What are the four parts of a while loop?

Explanation

A while loop needs initialisation, a test condition (checked before each iteration), a body and an update.

Q33.

What does the sum-of-digits program print for 4721?

while num > 0: total += num % 10 num = num // 10

Explanation

The digits 1, 2, 7 and 4 are added: 1 + 2 + 7 + 4 = 14.

Q34.

In a digit-processing while loop, what does num % 10 give?

Explanation

num % 10 gives the last digit; num // 10 removes the last digit.

Q35.

The palindrome program reverses a number with reverse = reverse * 10 + num % 10. For which input does it print 'is a palindrome'?

Explanation

1221 reversed is 1221 โ€” the same โ€” so it is a palindrome.

Q36.

What happens with this code?

count = 1 while count <= 5: print(count)

Explanation

Without count += 1, the condition never becomes False.

Q37.

Which key combination stops an infinite loop in IDLE?

Explanation

Pressing Ctrl + C interrupts a running program in IDLE.

Q38.

What is printed after this code?

x = 10 count = 0 while x > 0: x = x - 3 count += 1 print(x, count)

Explanation

x goes 10 โ†’ 7 โ†’ 4 โ†’ 1 โ†’ -2, so the loop runs 4 times.

Q39.

What is printed after this code?

x = 1 while x < 20: x *= 2 print(x)

Explanation

x goes 1 โ†’ 2 โ†’ 4 โ†’ 8 โ†’ 16 โ†’ 32; at 32 the condition x < 20 is False, so 32 is printed.

Q40.

Which statement about for and while loops is correct?

Explanation

for updates its variable automatically (low infinite-loop risk); while needs a manual update (higher risk).

Q41.

Which for loop is equivalent to this code?

i = 5 while i <= 25: print(i) i += 5

Explanation

The loop prints 5, 10, 15, 20, 25, so the stop must be past 25, e.g. range(5, 26, 5).

Q42.

What does break do inside a loop?

Explanation

break exits the loop completely.

Q43.

What does continue do inside a loop?

Explanation

continue skips the remaining statements of the current iteration only.

Q44.

What is the output of this code?

for i in range(1, 8): if i == 4: break print(i, end=" ")

Explanation

When i is 4 the loop ends, so only 1 2 3 are printed.

Q45.

What is the output of this code?

for i in range(1, 8): if i == 4: continue print(i, end=" ")

Explanation

Only i = 4 is skipped; the loop carries on with 5, 6 and 7.

Q46.

In the prime-check program, why is break used after finding a factor?

Explanation

One factor is enough to show the number is not prime, so break stops unnecessary checks.

Q47.

When does the else block attached to a loop run?

Explanation

The loop else runs whenever the loop finishes without break โ€” even if the body ran zero times.

Q48.

What does prime_with_else print for input 17?

for d in range(2, num): if num % d == 0: print(num, "is not prime") break else: print(num, "is prime")

Explanation

No divisor is found, so break never runs and the loop's else prints '17 is prime'.

Q49.

What is the output of this code?

i = 1 while i <= 3: print(i, end=" ") i += 1 else: print("-> loop over")

Explanation

The loop ends normally when i becomes 4, so the else block also runs.

Q50.

What is the output of this code?

for i in range(1, 10): if i % 3 == 0: continue if i == 8: break print(i, end=" ") else: print("Done") print("End")

Explanation

3 and 6 are skipped by continue; at i = 8 the loop breaks, so the loop's else (Done) does not run.

Q51.

In a nested loop, how many times does the inner loop run for each iteration of the outer loop?

Explanation

For every iteration of the outer loop, the inner loop runs completely.

Q52.

How many lines are printed by this code?

for i in range(1, 4): for j in range(1, 3): print(i, j)

Explanation

The outer loop runs 3 times and the inner loop 2 times each: 3 ร— 2 = 6.

Q53.

How many times does the inner print run?

for i in range(4): for j in range(3): print("*")

Explanation

When the inner count does not depend on the outer variable, total = outer ร— inner = 4 ร— 3 = 12.

Q54.

In pattern programs, what does the outer loop usually control?

Explanation

The outer loop controls the rows; the inner loop controls what is printed in each row.

Q55.

What is the last line printed by this code?

for i in range(1, 5): for j in range(1, i + 1): print(j, end=" ") print()

Explanation

Row i prints the numbers 1 to i, so the last row (i = 4) is 1 2 3 4.

Q56.

What is the first line printed by this code?

for i in range(4, 0, -1): print("*" * i)

Explanation

The first value of i is 4, so "*" * 4 gives ****; the lines get shorter after that.

Q57.

What is the output of this code?

for a in range(3): for b in range(a, 3): print(a + b, end=" ") print()

Explanation

a = 0: b = 0, 1, 2 gives 0 1 2; a = 1: b = 1, 2 gives 2 3; a = 2: b = 2 gives 4.

Q58.

What does the Fibonacci program print for 8 terms?

a, b = 0, 1 for i in range(n): print(a, end=" ") a, b = b, a + b

Explanation

Each term is the sum of the previous two, starting from 0 and 1: 0 1 1 2 3 5 8 13.

Q59.

Which line has an error?

if n > 0 print(n)

Explanation

Every header (if, for, while, else) must end with a colon; otherwise a SyntaxError occurs.