Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Quiz day 3 : Conditional Statements and Loops

Total questions: 10

Worksheet time: 5mins

Name
Class
Date
1.

What is the syntax for an if-else statement in Python?

a)

if condition: # code to be executed if the condition is false else: # code to be executed if the condition is true

b)

if condition: # code to be executed if the condition is true else: # code to be executed if the condition is false

c)

if condition: # code to be executed if the condition is true else: # code to be executed if the condition is true

d)

if condition: # code to be executed if the condition is true else: # code to be executed if the condition is false

2.

How do you write a nested if statement in Python?

a)

By adding a for loop within the if statement block

b)

By using a switch statement instead

c)

By including another if statement within the block of code for the first if statement.

d)

By removing the first if statement

3.

What is the purpose of the 'elif' keyword in Python?

a)

To end the 'if' statement

b)

To create a loop

c)

To define a function

d)

To add another condition to an 'if' statement

4.

What is the output of the following code? x = 5 if x > 3: print('x is greater than 3') else: print('x is less than or equal to 3')

a)

x is equal to 3

b)

x is greater than or equal to 3

c)

x is greater than 3

d)

x is less than 3

5.

What is the output of the following code? for i in range(5): print(i)

a)

15

b)

10

c)

5

d)

0 1 2 3 4

6.

What is the purpose of the 'break' keyword in a loop?

a)

To double the value of the loop variable

b)

To print a message to the console

c)

To continue to the next iteration of the loop

d)

To exit the loop immediately

7.

What is the purpose of the 'continue' keyword in a loop?

a)

To end the loop immediately

b)

To print the current iteration value

c)

To restart the loop from the beginning

d)

To skip the current iteration of the loop and continue with the next iteration.

8.

What is the output of the following code? for i in range(1, 6, 2): print(i)

a)

2

b)

1 3

c)

6

d)

4

9.

How do you write a nested for loop in Python?

a)

Use the syntax: for x in range(3): for y in range(3): print(x, y)

b)

Nested for loops are not supported in Python

c)

You can write a nested for loop in Python by using the syntax: for x in range(3): for y in range(3): print(x, y)

d)

You can't write a nested for loop in Python

10.

What is the output of the following code? x = 2 if x > 5: print('x is greater than 5') else: print('x is less than or equal to 5')

a)

x is equal to 5

b)

x is greater than 5

c)

x is less than 5

d)

x is less than or equal to 5