WorksheetsPython Quiz – 50 Questions (Easy + Medium Level)
Total questions: 50
Worksheet time: 28mins
What is the correct syntax to output “Hello World” in Python?
echo("Hello World")
print("Hello World")
cout << "Hello World"
printf("Hello World")
Which of the following is a valid variable name in Python?
1num
num_1
num-1
num@1
What will be the output of this code?
x = 5
y = 10
print(x * y + y // x)
A) 52
B) 55
C) 50
D) 60
What data type is the result of this expression: 3 / 2 ?
int
float
bool
str
In Python, how do you take input from a user and convert it to an integer?
input(int())
int(input())
Which operator is used for exponentiation in Python?
^
**
pow() only
exp()
Pseudo Code:
IF age >= 18 THEN
PRINT ("Adult")
ELSE
PRINT ("Minor" )
If age = 16, what will be printed?
Adult
Minor
Error
None
What is the output of the following code? for i in range(3): print(i, end=" ")
A) 0 1 2
B) 1 2 3
C) 0 1 2 3
D) Error
Which of the following keywords is used to skip an iteration in a loop?
exit
skip
continue
break
What is the output of this code? i = 1 while i < 4: print(i) i += 1
A) 0 1 2
B) 1 2 3
C) Infinite loop
D) 2 3 4
What will this code print? for i in range(5, 0, -2): print(i, end=",")
A) 5,3,1,
B) 5,4,3,2,1,
C) 5,2,
D) 1,3,5,
What is the output of the FizzBuzz pseudo-code for n = 15?
IF n MOD 3 == 0 AND n MOD 5 == 0:
print( "FizzBuzz")
Fizz
Buzz
FizzBuzz
None
Which keyword defines a function in Python?
function
define
def
fn
What is the output of this code? def add(a, b=3): return a + b print(add(2))
3
5
2
Error
Which of the following data structures is immutable?
List
Tuple
Set
Dictionary
What will be the output of this code? nums = [1, 2, 3] nums.append([4,5]) print(len(nums))
A) 3
B) 4
C) 5
D) Error
What will len({'a':1, 'b':2, 'c':3}) return?
3
6
2
Error
List Comprehension Output: [x**2 for x in range(3)]
[1, 2, 3]
[0, 1, 4]
[0, 1, 2]
[1, 4, 9]
Which mode is used to open a file for reading?
'r'
'w'
'a'
'rw'
What does the with statement ensure during file handling?
Manual file closing
Automatic resource management
File duplication
Error-free code
Which of the following creates a class?
function MyClass():
define MyClass():
class MyClass:
class(MyClass)
What is the correct way to create an object of a class in Python?
obj = MyClass()
obj = new MyClass
obj = MyClass.create()
MyClass obj
If a file does not exist, which mode creates it automatically?
'r'
'w'
'a+'
'x'
Which of these statements imports a custom module named "game"?
include game
import game
using game
from game include *
What will this code print?
class Car:
wheels = 4
def init(self, name):
self.name = name
obj = Car("BMW")
print(obj.wheels)
Error
BMW
4
None
Which library is commonly used to make 2D games in Python?
tkinter
numpy
pygame
matplotlib
In Pygame, which function initializes all modules?
pygame.start()
pygame.init()
pygame.run()
pygame.play()
What is the purpose of the game loop?
To initialize graphics
To continuously update and draw game frames
To handle imports
To save files
Which function updates the display window in Pygame?
pygame.display.flip()
pygame.window.update()
pygame.refresh()
pygame.display.init()
What event checks for key presses in Pygame?
pygame.event.key()
pygame.key.get_pressed()
pygame.check.key()
pygame.keyboard.press()
What will be the output of this code snippet?
for i in range(1, 6):
if i == 3:
continue
print(i, end=" ")
A) 1 2 3 4 5
B) 1 2 4 5
C) 1 3 5
D) 2 3 4 5
Which of the following statements will correctly read all lines from a file named ‘data.txt’?
f = open('data.txt', 'r').read()
f = open('data.txt').readlines()
f = open('data.txt', 'w').read()
f = open('data.txt', 'a').readlines()
What will happen if you call file.close() twice?
File will reopen
Error occurs
Nothing happens (second close is ignored)
File is deleted
Pseudo Code:
FUNCTION factorial(n)
IF n == 1 THEN
RETURN 1
ELSE
RETURN n * factorial(n-1)
What is the output of factorial(3)?
A) 3
B) 6
C) 9
D) 1
In Python, what is the default return value of a function that doesn’t have a 'return' statement?
0
None
""
False
Which of these methods can be used to remove an element from a list?
remove()
delete()
What will be the output of this code?
a = [1, 2, 3]
b = a
b.append(4)
print(a)
[1, 2, 3]
[1, 2, 3, 4]
[4]
Error
What does try-except block do?
Stops the program immediately
Handles runtime errors gracefully
Runs code faster
Ignores all errors silently
How do you set up a display window with a width of 800 pixels and a height of 600 pixels using Pygame?
A. pygame.display.set_mode((800, 600))
B. pygame.window.set_size(800, 600)
C. pygame.create_window(800, 600)
D. pygame.set_display_size((800, 600))
What type of error will the following code snippet produce?
print("Hello" + 5)
a) SyntaxError
b) TypeError
c) NameError
d) ValueError
Which function must be called to initialize all the modules required by the Pygame library?
A. pygame.start()
B. pygame.setup()
C. pygame.init()
D. pygame.begin()
What is the purpose of pygame.time.Clock() in a game loop?
Manages sound effects
Controls frame rate (FPS)
Tracks score
Handles user input
Which method detects mouse click events in Pygame?
pygame.mouse.get_event()
pygame.event.get() with MOUSEBUTTONDOWN
pygame.click()
pygame.mouse.click_event()
Which function in Pygame is used to fill the screen with a color?
screen.draw()
screen.fill(color)
pygame.color.fill()
fill.screen()
In the “Catch the Fruit” game, what does collision detection check for?
If basket touches window border
If fruit and basket overlap
If player presses a key
If game is paused
Which of these is a popular cross-platform game development framework for Python?
a) Tkinter
b) NumPy
c) Pygame
d) Pandas
What will happen if you forget to call pygame.quit() after closing the window?
Program crashes
Program hangs until force closed
Minor memory leak or unclosed resources
Nothing happens
Which function in Python is used to generate random numbers for game events?
random.randint()
rand()
random.int()
math.random()
If pygame.image.load('apple.png') is used, what does it return?
The image file path
An image object (Surface)
A string
None
Which of the following is a type of error that occurs during the execution of a program?
a) Syntax Error
b) Logical Error
c) Runtime Error (Exception)
d) All of the above
