Control Structures - boolean operators, conditional statements, loops for-while, break and continue - One Line Questions
1.
What is the primary difference between `if` and `elif`? —
`if` starts a chain of conditions, `elif` checks subsequent conditions only if prior ones were false.
2.
Which boolean operator is used for checking inequality? —
!=
3.
Consider the code: `count = 0
while count < 3:
print(count)
count += 1`. What is the output? —
0
1
2
4.
Consider the code: `for i in range(5):
if i == 3:
break
print(i)`. What is the output? —
0
1
2
5.
Consider the code: `for i in range(4):
if i % 2 != 0:
print(i)`. What is the output? —
1
3
6.
Consider the code: `for i in range(1, 5):
print(i)`. What is the output? —
1
2
3
4
7.
Consider the code: `my_list = [1, 2, 3]
for item in my_list:
if item == 2:
continue
print(item)`. What is the output? —
1
3
8.
Which expression evaluates to `False`? —
True and False
9.
Consider the code: `count = 5
while count > 0:
print(count)
count -= 2`. What is the output? —
5
3
1
10.
Consider the following code: `x = 5
if x > 10:
print('A')
else:
print('B')`. What will be printed? —
B
11.
Which of the following is an example of an iterable in Python suitable for a `for` loop? —
A list
12.
Which of the following is NOT a boolean operator in Python? —
xor
13.
Which operator returns `True` if at least one of the operands is true, and `False` otherwise? —
or
14.
Which boolean operator has the highest precedence? —
not
15.
Which statement is used to execute a block of code when an `if` condition evaluates to `False` and there is no `elif` condition met? —
else
16.
Which keyword is used to define a block of code that is executed if the preceding `if` or `elif` conditions are all false? —
else
17.
Which keyword is used to define a block of code that executes only if a preceding `if` condition is false? —
else
18.
Which statement allows executing code if an `if` condition is true, and another block of code if it's false? —
else
19.
Which keyword allows checking multiple conditions sequentially after an initial `if` statement? —
elif
20.
Which keyword is used to create a loop that continues as long as a specified condition remains true? —
while
21.
Which loop is guaranteed to execute at least once, even if the condition is false? —
do-while loop (not native in Python, simulated)
22.
Consider the code: `while 5 > 3:
print('Hello')
break`. What is the output? —
Hello
23.
Which control structure is used for error handling in Python? —
try-except
24.
What does the `else` block associated with a `for` loop do? —
It executes if the loop completes all its iterations without encountering a `break`.
25.
What does the `break` statement do within a loop? —
It terminates the loop entirely.
26.
What is the purpose of indentation in Python's control structures? —
It defines the scope and blocks of code.
27.
Which keyword is used to start a conditional statement in Python? —
if
28.
Consider the code: `i = 0
while True:
print('Looping')
if i == 2:
break
i += 1`. What is the output? —
Looping
Looping
Looping
29.
Consider the code: `num = 10
if num % 2 == 0:
print('Even')
else:
print('Odd')`. What will be printed? —
Even
30.
The `for` loop in Python iterates over items in which of the following? —
Any iterable sequence (like lists, tuples, strings, ranges).
31.
Which operator returns `True` if both operands are true, and `False` otherwise? —
and
32.
Consider the code: `for i in 'Python':
if i == 'h':
continue
print(i, end='')`. What is the output? —
Pyton
33.
What does the `not` operator do in Python? —
Returns `True` if the operand is false, and `False` if the operand is true.
34.
In a nested conditional statement, the inner `if` belongs to which block? —
The `if` block or `elif` block of the outer `if`.
35.
What is the difference between `for i in range(3)` and `for i in [0, 1, 2]`? —
The `range` version is more memory efficient for large sequences.
36.
What happens if the condition in a `while` loop is initially `False`? —
The loop body never executes.
37.
What is the primary use of the `range()` function with a `for` loop? —
To generate a sequence of numbers.
38.
What is the primary function of a `while` loop in Python? —
To execute a block of code as long as a condition is true.
39.
What is the purpose of `pass` statement in Python? —
To do nothing, used as a placeholder.
40.
What is the purpose of the `continue` statement in a Python loop? —
To skip the remainder of the current loop iteration and proceed to the next.
41.
What is the primary purpose of `elif`? —
To provide an alternative condition check after an `if` statement.
42.
What is the result of the expression `True and False`? —
False
43.
What is the result of `True or False`? —
True
44.
What is the result of `not True`? —
False
45.
What does the expression `5 > 3 and 10 < 20` evaluate to? —
True
46.
What does `x = 10; y = 5; print(x > y or x < y)` output? —
True
47.
What is the result of `True or (False and True)`? —
True
48.
Which statement is used to create a loop that runs for a specific number of times using a sequence? —
for
49.
Which loop is typically used when the number of iterations is known beforehand? —
for loop
50.
Which of the following correctly represents a boolean expression in Python? —
All of the above