0% found this document useful (0 votes)
14 views31 pages

Control Flow

Uploaded by

mrnobita332211
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views31 pages

Control Flow

Uploaded by

mrnobita332211
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

100 multiple‐choice questions (MCQs) on Python’s Control Flow

Q1. Which keyword is used to begin a conditional statement in Python?


A) if
B) for
C) while
D) def
Answer: A

Q2. Which keyword is used for an alternative branch when an if condition is not met?
A) elif
B) else
C) finally
D) except
Answer: B

Q3. Which keyword can be used to test multiple conditions sequentially?


A) if
B) else
C) elif
D) then
Answer: C

Q4. How does Python indicate a block of code following a control flow statement?
A) Curly braces { }
B) Parentheses ( )
C) Indentation
D) Square brackets [ ]
Answer: C

Q5. What is the output of the following code?

if False:
print("Hello")
else:
print("World")

A) Hello
B) World
C) HelloWorld
D) No output
Answer: B

Q6. Which loop is most appropriate for iterating over items in a sequence?
A) while loop
B) for loop
C) do-while loop
D) if loop
Answer: B

Q7. Which loop repeatedly executes as long as a condition is True?


A) for loop
B) while loop
C) switch loop
D) until loop
Answer: B

Q8. What does the break statement do in a loop?


A) Skips the current iteration
B) Exits the loop immediately
C) Pauses the loop
D) Restarts the loop
Answer: B

Q9. What does the continue statement do in a loop?


A) Exits the loop
B) Skips the remainder of the current iteration and continues with the next
C) Resets the loop counter
D) Ends the program
Answer: B

Q10. Which statement in Python serves as a no-operation placeholder?


A) skip
B) pass
C) continue
D) break
Answer: B

Q11. What is the purpose of an else clause in a loop?


A) It is executed every iteration
B) It is executed when the loop condition is false
C) It is executed after the loop completes normally
D) It is executed if an error occurs
Answer: C
Q12. Which of the following correctly shows the syntax of an if-elif-else construct?
A) if condition: …; elif condition: …; else: …
B) if condition then …; else if condition then …; otherwise …
C) if (condition) { … } else { … }
D) if condition do …; elif condition do …; else do …
Answer: A

Q13. In Python, what happens if none of the conditions in an if-elif chain are met and there is no else clause?
A) A SyntaxError is raised
B) The program crashes
C) Nothing happens; the block is skipped
D) It executes the last elif
Answer: C

Q14. What is a common use of the ternary operator in Python?


A) To define a loop
B) To execute a one-line conditional expression
C) To create a new function
D) To declare a variable type
Answer: B

Q15. Which of the following is the correct syntax for a ternary (conditional) expression in Python?
A) result = true_value if condition else false_value
B) result = if condition then true_value else false_value
C) result = condition ? true_value : false_value
D) result = (if condition, true_value, else false_value)
Answer: A
Q16. What is the output of the code snippet below?

x = 10
if x > 5:
print("High")
elif x == 5:
print("Equal")
else:
print("Low")

A) High
B) Equal
C) Low
D) No output
Answer: A

Q17. Which keyword is used to exit a loop prematurely?


A) pass
B) continue
C) exit
D) break
Answer: D

Q18. What does the following code print?

for i in range(5):
if i == 3:
break
print(i)
A) 0 1 2 3 4
B) 0 1 2 3
C) 0 1 2
D) 1 2 3
Answer: C

Q19. In a while loop, when is the condition checked?


A) Only once at the beginning
B) At the beginning of each iteration
C) Only at the end of the loop
D) Randomly during the loop
Answer: B

Q20. Which of the following loops will execute at least once regardless of the condition?
A) for loop
B) while loop
C) do-while loop (if implemented)
D) None in Python, since Python does not have a built-in do-while loop
Answer: D

Q21. What is the result of the following code?

x=5
while x:
print(x)
x -= 2

A) 5 3 1
B) 5 4 3 2 1
C) 5 3 0
D) 5 3 2 1
Answer: A

Q22. Which of the following is true about nested loops in Python?


A) They are not supported
B) The inner loop must end before the outer loop continues
C) Only one level of nesting is allowed
D) They require a special keyword
Answer: B

Q23. What will the following code output?

for i in range(3):
for j in range(2):
print(i, j)

A) It prints pairs for each i and j in the specified ranges


B) It prints only the value of i
C) It prints only the value of j
D) It produces a SyntaxError
Answer: A

Q24. In a for loop iterating over a list, what does the loop variable represent?
A) The index of the element
B) The element itself
C) A tuple of index and element
D) A pointer to the element
Answer: B

Q25. What is the purpose of using the built-in function range() in loops?
A) To generate a sequence of numbers
B) To create a list
C) To sort items
D) To enumerate items
Answer: A

Q26. Which of the following correctly creates a list of numbers from 0 to 4?


A) list(range(5))
B) range(5)
C) range(0, 5, 1)
D) Both A and C
Answer: D

Q27. What does the loop "for i in range(0)" do?


A) Iterates once with i = 0
B) Does not iterate at all
C) Raises an error
D) Iterates indefinitely
Answer: B

Q28. Which statement about the else clause in loops is correct?


A) It executes if the loop finishes normally (without break)
B) It always executes after the loop
C) It executes only if the loop condition is false from the start
D) It is only valid in for loops
Answer: A
Q29. What is the output of the code below?

for i in range(3):
print(i)
else:
print("Done")

A) 0 1 2 Done
B) 0 1 2
C) Done
D) 0 Done 1 Done 2 Done
Answer: A

Q30. Which loop control structure would you use to skip printing even numbers from 0 to 4?
A) Use if with break
B) Use if with continue
C) Use else clause
D) Use pass
Answer: B

Q31. What does the following code print?

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

A) 0 2 4
B) 1 3
C) 0 1 2 3 4
D) 2 4
Answer: B

Q32. Which keyword is used to indicate that no action should be performed within a block?
A) null
B) pass
C) none
D) skip
Answer: B

Q33. How can you write a one-line conditional in Python to print "Yes" if a variable x is greater than 10?
A) if x > 10: print("Yes")
B) print("Yes") if x > 10 else None
C) print("Yes") if x > 10
D) Both A and B
Answer: D

Q34. Which of the following is an example of nested control flow?


A) An if statement inside a for loop
B) A function definition inside an if block
C) A for loop inside another for loop
D) All of the above
Answer: D

Q35. What happens if the break statement is executed inside a nested loop?
A) It breaks only the inner loop
B) It breaks both inner and outer loops
C) It breaks only the outer loop
D) It causes a SyntaxError
Answer: A

Q36. Which clause can be added to a try-except block to execute code regardless of whether an exception occurred or not?
A) else
B) finally
C) pass
D) continue
Answer: B
(While related to exception control flow, the finally clause is a control structure for cleanup.)

Q37. What is the output of the following code fragment?

x=0
while x < 4:
print(x)
x += 1
else:
print("Finished")

A) 0 1 2 3 Finished
B) 0 1 2 3
C) Finished
D) 1 2 3 4 Finished
Answer: A

Q38. Which of the following is correct about Python’s for loop?


A) It always iterates over numbers
B) It can iterate over any iterable object
C) It is only used with lists
D) It does not support iteration over strings
Answer: B

Q39. What does the enumerate() function do when used in a loop?


A) Generates a sequence of numbers
B) Returns a tuple containing index and value for each element
C) Sorts the elements of the iterable
D) Filters out even-indexed elements
Answer: B

Q40. What is the result of the code below?

for index, value in enumerate("ABC"):


print(index, value)

A) 0 A
1B
2C
B) A 0, B 1, C 2
C) A B C
D) 1 A, 2 B, 3 C
Answer: A

Q41. Which control flow construct would you use to wait for user input before proceeding in a loop?
A) time.sleep()
B) input() inside the loop
C) break
D) continue
Answer: B

Q42. What is the output of the following nested loop code?

for i in range(2):
for j in range(3):
print(i, j, end=";")

A) 0 0; 0 1; 0 2; 1 0; 1 1; 1 2;
B) 0 0; 1 0; 0 1; 1 1; 0 2; 1 2;
C) 0 0; 1 1; 0 2; 1 0; 1 2; 0 1;
D) A SyntaxError occurs
Answer: A

Q43. Which of the following best describes the pass statement?


A) It terminates a loop
B) It does nothing and is used as a placeholder
C) It skips to the next iteration
D) It returns a value
Answer: B

Q44. What is printed by the following code?

x=3
if x:
print("Nonzero")
else:
print("Zero")

A) Zero
B) Nonzero
C) 3
D) Nothing
Answer: B

Q45. Which control-flow structure is best for iterating over keys in a dictionary?
A) while loop
B) for loop
C) if-else structure
D) do-while loop
Answer: B

Q46. What will the following code output?

for i in []:
print("Loop")
else:
print("No Iteration")

A) Loop
B) No Iteration
C) Nothing
D) SyntaxError
Answer: B

Q47. Which of the following loop constructs does not require an explicit counter variable?
A) for loop over range()
B) while loop
C) for loop over an iterable
D) Both B and C
Answer: D

Q48. What happens if the condition in a while loop is initially False?


A) The loop body executes once
B) The loop body never executes
C) A SyntaxError occurs
D) The condition becomes True automatically
Answer: B

Q49. Which of the following code snippets correctly uses an if statement to check if a value is in a list?
A) if value in list: ...
B) if value not in list: ...
C) if list.contains(value): ...
D) Both A and B (depending on desired behavior)
Answer: D

Q50. What is the function of an else clause attached to a for loop?


A) It executes only if a break has occurred
B) It executes after the for loop completes normally
C) It executes before the loop
D) It is invalid syntax
Answer: B

Q51. What is printed by the following code?


i=0
while i < 3:
print(i, end=" ")
i += 1
print("Done")

A) 0 1 2 Done
B) 0 1 2 3 Done
C) 1 2 3 Done
D) Done only
Answer: A

Q52. Which statement below about loop control with break and continue is correct?
A) Both break and continue exit the loop
B) break stops the loop, continue skips to the next iteration
C) continue stops the loop, break pauses it
D) Neither affect the loop’s execution
Answer: B

Q53. How do you execute code after a loop that has not been terminated by a break statement?
A) Using an else clause attached to the loop
B) Writing code directly after the loop block
C) Using a finally clause
D) Both A and B
Answer: D

Q54. Which construct would you use to check for multiple unrelated conditions and execute corresponding blocks?
A) Single if statement
B) if-elif-else chain
C) Nested loops
D) switch-case
Answer: B

Q55. What is the outcome when the condition in a while loop becomes False?
A) The loop executes one more time
B) The loop immediately exits
C) The condition is re-evaluated
D) The program crashes
Answer: B

Q56. Which of the following is used to shorten a multi-line if statement when it contains a single statement?
A) Inline if
B) Ternary operator
C) Semicolon separation
D) Indented block
Answer: B

Q57. What will the following code print?

x=0
while x < 3:
if x == 1:
x += 1
continue
print(x)
x += 1

A) 0 1 2
B) 0 2
C) 1 2
D) 0 1
Answer: B

Q58. Which of these code snippets demonstrates a nested if statement?


A)

if a:
if b:
print("Both true")

B) if a and b: print("Both true")


C) if a: pass
D) if a: print("A"); else: print("Not A")
Answer: A

Q59. In an if-elif-else structure, if multiple conditions are True, which block is executed?
A) All true blocks
B) The first block whose condition is True
C) The last block whose condition is True
D) None of the above
Answer: B

Q60. Which operator is commonly used with if statements to combine conditions?


A) &&
B) &
C) and
D) or
Answer: C

Q61. What is the output of the code below?

if 0:
print("Zero is True")
else:
print("Zero is False")

A) Zero is True
B) Zero is False
C) Error
D) No output
Answer: B

Q62. Which of the following is a valid use of the ternary operator?


A) result = "Yes" if condition else "No"
B) if condition: result = "Yes" else: result = "No"
C) result = condition ? "Yes" : "No"
D) result = (condition, "Yes", "No")
Answer: A

Q63. What is printed by this code segment?

for i in range(3):
if i == 1:
break
print(i)
else:
print("Finished")

A) 0 Finished
B) 0
C) 0 1 Finished
D) 1 Finished
Answer: B

Q64. In a while loop, which statement can be used to ensure that an inner loop does not run forever?
A) A conditional break
B) The continue statement
C) The pass statement
D) No prevention is possible
Answer: A

Q65. Which of the following statements is true regarding the use of else with loops?
A) The else block executes only if the loop terminates normally, not via break
B) The else block always executes regardless of how the loop exits
C) The else block executes only when a break is encountered
D) Python does not support else with loops
Answer: A

Q66. What is the effect of placing an else clause after a while loop?
A) It is executed if the while loop is never entered
B) It is executed only when the loop condition is initially False
C) It is executed after the loop completes normally without a break
D) It is executed before the loop starts
Answer: C
Q67. Which of the following can be iterated over directly in a for loop?
A) Dictionaries
B) Strings
C) Files
D) All of the above
Answer: D

Q68. What does the following code print?

for char in "Hi":


print(char)

A) H i
B) Hi
C) H
i
D) Error
Answer: C

Q69. How do you ensure that a block of code only executes if a certain condition is not met within a loop?
A) By using a nested if inside the loop
B) By using an else clause with the loop
C) By using the break statement
D) Both A and B
Answer: D

Q70. Which of the following best describes how a for loop works in Python?
A) It iterates over the indices of a sequence
B) It retrieves each element from an iterable one by one
C) It creates a copy of the sequence for iteration
D) It only works with list data types
Answer: B

Q71. Which control structure would you use to repeat a task until a user provides the correct input?
A) for loop
B) while loop
C) if statement
D) try-except
Answer: B

Q72. When might you use a nested loop structure?


A) When you need to iterate over a two-dimensional data structure
B) When a single loop is sufficient
C) When performing arithmetic operations
D) When calling a function
Answer: A

Q73. What is the purpose of using an else clause after a for loop when searching for a value?
A) To execute code if the value was found
B) To execute code if the value was not found
C) To always execute regardless of the search outcome
D) To repeat the loop
Answer: B

Q74. What does the following code print?


i=0
while i < 5:
if i == 3:
break
print(i, end=" ")
i += 1
else:
print("Complete")

A) 0 1 2 Complete
B) 0 1 2
C) 0 1 2 3 Complete
D) 0 1 2 3
Answer: B

Q75. In Python, which of the following is true regarding the execution of an if statement?
A) It must include an else clause
B) It checks the condition only once
C) The code block inside executes only if the condition is True
D) Both B and C
Answer: D

Q76. Which of the following is the correct way to write a multi-line conditional expression using a backslash for line continuation?
A)

if (a > 0 and \
b < 5):
print("Valid")

B)
if a > 0 and
b < 5:
print("Valid")

C)

if a > 0 and \
b < 5:
print("Valid")

D) Both A and C
Answer: D

Q77. Which of the following loop constructs can include an else clause?
A) Only for loops
B) Only while loops
C) Both for and while loops
D) Neither for nor while loops
Answer: C

Q78. What does the continue statement do when executed inside a nested loop?
A) It continues only the inner loop’s next iteration
B) It continues the outer loop’s iteration
C) It stops both loops
D) It restarts the entire nested loop structure
Answer: A

Q79. Which keyword would you use to exit a loop if a condition involving multiple variables is met?
A) break
B) exit
C) quit
D) end
Answer: A

Q80. What is the outcome of this code?

for i in range(1, 10):


if i % 3 == 0:
print(i)

A) 1 2 3 4 5 6 7 8 9
B) 3 6 9
C) 1 2 4 5 7 8
D) 3 6
Answer: B

Q81. Which construct is best for running a block of code a known number of times?
A) while loop
B) for loop
C) do-while loop
D) if-else construct
Answer: B

Q82. What does the following code print?

for i in range(3):
for j in range(2):
if j == 1:
continue
print(i, j)

A) 0 0; 0 1; 1 0; 1 1; 2 0; 2 1
B) 0 0; 1 0; 2 0
C) 0 1; 1 1; 2 1
D) 0 0; 0 1; 1 0; 1 1
Answer: B

Q83. Which statement about control flow in Python is false?


A) Indentation defines the scope of control flow blocks
B) The break statement terminates the loop
C) Every loop must have an else clause
D) The continue statement moves control to the next iteration
Answer: C

Q84. How do you write a one-line if statement that executes a function do_something() when variable x is positive?
A) if x > 0: do_something()
B) if x > 0 then do_something()
C) if (x > 0) do_something()
D) if x > 0 do_something()
Answer: A

Q85. What will the following code print?

x = 10
if x > 5: print("Big"), print("Number")
A) Big
Number
B) Big Number in one line
C) SyntaxError
D) 10
Answer: A

Q86. Which of the following is a valid Python construct for an infinite loop?
A) while True:
B) for i in range(0):
C) while 0:
D) for ;;
Answer: A

Q87. Which loop control keyword cannot be used to exit a nested loop from an inner block?
A) break
B) continue
C) pass
D) None – they all work for nested loops
Answer: B
(continue only skips the current iteration, it does not exit the loop.)

Q88. What does the following code produce?

i=0
while i < 3:
print(i)
i += 2
A) 0 1 2
B) 0 2
C) 0 2 4
D) 0 1
Answer: B

Q89. Which keyword would be used to check membership in a sequence as part of a control flow condition?
A) in
B) has
C) exists
D) contains
Answer: A

Q90. Which operator could be used within an if statement to test that two variables are not equal?
A) <>
B) !=
C) =!
D) not=
Answer: B

Q91. Which of the following is correct regarding the use of colons in control flow statements?
A) Colons are used to end the block
B) Colons indicate the start of the indented block
C) Colons are optional
D) Colons can replace indentation
Answer: B

Q92. What does this code print?


for i in range(4):
if i == 2:
pass
print(i)

A) 0 1 2 3
B) 0 1 3
C) 0 1 2
D) 2 3
Answer: A

Q93. Which control structure would be best to iterate over every character in a string?
A) while loop with a counter
B) for loop directly on the string
C) Both A and B
D) Neither A nor B
Answer: C

Q94. What is printed by the following code snippet?

i=0
while i < 3:
if i == 2:
break
print(i, end=" ")
i += 1
print("Done")

A) 0 1 Done
B) 0 1 2 Done
C) 0 1
D) Done
Answer: A

Q95. Which loop type is often used when the number of iterations is unknown until runtime?
A) for loop
B) while loop
C) do-while loop
D) if loop
Answer: B

Q96. What does the following code print?

for i in range(3):
if i == 0:
continue
print(i)
else:
print("Loop ended")

A) 0 1 2 Loop ended
B) 1 2 Loop ended
C) 1 2
D) 0 1 2
Answer: B

Q97. Which statement is true about the control flow in Python functions?
A) Functions have no control flow
B) Functions can contain their own control flow, like conditionals and loops
C) Control flow in functions is defined by return statements only
D) Control flow is not allowed inside functions
Answer: B

Q98. Which of the following is true about conditional expressions (ternary operators) in Python?
A) They can only be used in assignment statements
B) They can be nested
C) They always require parentheses
D) They cannot be used in function calls
Answer: B

Q99. What is one benefit of using f-strings (formatted string literals) within control flow print statements?
A) They enforce strict type checking
B) They allow in-line evaluation and formatting of expressions
C) They automatically loop through data
D) They replace the need for if statements
Answer: B

Q100. Which of the following best summarizes Python’s control flow constructs?
A) They only support sequential execution
B) They allow conditional execution, repetition, and branch handling to control program order
C) They are only used for error handling
D) They are specific only to loops and ignore conditions
Answer: B

You might also like