Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Quiz – 50 Questions (Easy + Medium Level)

Total questions: 50

Worksheet time: 28mins

Name
Class
Date
1.

What is the correct syntax to output “Hello World” in Python?

a)

echo("Hello World")

b)

print("Hello World")

c)

cout << "Hello World"

d)

printf("Hello World")

2.

Which of the following is a valid variable name in Python?

a)

1num

b)

num_1

c)

num-1

d)

num@1

3.

What will be the output of this code?

x = 5

y = 10

print(x * y + y // x)

a)

A) 52

b)

B) 55

c)

C) 50

d)

D) 60

4.

What data type is the result of this expression: 3 / 2 ?

a)

int

b)

float

c)

bool

d)

str

5.

In Python, how do you take input from a user and convert it to an integer?

a)

input(int())

b)

int(input())

6.

Which operator is used for exponentiation in Python?

a)

^

b)

**

c)

pow() only

d)

exp()

7.

Pseudo Code:

IF age >= 18 THEN

   PRINT ("Adult")

ELSE

    PRINT ("Minor" )

If age = 16, what will be printed?

a)

Adult

b)

Minor

c)

Error

d)

None

8.

What is the output of the following code? for i in range(3): print(i, end=" ")

a)

A) 0 1 2

b)

B) 1 2 3

c)

C) 0 1 2 3

d)

D) Error

9.

Which of the following keywords is used to skip an iteration in a loop?

a)

exit

b)

skip

c)

continue

d)

break

10.

What is the output of this code? i = 1 while i < 4: print(i) i += 1

a)

A) 0 1 2

b)

B) 1 2 3

c)

C) Infinite loop

d)

D) 2 3 4

11.

What will this code print? for i in range(5, 0, -2): print(i, end=",")

a)

A) 5,3,1,

b)

B) 5,4,3,2,1,

c)

C) 5,2,

d)

D) 1,3,5,

12.

What is the output of the FizzBuzz pseudo-code for n = 15?

IF n MOD 3 == 0 AND n MOD 5 == 0:

   print( "FizzBuzz")

a)

Fizz

b)

Buzz

c)

FizzBuzz

d)

None

13.

Which keyword defines a function in Python?

a)

function

b)

define

c)

def

d)

fn

14.

What is the output of this code? def add(a, b=3): return a + b print(add(2))

a)

3

b)

5

c)

2

d)

Error

15.

Which of the following data structures is immutable?

a)

List

b)

Tuple

c)

Set

d)

Dictionary

16.

What will be the output of this code? nums = [1, 2, 3] nums.append([4,5]) print(len(nums))

a)

A) 3

b)

B) 4

c)

C) 5

d)

D) Error

17.

What will len({'a':1, 'b':2, 'c':3}) return?

a)

3

b)

6

c)

2

d)

Error

18.

List Comprehension Output: [x**2 for x in range(3)]

a)

[1, 2, 3]

b)

[0, 1, 4]

c)

[0, 1, 2]

d)

[1, 4, 9]

19.

Which mode is used to open a file for reading?

a)

'r'

b)

'w'

c)

'a'

d)

'rw'

20.

What does the with statement ensure during file handling?

a)

Manual file closing

b)

Automatic resource management

c)

File duplication

d)

Error-free code

21.

Which of the following creates a class?

a)

function MyClass():

b)

define MyClass():

c)

class MyClass:

d)

class(MyClass)

22.

What is the correct way to create an object of a class in Python?

a)

obj = MyClass()

b)

obj = new MyClass

c)

obj = MyClass.create()

d)

MyClass obj

23.

If a file does not exist, which mode creates it automatically?

a)

'r'

b)

'w'

c)

'a+'

d)

'x'

24.

Which of these statements imports a custom module named "game"?

a)

include game

b)

import game

c)

using game

d)

from game include *

25.

What will this code print?

class Car:

    wheels = 4

    def init(self, name):

        self.name = name

 

obj = Car("BMW")

print(obj.wheels)

a)

Error

b)

BMW

c)

4

d)

None

26.

Which library is commonly used to make 2D games in Python?

a)

tkinter

b)

numpy

c)

pygame

d)

matplotlib

27.

In Pygame, which function initializes all modules?

a)

pygame.start()

b)

pygame.init()

c)

pygame.run()

d)

pygame.play()

28.

What is the purpose of the game loop?

a)

To initialize graphics

b)

To continuously update and draw game frames

c)

To handle imports

d)

To save files

29.

Which function updates the display window in Pygame?

a)

pygame.display.flip()

b)

pygame.window.update()

c)

pygame.refresh()

d)

pygame.display.init()

30.

What event checks for key presses in Pygame?

a)

pygame.event.key()

b)

pygame.key.get_pressed()

c)

pygame.check.key()

d)

pygame.keyboard.press()

31.

What will be the output of this code snippet?

for i in range(1, 6):

if i == 3:

continue

print(i, end=" ")

a)

A) 1 2 3 4 5

b)

B) 1 2 4 5

c)

C) 1 3 5

d)

D) 2 3 4 5

32.

Which of the following statements will correctly read all lines from a file named ‘data.txt’?

a)

f = open('data.txt', 'r').read()

b)

f = open('data.txt').readlines()

c)

f = open('data.txt', 'w').read()

d)

f = open('data.txt', 'a').readlines()

33.

What will happen if you call file.close() twice?

a)

File will reopen

b)

Error occurs

c)

Nothing happens (second close is ignored)

d)

File is deleted

34.

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)

A) 3

b)

B) 6

c)

C) 9

d)

D) 1

35.

In Python, what is the default return value of a function that doesn’t have a 'return' statement?

a)

0

b)

None

c)

""

d)

False

36.

Which of these methods can be used to remove an element from a list?

a)

remove()

b)

delete()

37.

What will be the output of this code?

a = [1, 2, 3]

b = a

b.append(4)

print(a)

a)

[1, 2, 3]

b)

[1, 2, 3, 4]

c)

[4]

d)

Error

38.

What does try-except block do?

a)

Stops the program immediately

b)

Handles runtime errors gracefully

c)

Runs code faster

d)

Ignores all errors silently

39.

How do you set up a display window with a width of 800 pixels and a height of 600 pixels using Pygame?

a)

A. pygame.display.set_mode((800, 600))

b)


B. pygame.window.set_size(800, 600)

c)

C. pygame.create_window(800, 600)

d)


D. pygame.set_display_size((800, 600))

40.
  • What type of error will the following code snippet produce?

print("Hello" + 5)

a)

a) SyntaxError

b)

b) TypeError

c)


c) NameError

d)

d) ValueError

41.

Which function must be called to initialize all the modules required by the Pygame library?

a)

A. pygame.start()

b)


B. pygame.setup()

c)


C. pygame.init()

d)


D. pygame.begin()

42.

What is the purpose of pygame.time.Clock() in a game loop?

a)

Manages sound effects

b)

Controls frame rate (FPS)

c)

Tracks score

d)

Handles user input

43.

Which method detects mouse click events in Pygame?

a)

pygame.mouse.get_event()

b)

pygame.event.get() with MOUSEBUTTONDOWN

c)

pygame.click()

d)

pygame.mouse.click_event()

44.

Which function in Pygame is used to fill the screen with a color?

a)

screen.draw()

b)

screen.fill(color)

c)

pygame.color.fill()

d)

fill.screen()

45.

In the “Catch the Fruit” game, what does collision detection check for?

a)

If basket touches window border

b)

If fruit and basket overlap

c)

If player presses a key

d)

If game is paused

46.
  1. Which of these is a popular cross-platform game development framework for Python?

a)
  • a) Tkinter

b)
  • b) NumPy

c)
  • c) Pygame

d)
  • d) Pandas

47.

What will happen if you forget to call pygame.quit() after closing the window?

a)

Program crashes

b)

Program hangs until force closed

c)

Minor memory leak or unclosed resources

d)

Nothing happens

48.

Which function in Python is used to generate random numbers for game events?

a)

random.randint()

b)

rand()

c)

random.int()

d)

math.random()

49.

If pygame.image.load('apple.png') is used, what does it return?

a)

The image file path

b)

An image object (Surface)

c)

A string

d)

None

50.
  • Which of the following is a type of error that occurs during the execution of a program?

a)

a) Syntax Error

b)

b) Logical Error

c)

c) Runtime Error (Exception)

d)

d) All of the above