wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Quiz

Total questions: 27

Worksheet time: 14mins

Name
Class
Date
1.

What will be the output of the following code? x = 5 y = "Hello" z = 3.0 print(type(x), type(y), type(z))

a)

<class 'int'> <class 'string'> <class 'float'>

b)

<class 'int'> <class 'str'> <class 'float'>

c)

<class 'int'> <class 'string'> <class 'complex'>

d)

<class 'float'> <class 'int'> <class 'str'>

2.

Which of the following is the correct syntax to declare a function in Python?

a)

def function_name:

b)

def function_name[]:

c)

def function_name():

d)

define function_name():

3.

What will be the result of the following operation? result = 7 // 2 print(result)

a)

3.5

b)

3

c)

4

d)

2.5

4.

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

a)

_myvar

b)

my_var2

c)

2varname

d)

myVar

5.

What will be the output of the following code? my_list = [1, 2, 3, 4, 5] my_list.append([6, 7]) print(my_list)

a)

[1, 2, 3, 4, 5, 6, 7]

b)

[1, 2, 3, 4, 5, [6, 7]]

c)

[1, 2, 3, 4, 5, 6, [7]]

d)

[1, 2, 3, 4, 5, 6, 7, [8]]

6.

What is the output of the following code? my_list = [10, 20, 30, 40, 50] print(my_list[1:4])

a)

[20, 30, 40]

b)

[10, 20, 30, 40]

c)

[30, 40, 50]

d)

[20, 30, 40, 50]

7.

Which method is used to add multiple elements to a list in Python?

a)

append()

b)

extend()

c)

insert()

d)

add()

8.

What will be the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3} print(my_dict['b'])

a)

1

b)

2

c)

3

d)

'b'

9.

Which of the following is the correct way to delete an element from a dictionary?

a)

del my_dict[key]

b)

my_dict.remove(key)

c)

my_dict.pop(key)

d)

my_dict.clear()

10.

What happens if you try to access a key that doesn't exist in a dictionary?

a)

Returns None

b)

Throws a KeyError

c)

Adds the key with a value 0

d)

Creates a new key-value pair

11.

Which of the following will create a set in Python?

a)

my_set = ()

b)

my_set = {}

c)

my_set = {1, 2, 3}

d)

my_set = (1,2,3)

12.

What will be the output of the following code? my_set = {1, 2, 3, 4, 5} my_set.add(6) print(my_set)

a)

{1, 2, 3, 4, 5}

b)

{1, 2, 3, 4, 5, 6}

c)

[1, 2, 3, 4, 5, 6]

d)

{6, 5, 4, 3, 2, 1}

13.

Which of the following is a valid tuple declaration?

a)

my_tuple = (1,)

b)

my_tuple = [1]

c)

my_tuple = {1,}

d)

my_tuple = (1)

14.

What will be the output of the following code? class MyClass: def __init__(self, name): self.name = name obj = MyClass("John") print(obj.name)

a)

John

b)

name

c)

None

d)

Error

15.

Which of the following statements is true about inheritance in Python?

a)

A class can inherit only from one superclass.

b)

A class can inherit from multiple classes.

c)

Python does not support inheritance.

d)

Classes cannot have methods in Python.

16.

Which of the following describes polymorphism in Python?

a)

Using the same method name in different classes.

b)

Creating multiple objects from the same class.

c)

Overriding methods from the parent class.

d)

Inheriting from multiple classes.

17.

What is method overriding in Python?

a)

When two methods have the same name but different signatures.

b)

When a subclass defines a method with the same name as a method in its parent class.

c)

When a method in the same class is written twice.

d)

Defining a method without parameters.

18.

What will be the output of the following code?

a)

[0, 1, 4] [3, 2, 1, 0, 1, 4] [0, 1, 4]

b)

[0, 1, 4] [3, 2, 1, 0, 1, 4] [0, 1]

c)

[0, 1, 4] [3, 2, 1, 0, 1, 4] [0, 1, 4]

d)

[0, 1, 4] [0, 1] [0, 1]

19.

What will be the result of the following code? a = [1, 2, 3] b = [1, 2, 3] print(a == b) print(a is b)

a)

True True

b)

True False

c)

False True

d)

False False

20.

What is the output of the following code? def gfg(x=[]): x.append(1) return x print(gfg()) print(gfg())

a)

[1] [1, 1]

b)

[1] [1]

c)

[1, 1] [1, 1]

d)

[] []

21.

What will be the output of the following code? class A: def __init__(self): self.__var = 10 a = A() print(a.__var)

a)

10

b)

AttributeError

c)

None

d)

SyntaxError

22.

What will the following code print? class Test: def __init__(self): self.value = 1 def some_func(obj): obj.value += 10 test = Test() some_func(test) print(test.value)

a)

1

b)

10

c)

11

d)

Error

23.

Which of the following decorators can be used to make a method of a class belong to the class rather than an instance of it?

a)

@classmethod

b)

@staticmethod

c)

@abstractmethod

d)

@property

24.

What is the purpose of the global keyword in Python?

a)

To define a variable inside a function.

b)

To declare that a variable inside a function refers to a global variable.

c)

To restrict access to a variable outside of a function.

d)

To initialize a static variable.

25.

x = [1, 2, 3]

y = x

y.append(4)

print(x)

print(y)

a)

[1, 2, 3], [1, 2, 3, 4]

b)

[1, 2, 3, 4], [1, 2, 3]

c)

[1, 2, 3, 4], [1, 2, 3, 4]

d)

Error: Cannot append to a list

26.

What is the result of the following code?

class MyClass:

x = 10

a = MyClass()

b = MyClass()

a.x = 20

print(b.x)

a)

10

b)

20

c)

Error

d)

None

27.

my_tuple = (1, 2, 3)
my_tuple[1] = 4
print(my_tuple)

a)

(1, 4, 3)

b)

(1, 2, 3)

c)

TypeError: 'tuple' object does not support item assignment

d)

[1, 4, 3]