wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Programming Quiz

Total questions: 40

Worksheet time: 35mins

Name
Class
Date
1.

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

a)

B

b)

A

c)

Error

d)

None of the above

2.

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

a)

if x > y then:

b)

if (x > y)

c)

if x > y:

d)

if x > y {

3.

x = 10 y = 20 if x > y: print("X is greater") else: print("Y is greater")

4 lines
4.

Choose the correct code to complete the following program: x = 15 if x % 2 == 0: print("Even") else: __________

a)

print("Odd")

b)

print("Even")

c)

print("None")

d)

print(x)

5.

Which statement is used to exit a loop?

a)

exit

b)

break

c)

continue

d)

stop

6.

Which data structure is commonly used to mimic a switch statement in Python?

a)

List

b)

Dictionary

c)

Tuple

d)

Set

7.

def switch_demo(x): switcher = { 1: "One", 2: "Two", 3: "Three" } return switcher.get(x, "Invalid") print(switch_demo(2))

4 lines
8.

Complete the code to simulate a switch statement: def day_name(day): days = { 1: "Monday", 2: "Tuesday", 3: ___________, } return days.get(day, "Invalid day")

a)

Wednesday

b)

Friday

c)

Sunday

d)

None

9.

Which statement is used to skip the current iteration and continue with the next iteration?

a)

break

b)

continue

c)

pass

d)

return

10.

for i in range(5): if i == 3: continue print(i, end=" ")

4 lines
11.

Which of the following correctly represents a nested if statement?

a)

if x > 0: if y > 0: print("Both positive")

b)

if x > 0: print("Positive") else: print("Negative")

c)

if x > 0: elif y > 0: print("Positive")

d)

if x > 0: print("Positive") if y > 0: print("Y is positive")

12.

x = 10 y = 5 if x > 0: if y > 0: print("Both positive") else: print("Y is non-positive") else: print("X is non-positive")

4 lines
13.

Which keyword is used to handle an exception in Python?

a)

throw

b)

except

c)

catch

d)

error

14.

What will be the output of the following code? try: print(10 / 0) except ZeroDivisionError: print("Cannot divide by zero")

a)

Error

b)

0

c)

Cannot divide by zero

d)

None

15.

try: num = int(input("Enter a number: ")) except __________: print("Invalid input")

a)

ValueError

b)

TypeError

c)

ZeroDivisionError

d)

IndexError

16.

Which of the following is the correct syntax for a while loop in Python?

a)

while x < 5 {

b)

while (x < 5)

c)

while x < 5:

d)

while x < 5 do:

17.

i = 0 while i < 3: print(i) i += 1

4 lines
18.

Which statement is used to terminate a loop prematurely?

a)

stop

b)

end

c)

break

d)

exit

19.

What will be the output of the following code? for i in range(3): for j in range(2): print(i, j)

4 lines
20.

Complete the loop to print even numbers from 0 to 10: for i in range(0, 11): if i % 2 == 0: __________

a)

print(i)

b)

continue

c)

break

d)

pass

21.

Complete the code to handle an exception for dividing by zero: try: result = 10 / x except __________: print("Division by zero is not allowed.")

a)

TypeError

b)

ValueError

c)

ZeroDivisionError

d)

IndexError

22.

Choose the correct function to check if a value is present in a list: numbers = [1, 2, 3, 4, 5] if __________(3, numbers): print("Found")

a)

contains()

b)

in

c)

find()

d)

has()

23.

Select the correct line of code to continue to the next iteration in a loop: for i in range(5): if i == 3: __________ print(i)

a)

break

b)

pass

c)

continue

d)

return

24.

Which function is missing to get input from the user? name = __________("Enter your name: ") print("Hello, " + name)

a)

input()

b)

print()

c)

len()

d)

str()

25.

Complete the nested if statement: x = 10 y = 5 if x > y: if x > 0: print("X is positive and greater than Y") __________ print("X is non-positive")

a)

elif

b)

else

c)

if

d)

pass

26.

Fill in the missing line to handle any kind of exception: try: result = 10 / 0 except __________: print("An error occurred.")

a)

Exception

b)

BaseException

c)

ZeroDivisionError

d)

Error

27.

Choose the correct function to get the length of a string: name = "Python" length = __________(name) print(length)

a)

size()

b)

count()

c)

length()

d)

len()

28.

Which keyword is needed to define a function in Python? __________ greet(): print("Hello, World!")

a)

function

b)

define

c)

def

d)

fun

29.

Complete the while loop condition to print numbers from 1 to 5: i = 1 while __________: print(i) i += 1

a)

i < 5

b)

i <= 5

c)

i > 5

d)

i >= 5

30.

Select the correct code to catch multiple exceptions: try: num = int("abc") except (ValueError, __________): print("An error occurred.")

a)

TypeError

b)

ZeroDivisionError

c)

IndexError

d)

KeyError

31.

Complete the loop to print numbers from 0 to 4: for i in __________: print(i)

a)

range(5)

b)

range(0, 5)

c)

range(0, 4)

d)

range(1, 5)

32.

Which statement is required to exit a function? def check_number(x): if x < 0: __________ print("Positive number")

a)

continue

b)

break

c)

return

d)

exit

33.

Select the correct function to convert a string to an integer: num = __________("10") print(num + 5)

a)

float()

b)

str()

c)

int()

d)

eval()

34.

Choose the correct code to handle an exception and execute final block: try: x = 10 / 2 except ZeroDivisionError: print("Cannot divide by zero") finally: __________

a)

exit()

b)

pass

c)

print("Execution completed")

d)

return

35.

Which line completes the program to check for an even number? num = 8 if num % 2 == 0: print("Even") else: __________

a)

print("Odd")

b)

print("Even")

c)

break

d)

pass

36.

Choose the correct statement to define a dictionary: data = __________ data['name'] = 'Alice' print(data)

a)

dict()

b)

list()

c)

set()

d)

tuple()

37.

Complete the switch-like statement using a dictionary: def get_day(n): days = { 1: "Monday", 2: "Tuesday", 3: "Wednesday" } return days.__________(n, "Invalid day")

a)

get()

b)

find()

c)

index()

d)

search()

38.

Which function is used to iterate over both key and value of a dictionary? data = {'a': 1, 'b': 2} for key, value in data.__________(): print(key, value)

a)

keys

b)

values

c)

items

d)

pairs

39.

Select the correct function to remove the last element from a list: numbers = [1, 2, 3, 4, 5] numbers.__________() print(numbers)

a)

remove()

b)

delete()

c)

pop()

d)

discard()

40.

Complete the nested loop to print a pattern: for i in range(3): for j in range(3): __________ print()

a)

print("*", end=" ")

b)

print("*")

c)

print("*", end="")

d)

print("*", end="\n")