wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python Conditional Statements and Loop Control MCQs

Total questions: 25

Worksheet time: 13mins

Name
Class
Date
1.

What is the output of the following code? x = 5 if x > 3: print("A") if x > 4: print("B") else: print("C")

a)

A

b)

A B

c)

B

d)

A C

2.

Which of the following statements is TRUE about Python if?

a)

Indentation is mandatory

b)

Semicolon is mandatory

c)

Curly braces are mandatory

d)

Parentheses are mandatory

3.

What will be printed? if 0: print("True") else: print("False")

a)

True

b)

False

c)

Error

d)

None

4.

What is the output? for i in range(1, 5, 2): print(i, end=" ")

a)

1 2 3 4

b)

1 3

c)

1 3 5

d)

Error

5.

How many times will the loop execute? i = 1 while i <= 5: i += 2

a)

Infinite

b)

4

c)

3

d)

2

6.

What is the output? for i in range(3): for j in range(i): print("*", end="") print()

a)

(newline)* ** ***

b)

(newline)* **

c)

(newline)* ** (newline)

d)

* ** ***

7.

What will be printed? for i in range(5): if i == 3: break print(i, end=" ")

a)

0 1 2

b)

0 1 2 3

c)

Error

d)

1 2 3

8.

What is the output? for i in range(5): if i == 3: continue print(i, end=" ")

a)

1 2 4

b)

0 1 2 3 4

c)

0 1 2

d)

0 1 2 4

9.

pass statement is used to:

a)

Exit loop

b)

Skip iteration

c)

Do nothing

d)

Terminate program

10.

What is the output? try: x = 10 / 0 except ZeroDivisionError: print("Error")

a)

Exception

b)

Error

c)

Program terminates

d)

0

11.

Which block is executed irrespective of exception?

a)

except

b)

else

c)

try

d)

finally

12.

What will be printed? try: print(1) raise ValueError except ValueError: print(2) finally: print(3)

a)

Error

b)

1 2

c)

1 2 3

d)

1 3

13.

Which of the following raises an exception?

a)

int("10")

b)

int("A")

c)

float("3.5")

d)

str(10)

14.

What is the output? try: print("A") except: print("B") else: print("C")

a)

A

b)

A C

c)

B

d)

Error

15.

Infinite loop is created using:

a)

while True:

b)

for i in range(0)

c)

while 1 == 0:

d)

for i in []:

16.

What will be printed? for i in range(3): print(i) if i == 1: break print(i + 1)

a)

0 1 1

b)

0 1 2

c)

0 1 1 2

d)

0 1

17.

What is the output of the following code? x = 10 if x < 5: print("Low") elif x < 15: print("Medium") else: print("High")

a)

Low

b)

Medium

c)

Error

d)

High

18.

What will be printed? try: print("Start") raise Exception except Exception: print("Caught") else: print("No Exception")

a)

Start Caught

b)

Caught

c)

Start

d)

Start Caught No Exception

19.

What will be printed? for i in range(4): if i == 2: continue print(i, end=" ")

a)

0 1 3

b)

1 2 3

c)

0 1 2 3

d)

0 1 2

20.

What is the output of the following code? x = 7 if x < 10: print("Less than 10") else: print("10 or more")

a)

None

b)

Error

c)

10 or more

d)

Less than 10

21.

Which of the following will cause a syntax error?

a)

print("Hello")

b)

if x = 5:

c)

for i in range(5):

d)

while True:

22.

What will be printed? for i in range(4): if i == 1: continue print(i, end=" ")

a)

0 1 2 3

b)

1 2 3

c)

0 2 3

d)

0 1 3

23.

Which of the following is NOT a valid way to define a function in Python?

a)

lambda x: x + 1

b)

def my_function():

c)

function my_function():

d)

def my_function(param):

24.

What will be printed? for i in range(3): for j in range(2): if j == 1: break print(i, j)

a)

0 0 1 0 2 0

b)

0 0 1 0 2 1

c)

0 0 1 0 2 0 2 1

d)

0 0 1 0 2 0 2 1

25.

Which of the following will NOT raise an exception?

a)

int("10")

b)

list(10)

c)

int("A")

d)

float("3.5")