Control Structures - boolean operators, conditional statements, loops for-while, break and continue - Question Bank

1. Consider the code: `count = 5 while count > 0: print(count) count -= 2`. What is the output?
A) 5 3 1
B) 5 4 3 2 1
C) 5 3 1 -1
D) 4 2 0
2. What is the primary purpose of `elif`?
A) To provide an alternative condition check after an `if` statement.
B) To execute code when the `if` condition is false.
C) To create nested loops.
D) To define a function.
3. Which expression evaluates to `False`?
A) 1 == 1
B) 0 != 1
C) True and False
D) True or False
4. What does the `else` block associated with a `for` loop do?
A) It executes if the loop terminates due to a `break` statement.
B) It executes if the loop completes all its iterations without encountering a `break`.
C) It executes on every iteration.
D) It is never executed.
5. Consider the code: `for i in range(4): if i % 2 != 0: print(i)`. What is the output?
A) 0 2
B) 1 3
C) 0 1 2 3
D) 2 4
6. Which keyword is used to create a loop that continues as long as a specified condition remains true?
A) for
B) if
C) while
D) loop
7. What is the result of `True or (False and True)`?
A) True
B) False
C) None
D) Error
8. Which boolean operator is used for checking inequality?
A) ==
B) !=
C) >=
D) <=
9. Consider the code: `while 5 > 3: print('Hello') break`. What is the output?
A) Hello
B) Hello Hello
C) Infinite loop
D) Nothing
10. What is the difference between `for i in range(3)` and `for i in [0, 1, 2]`?
A) The `range` version is more memory efficient for large sequences.
B) The list version is faster.
C) They produce different outputs.
D) There is no significant difference in this case.
11. Which keyword is used to define a block of code that is executed if the preceding `if` or `elif` conditions are all false?
A) continue
B) else
C) break
D) finally
12. What does `x = 10; y = 5; print(x > y or x < y)` output?
A) True
B) False
C) None
D) Error
13. Consider the code: `my_list = [1, 2, 3] for item in my_list: if item == 2: continue print(item)`. What is the output?
A) 1 2 3
B) 1 3
C) 2 3
D) 1 2
14. Which loop is guaranteed to execute at least once, even if the condition is false?
A) for loop
B) while loop
C) do-while loop (not native in Python, simulated)
D) None of the above
15. What is the purpose of indentation in Python's control structures?
A) It's purely for aesthetic purposes.
B) It defines the scope and blocks of code.
C) It indicates the end of a statement.
D) It is optional and ignored by the interpreter.
16. Consider the code: `i = 0 while True: print('Looping') if i == 2: break i += 1`. What is the output?
A) Looping
B) Looping Looping
C) Looping Looping Looping
D) Error
17. Which statement allows executing code if an `if` condition is true, and another block of code if it's false?
A) elif
B) while
C) for
D) else
18. What does the expression `5 > 3 and 10 < 20` evaluate to?
A) True
B) False
C) None
D) Error
19. Consider the code: `for i in range(1, 5): print(i)`. What is the output?
A) 1 2 3 4 5
B) 1 2 3 4
C) 0 1 2 3 4
D) 2 3 4 5
20. Which boolean operator has the highest precedence?
A) and
B) or
C) not
D) ==
21. What is the primary use of the `range()` function with a `for` loop?
A) To create a list of strings.
B) To generate a sequence of numbers.
C) To define a condition for the loop.
D) To exit the loop early.
22. Consider the code: `num = 10 if num % 2 == 0: print('Even') else: print('Odd')`. What will be printed?
A) Odd
B) Even
C) Even Odd
D) Nothing
23. Which control structure is used for error handling in Python?
A) if-elif-else
B) for-while
C) try-except
D) break-continue
24. What is the result of `not True`?
A) True
B) False
C) None
D) Error
25. Which statement is used to create a loop that runs for a specific number of times using a sequence?
A) while
B) if
C) for
D) goto
26. 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) The program terminates.
D) An error occurs.
27. Which of the following correctly represents a boolean expression in Python?
A) x = 5 > 3
B) x = 'hello' == 'world'
C) x = True and False
D) All of the above
28. Consider the code: `for i in 'Python': if i == 'h': continue print(i, end='')`. What is the output?
A) Python
B) Pyton
C) Pytn
D) Error
29. What is the purpose of `pass` statement in Python?
A) To exit a loop.
B) To skip an iteration.
C) To do nothing, used as a placeholder.
D) To define a default value.
30. In a nested conditional statement, the inner `if` belongs to which block?
A) The `else` block of the outer `if`.
B) The `elif` block of the outer `if`.
C) The `if` block or `elif` block of the outer `if`.
D) It cannot be nested.
31. What is the result of `True or False`?
A) True
B) False
C) None
D) Error
32. Which operator returns `True` if at least one of the operands is true, and `False` otherwise?
A) and
B) not
C) or
D) is
33. Consider the code: `for i in range(5): if i == 3: break print(i)`. What is the output?
A) 0 1 2
B) 0 1 2 3
C) 0 1 2 3 4
D) Nothing
34. Which of the following is an example of an iterable in Python suitable for a `for` loop?
A) An integer variable
B) A boolean value
C) A list
D) A function definition
35. What does the `not` operator do in Python?
A) Returns `True` if the operand is false, and `False` if the operand is true.
B) Returns `True` if at least one operand is true.
C) Returns `True` if both operands are true.
D) Returns `None` if the operand is false.
36. Which statement is used to execute a block of code when an `if` condition evaluates to `False` and there is no `elif` condition met?
A) continue
B) break
C) else
D) pass
37. Consider the code: `count = 0 while count < 3: print(count) count += 1`. What is the output?
A) 0 1 2
B) 1 2 3
C) 0 1 2 3
D) Loop infinitely
38. What is the primary difference between `if` and `elif`?
A) `if` checks a condition, `elif` executes a block.
B) `if` is for true conditions, `elif` is for false conditions.
C) `if` starts a chain of conditions, `elif` checks subsequent conditions only if prior ones were false.
D) There is no functional difference.
39. Which keyword allows checking multiple conditions sequentially after an initial `if` statement?
A) else
B) elif
C) continue
D) break
40. What is the result of the expression `True and False`?
A) True
B) False
C) None
D) Error
41. Which operator returns `True` if both operands are true, and `False` otherwise?
A) or
B) not
C) and
D) ==
42. The `for` loop in Python iterates over items in which of the following?
A) Only integers.
B) Only strings.
C) Any iterable sequence (like lists, tuples, strings, ranges).
D) Only boolean values.
43. Consider the following code: `x = 5 if x > 10: print('A') else: print('B')`. What will be printed?
A) A
B) B
C) A B
D) Nothing
44. Which loop is typically used when the number of iterations is known beforehand?
A) while loop
B) for loop
C) do-while loop
D) recursive loop
45. What is the purpose of the `continue` statement in a Python loop?
A) To exit the loop immediately.
B) To execute a specific block of code once.
C) To skip the remainder of the current loop iteration and proceed to the next.
D) To define a condition for the loop to continue.
46. Which keyword is used to define a block of code that executes only if a preceding `if` condition is false?
A) elif
B) else
C) try
D) except
47. What does the `break` statement do within a loop?
A) It terminates the loop entirely.
B) It skips the rest of the current iteration and continues with the next.
C) It restarts the loop from the beginning.
D) It exits the current block of code.
48. Which of the following is NOT a boolean operator in Python?
A) and
B) or
C) not
D) xor
49. What is the primary function of a `while` loop in Python?
A) To execute a block of code a fixed number of times.
B) To iterate over a sequence of items.
C) To execute a block of code as long as a condition is true.
D) To skip the current iteration of a loop.
50. Which keyword is used to start a conditional statement in Python?
A) loop
B) if
C) while
D) for