Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Quiz: Decision Statements & Loops

Total questions: 30

Worksheet time: 15mins

Name
Class
Date
1.

Which of the following is the correct syntax of an `if` statement in Python?

a)

if (x > 5)

b)

if x > 5:

c)

if x > 5 { }

d)

if x > 5 then:

2.

Which keyword is used to check multiple conditions in Python?

a)

elseif

b)

elif

c)

else if

d)

ifelse

3.

What is the output of the following code? x = 7 if x % 2 == 0: print("Even") else: print("Odd")

a)

Even

b)

Odd

c)

7

d)

Error

4.

Which of the following is NOT a valid relational operator in Python?

a)

!=

b)

<>

c)

>=

d)

<

5.

Consider this code: x = 10 if x > 5: print("A") if x > 8: print("B") What will be the output?

a)

A

b)

B

c)

A B

d)

None

6.

Which statement is true about the `else` block in Python?

a)

It must always follow `if`

b)

It must always follow `elif`

c)

It can follow `if` or `elif`

d)

It is optional

7.

What is the output? a = 3 if a == 3: print("Yes") else: print("No")

a)

Yes

b)

No

c)

3

d)

Error

8.

Which of the following is a valid nested if?

a)

if x > 0: if x % 2 == 0: print("Even Positive")

b)

if x > 0: print("Positive")

c)

if x > 0 then: if x % 2 == 0: print("Even Positive")

d)

Both a and b

9.

Which of these operators is used for logical AND in Python?

a)

&

b)

&&

c)

and

d)

AND

10.

What will be the output? x = 5 if x > 5: print("A") elif x == 5: print("B") else: print("C")

a)

A

b)

B

c)

C

d)

None

11.

Which of the following statements is incorrect?

a)

if can exist without else

b)

else can exist without if

c)

elif must come after if

d)

else is optional

12.

What will be the output? x = 10 y = 20 if x > y: print("X") else: print("Y")

a)

X

b)

Y

c)

XY

d)

Error

13.

Which of the following is the correct way to check if num is not equal to 100?

a)

if num != 100:

b)

if num <> 100:

c)

if num NOT 100:

d)

if num ^= 100:

14.

What is the output? x = -1 if x: print("True") else: print("False")

a)

True

b)

False

c)

-1

d)

Error

15.

Which of the following evaluates to False in Python?

a)

if 0:

b)

if "Hello":

c)

if [1, 2]:

d)

if 10:

16.

Which function is most commonly used with a for loop to generate a sequence of numbers?

a)

list()

b)

tuple()

c)

range()

d)

set()

17.

What will be the output of this code?

n = 5

while n > 0:

if n == 3:

break

print(n)

n -= 1

a)

5

4

b)

5

4

3

2

1

c)

5

4

2

1

d)

Infinite loop

18.

What is the output? count = 0 while count < 3: print(count) count += 1

a)

0 1

b)

0 1 2

c)

1 2 3

d)

Infinite loop

19.

What will be the output of the following code?

x = 0

for i in range(1, 4):

x += i

print(x)

a)

3

b)

6

c)

10

d)

Error

20.

What will be the output?

for i in range(5):

if i == 3:

break

print(i)

a)

0

1

2

b)

0

1

2

3

c)

0

1

2

3

4

d)

3

21.

What will be the output of the following code?

x = 1

while x < 10:

print(x)

x *= 2

a)

1

2

3

4

5

6

7

8

9

b)

1

2

4

8

c)

1

3

9

d)

Infinite loop

22.

Which of the following loops will execute at least once?

a)

while loop

b)

for loop

c)

Both a and b

d)

None of the above

23.

What will be the output? for i in range(1, 10, 3): print(i)

a)

1

2

3

b)

1

4

7

c)

1

3

6

9

d)

1

5

9

24.

Which of the following is correct?

a)

while (x < 10):

b)

while x < 10:

c)

while x < 10 {}

d)

while x < 10 do:

25.

What is the output? x = 0 while x < 5: print(x) x += 2

a)

0

1

2

3

4

b)

0

2

4

c)

1

3

5

d)

Infinite loop

26.

Which loop is best suited when the number of iterations is known?

a)

for

b)

while

c)

do-while

d)

None

27.

What is the output? for i in "PY": print(i)

a)

P Y

b)

P

Y

c)

Error

d)

None

28.

What will happen? while False: print("Hello")

a)

Hello

b)

No output

c)

Infinite loop

d)

Error

29.

Which of the following will create an infinite loop?

a)

while 1: print("Hi")

b)

while True: print("Hi")

c)

for i in range(1, 10, -1): print(i)

d)

All of the above

30.

What is the output? for i in range(2, 2): print(i)

a)

2

b)

0 1

c)

No output

d)

Error