WorksheetsPython Quiz: Decision Statements & Loops
Total questions: 30
Worksheet time: 15mins
Which of the following is the correct syntax of an `if` statement in Python?
if (x > 5)
if x > 5:
if x > 5 { }
if x > 5 then:
Which keyword is used to check multiple conditions in Python?
elseif
elif
else if
ifelse
What is the output of the following code? x = 7 if x % 2 == 0: print("Even") else: print("Odd")
Even
Odd
7
Error
Which of the following is NOT a valid relational operator in Python?
!=
<>
>=
<
Consider this code: x = 10 if x > 5: print("A") if x > 8: print("B") What will be the output?
A
B
A B
None
Which statement is true about the `else` block in Python?
It must always follow `if`
It must always follow `elif`
It can follow `if` or `elif`
It is optional
What is the output? a = 3 if a == 3: print("Yes") else: print("No")
Yes
No
3
Error
Which of the following is a valid nested if?
if x > 0: if x % 2 == 0: print("Even Positive")
if x > 0: print("Positive")
if x > 0 then: if x % 2 == 0: print("Even Positive")
Both a and b
Which of these operators is used for logical AND in Python?
&
&&
and
AND
What will be the output? x = 5 if x > 5: print("A") elif x == 5: print("B") else: print("C")
A
B
C
None
Which of the following statements is incorrect?
if can exist without else
else can exist without if
elif must come after if
else is optional
What will be the output? x = 10 y = 20 if x > y: print("X") else: print("Y")
X
Y
XY
Error
Which of the following is the correct way to check if num is not equal to 100?
if num != 100:
if num <> 100:
if num NOT 100:
if num ^= 100:
What is the output? x = -1 if x: print("True") else: print("False")
True
False
-1
Error
Which of the following evaluates to False in Python?
if 0:
if "Hello":
if [1, 2]:
if 10:
Which function is most commonly used with a for loop to generate a sequence of numbers?
list()
tuple()
range()
set()
What will be the output of this code?
n = 5
while n > 0:
if n == 3:
break
print(n)
n -= 1
5
4
5
4
3
2
1
5
4
2
1
Infinite loop
What is the output? count = 0 while count < 3: print(count) count += 1
0 1
0 1 2
1 2 3
Infinite loop
What will be the output of the following code?
x = 0
for i in range(1, 4):
x += i
print(x)
3
6
10
Error
What will be the output?
for i in range(5):
if i == 3:
break
print(i)
0
1
2
0
1
2
3
0
1
2
3
4
3
What will be the output of the following code?
x = 1
while x < 10:
print(x)
x *= 2
1
2
3
4
5
6
7
8
9
1
2
4
8
1
3
9
Infinite loop
Which of the following loops will execute at least once?
while loop
for loop
Both a and b
None of the above
What will be the output? for i in range(1, 10, 3): print(i)
1
2
3
1
4
7
1
3
6
9
1
5
9
Which of the following is correct?
while (x < 10):
while x < 10:
while x < 10 {}
while x < 10 do:
What is the output? x = 0 while x < 5: print(x) x += 2
0
1
2
3
4
0
2
4
1
3
5
Infinite loop
Which loop is best suited when the number of iterations is known?
for
while
do-while
None
What is the output? for i in "PY": print(i)
P Y
P
Y
Error
None
What will happen? while False: print("Hello")
Hello
No output
Infinite loop
Error
Which of the following will create an infinite loop?
while 1: print("Hi")
while True: print("Hi")
for i in range(1, 10, -1): print(i)
All of the above
What is the output? for i in range(2, 2): print(i)
2
0 1
No output
Error
