Font size
WorksheetsPython Programming Quiz
Total questions: 21
Worksheet time: 11mins
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) [1], [2]
b) [1], [1, 2]
c) [1, 2], [1, 2]
d) [2], [2, 2]
What does the `itertools` module provide?
a) A collection of tools for handling iterators
b) A collection of tools for working with images
c) A collection of tools for working with files
d) A collection of tools for working with databases
Which of the following statements about Python decorators is true?
a) Decorators can only be applied to functions
b) Decorators can be used to modify classes and functions
c) Decorators are used to create new functions
d) Decorators cannot accept arguments
Which of the following methods is used to read all the lines of a file into a list in Python?
a) `readlines()`
b) `read()`
c) `readline()`
d) `readall()`
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) 5 0
b) 0 5
c) 5 5
d) 0 0
What does the `zip()` function do in Python?
a) Combines elements from two or more iterables into tuples
b) Compresses a string
c) Encrypts data
d) None of the above
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) [1, 2, 3, 4, 5, 6, 7, 8]
b) [6, 8, 10, 12]
c) [5, 7, 9, 11]
d) [2, 4, 6, 8]
Which of the following is not a valid way to create a dictionary in Python?
a) `d = {'one': 1, 'two': 2}`
b) `d = dict(one=1, two=2)`
c) `d = dict([('one', 1), ('two', 2)])`
d) `d = dict('one': 1, 'two': 2)`
What is the correct syntax to define a lambda function that adds 10 to the input number?
a) `lambda x: x + 10`
b) `lambda x = x + 10`
c) `lambda x, x + 10`
d) `lambda x; x + 10`
Which built-in function can be used to get the length of a list in Python?
a) `size()`
b) `len()`
c) `length()`
d) `count()`
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]
What will be the output of the following code? a = [1, 2, 3] b = a b.append(4) print(a)
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 3]
[1, 2, 4]
Which of the following statements is true for the `@staticmethod` decorator in Python?
It allows a method to be called on an instance of the class
It allows a method to be called without creating an instance of the class
It restricts the method to be called only within the class
It is used to declare an abstract method
What does the `map()` function do in Python?
It applies a function to all the items in an input list
It creates a map object
It maps the values of a list into a dictionary
It returns a list of the results
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))
True
False
None
Error
Which of the following is used to handle exceptions in Python?
`try-except` block
`if-else` block
`for-while` block
`do-while` block
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)
[1, 2, 3]
[1, 2, 3, 4]
[4]
None of the above
Which module in Python can be used for regular expressions?
`re`
`regex`
`rexp`
`regexp`
What will be the output of the following code? def f(): x = 10 def g(): nonlocal x x = 20 g() return x print(f())
10
20
Error
None
What is the difference between a list and a tuple in Python?
Lists are mutable, tuples are immutable
Lists are immutable, tuples are mutable
There is no difference
Lists are faster than tuples
Which of the following statements is true about the `with` statement in Python?
It is used to create loops
It simplifies exception handling
It is used for file handling and ensures proper acquisition and release of resources
It is used to define functions
