wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Programming Quiz

Total questions: 21

Worksheet time: 11mins

Name
Class
Date
1.

What will be the output of the following code? def func(x, y=[]): y.append(x) return y print(func(1)) print(func(2))

a)

a) [1], [2]

b)

b) [1], [1, 2]

c)

c) [1, 2], [1, 2]

d)

d) [2], [2, 2]

2.

What does the `itertools` module provide?

a)

a) A collection of tools for handling iterators

b)

b) A collection of tools for working with images

c)

c) A collection of tools for working with files

d)

d) A collection of tools for working with databases

3.

Which of the following statements about Python decorators is true?

a)

a) Decorators can only be applied to functions

b)

b) Decorators can be used to modify classes and functions

c)

c) Decorators are used to create new functions

d)

d) Decorators cannot accept arguments

4.

Which of the following methods is used to read all the lines of a file into a list in Python?

a)

a) `readlines()`

b)

b) `read()`

c)

c) `readline()`

d)

d) `readall()`

5.

What will be the output of the following code? class MyClass: def __init__(self, value=0): self.value = value obj1 = MyClass(5) obj2 = MyClass() print(obj1.value, obj2.value)

a)

a) 5 0

b)

b) 0 5

c)

c) 5 5

d)

d) 0 0

6.

What does the `zip()` function do in Python?

a)

a) Combines elements from two or more iterables into tuples

b)

b) Compresses a string

c)

c) Encrypts data

d)

d) None of the above

7.

What will be the output of the following code? x = [1, 2, 3, 4] y = [5, 6, 7, 8] z = [a + b for a, b in zip(x, y)] print(z)

a)

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

b)

b) [6, 8, 10, 12]

c)

c) [5, 7, 9, 11]

d)

d) [2, 4, 6, 8]

8.

Which of the following is not a valid way to create a dictionary in Python?

a)

a) `d = {'one': 1, 'two': 2}`

b)

b) `d = dict(one=1, two=2)`

c)

c) `d = dict([('one', 1), ('two', 2)])`

d)

d) `d = dict('one': 1, 'two': 2)`

9.

What is the correct syntax to define a lambda function that adds 10 to the input number?

a)

a) `lambda x: x + 10`

b)

b) `lambda x = x + 10`

c)

c) `lambda x, x + 10`

d)

d) `lambda x; x + 10`

10.

Which built-in function can be used to get the length of a list in Python?

a)

a) `size()`

b)

b) `len()`

c)

c) `length()`

d)

d) `count()`

11.

What will be the output of the following code? a = [1, 2, 3] b = a b.append(4) print(a)

a)

a) [1, 2, 3]

b)

b) [1, 2, 3, 4]

c)

c) [1, 2, 3, 3]

12.

What will be the output of the following code? a = [1, 2, 3] b = a b.append(4) print(a)

a)

[1, 2, 3]

b)

[1, 2, 3, 4]

c)

[1, 2, 3, 3]

d)

[1, 2, 4]

13.

Which of the following statements is true for the `@staticmethod` decorator in Python?

a)

It allows a method to be called on an instance of the class

b)

It allows a method to be called without creating an instance of the class

c)

It restricts the method to be called only within the class

d)

It is used to declare an abstract method

14.

What does the `map()` function do in Python?

a)

It applies a function to all the items in an input list

b)

It creates a map object

c)

It maps the values of a list into a dictionary

d)

It returns a list of the results

15.

What will be the output of the following code? def foo(x): return x * 2 bar = lambda x: x * 2 print(foo(3) == bar(3))

a)

True

b)

False

c)

None

d)

Error

16.

Which of the following is used to handle exceptions in Python?

a)

`try-except` block

b)

`if-else` block

c)

`for-while` block

d)

`do-while` block

17.

What is the output of the following code? def add_to_list(lst, item): lst.append(item) return lst my_list = [1, 2, 3] new_list = add_to_list(my_list, 4) print(my_list)

a)

[1, 2, 3]

b)

[1, 2, 3, 4]

c)

[4]

d)

None of the above

18.

Which module in Python can be used for regular expressions?

a)

`re`

b)

`regex`

c)

`rexp`

d)

`regexp`

19.

What will be the output of the following code? def f(): x = 10 def g(): nonlocal x x = 20 g() return x print(f())

a)

10

b)

20

c)

Error

d)

None

20.

What is the difference between a list and a tuple in Python?

a)

Lists are mutable, tuples are immutable

b)

Lists are immutable, tuples are mutable

c)

There is no difference

d)

Lists are faster than tuples

21.

Which of the following statements is true about the `with` statement in Python?

a)

It is used to create loops

b)

It simplifies exception handling

c)

It is used for file handling and ensures proper acquisition and release of resources

d)

It is used to define functions