Font size
WorksheetsPython Programming Quiz
Total questions: 40
Worksheet time: 35mins
What is the output of the following code snippet? x = 5 if x > 3: print("A") else: print("B")
B
A
Error
None of the above
Which of the following is a correct syntax for an if statement in Python?
if x > y then:
if (x > y)
if x > y:
if x > y {
x = 10 y = 20 if x > y: print("X is greater") else: print("Y is greater")
Choose the correct code to complete the following program: x = 15 if x % 2 == 0: print("Even") else: __________
print("Odd")
print("Even")
print("None")
print(x)
Which statement is used to exit a loop?
exit
break
continue
stop
Which data structure is commonly used to mimic a switch statement in Python?
List
Dictionary
Tuple
Set
def switch_demo(x): switcher = { 1: "One", 2: "Two", 3: "Three" } return switcher.get(x, "Invalid") print(switch_demo(2))
Complete the code to simulate a switch statement: def day_name(day): days = { 1: "Monday", 2: "Tuesday", 3: ___________, } return days.get(day, "Invalid day")
Wednesday
Friday
Sunday
None
Which statement is used to skip the current iteration and continue with the next iteration?
break
continue
pass
return
for i in range(5): if i == 3: continue print(i, end=" ")
Which of the following correctly represents a nested if statement?
if x > 0: if y > 0: print("Both positive")
if x > 0: print("Positive") else: print("Negative")
if x > 0: elif y > 0: print("Positive")
if x > 0: print("Positive") if y > 0: print("Y is positive")
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")
Which keyword is used to handle an exception in Python?
throw
except
catch
error
What will be the output of the following code? try: print(10 / 0) except ZeroDivisionError: print("Cannot divide by zero")
Error
0
Cannot divide by zero
None
try: num = int(input("Enter a number: ")) except __________: print("Invalid input")
ValueError
TypeError
ZeroDivisionError
IndexError
Which of the following is the correct syntax for a while loop in Python?
while x < 5 {
while (x < 5)
while x < 5:
while x < 5 do:
i = 0 while i < 3: print(i) i += 1
Which statement is used to terminate a loop prematurely?
stop
end
break
exit
What will be the output of the following code? for i in range(3): for j in range(2): print(i, j)
Complete the loop to print even numbers from 0 to 10: for i in range(0, 11): if i % 2 == 0: __________
print(i)
continue
break
pass
Complete the code to handle an exception for dividing by zero: try: result = 10 / x except __________: print("Division by zero is not allowed.")
TypeError
ValueError
ZeroDivisionError
IndexError
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")
contains()
in
find()
has()
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)
break
pass
continue
return
Which function is missing to get input from the user? name = __________("Enter your name: ") print("Hello, " + name)
input()
print()
len()
str()
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")
elif
else
if
pass
Fill in the missing line to handle any kind of exception: try: result = 10 / 0 except __________: print("An error occurred.")
Exception
BaseException
ZeroDivisionError
Error
Choose the correct function to get the length of a string: name = "Python" length = __________(name) print(length)
size()
count()
length()
len()
Which keyword is needed to define a function in Python? __________ greet(): print("Hello, World!")
function
define
def
fun
Complete the while loop condition to print numbers from 1 to 5: i = 1 while __________: print(i) i += 1
i < 5
i <= 5
i > 5
i >= 5
Select the correct code to catch multiple exceptions: try: num = int("abc") except (ValueError, __________): print("An error occurred.")
TypeError
ZeroDivisionError
IndexError
KeyError
Complete the loop to print numbers from 0 to 4: for i in __________: print(i)
range(5)
range(0, 5)
range(0, 4)
range(1, 5)
Which statement is required to exit a function? def check_number(x): if x < 0: __________ print("Positive number")
continue
break
return
exit
Select the correct function to convert a string to an integer: num = __________("10") print(num + 5)
float()
str()
int()
eval()
Choose the correct code to handle an exception and execute final block: try: x = 10 / 2 except ZeroDivisionError: print("Cannot divide by zero") finally: __________
exit()
pass
print("Execution completed")
return
Which line completes the program to check for an even number? num = 8 if num % 2 == 0: print("Even") else: __________
print("Odd")
print("Even")
break
pass
Choose the correct statement to define a dictionary: data = __________ data['name'] = 'Alice' print(data)
dict()
list()
set()
tuple()
Complete the switch-like statement using a dictionary: def get_day(n): days = { 1: "Monday", 2: "Tuesday", 3: "Wednesday" } return days.__________(n, "Invalid day")
get()
find()
index()
search()
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)
keys
values
items
pairs
Select the correct function to remove the last element from a list: numbers = [1, 2, 3, 4, 5] numbers.__________() print(numbers)
remove()
delete()
pop()
discard()
Complete the nested loop to print a pattern: for i in range(3): for j in range(3): __________ print()
print("*", end=" ")
print("*")
print("*", end="")
print("*", end="\n")
