Python Programming Quiz - Advanced Level
An advanced evaluation of Python core concepts, memory, and OOP.
Part A: Code, Output, and Fill-in-the-blank
1. What is the output of the following code?
def append_to(num, target=[]):
[Link](num)
return target
print(append_to(1))
print(append_to(2))
A. [1]\n[2]
B. [1]\n[1, 2]
C. Error
D. [1, 2]\n[1, 2]
2. What is the output of the following code?
a = 256
b = 256
c = 257
d = 257
print(a is b, c is d)
A. True True
B. False False
C. True False
D. False True
3. What is the output of the following code?
x = 10
def foo():
x += 1
print(x)
foo()
A. 11
B. 10
C. UnboundLocalError
D. NameError
4. What is the output of the following code?
s = 'abcdef'
print(s[1:-1:2])
A. bdf
B. bd
C. ace
D. ce
5. What is the output of the following code?
matrix = [[1, 2], [3, 4]]
res = [x for row in matrix for x in row]
print(res)
A. [[1, 2], [3, 4]]
B. [1, 2, 3, 4]
C. [[1, 3], [2, 4]]
D. Error
6. What is the output of the following code?
d = {k: v for k, v in enumerate('abc')}
print(d[1])
A. a
B. b
C. c
D. 1
7. What is the output of the following code?
nums = [1, 2, 3]
res = list(map(lambda x: x*2, filter(lambda x: x>1, nums)))
print(res)
A. [2, 4, 6]
B. [4, 6]
C. [2, 3]
D. [6]
8. What is the output of the following code?
def func():
try:
return 1
finally:
return 2
print(func())
A. 1
B. 2
C. (1, 2)
D. Error
9. What is the output of the following code?
class A:
def foo(self): print('A')
class B(A):
pass
class C(A):
def foo(self): print('C')
class D(B, C):
pass
D().foo()
A. A
B. B
C. C
D. Error
10. What is the output of the following code?
def gen():
yield 1
return 2
yield 3
g = gen()
print(next(g))
print(next(g))
A. 1\n2
B. 1\n3
C. 1\nStopIteration
D. Error
11. What is the output of the following code?
def dec(func):
def wrapper():
return func() + '!'
return wrapper
@dec
def greet():
return 'Hello'
print(greet())
A. Hello
B. Hello!
C.
D. Error
12. What is the output of the following code?
s1 = {1, 2, 3}
s2 = {3, 4, 5}
print(s1 ^ s2)
A. {3}
B. {1, 2, 4, 5}
C. {1, 2, 3, 4, 5}
D. Error
13. What is the output of the following code?
def outer():
x = 1
def inner():
nonlocal x
x = 2
inner()
print(x)
outer()
A. 1
B. 2
C. Error
D. None
14. What is the output of the following code?
print(all([True, 1, ' ', 0]))
A. True
B. False
C. Error
D. 1
15. What is the output of the following code?
keys = ['a', 'b']
vals = [1, 2, 3]
print(dict(zip(keys, vals)))
A. {'a': 1, 'b': 2, 'c': 3}
B. {'a': 1, 'b': 2}
C. Error
D. {'a': 1, 'b': 3}
16. Choose the correct expression to unpack a dictionary 'd' as keyword arguments into a
function 'func'.
A. func(*d)
B. func(d*)
C. func(**d)
D. func(d)
17. What is the output of the following code?
a = [1, 2, 3]
b = a
[Link](4)
print(a)
A. [1, 2, 3]
B. [1, 2, 3, 4]
C. [4]
D. Error
18. What is the output of the following code?
funcs = [lambda x: x + i for i in range(3)]
print([f(1) for f in funcs])
A. [1, 2, 3]
B. [3, 3, 3]
C. [1, 1, 1]
D. Error
19. What is the output of the following code?
class X:
__val = 10
print(X.__val)
A. 10
B. None
C. AttributeError
D. 0
20. What is the output of the following code?
print(bool('False'), bool(''))
A. False False
B. True True
C. True False
D. False True
Part B: Theory Questions
21. What does the Global Interpreter Lock (GIL) in CPython do?
A. Prevents multiple processes from running simultaneously.
B. Prevents multiple threads from executing Python bytecodes at once.
C. Locks memory variables from being garbage collected.
D. Encrypts source code securely.
22. Which of the following is true about deepcopy vs shallow copy?
A. Shallow copy creates independent copies of all nested objects.
B. Deepcopy copies references to the nested objects.
C. Deepcopy recursively creates copies of all nested objects.
D. They behave exactly the same for lists.
23. Which magic methods are required to implement a context manager (using 'with'
statement)?
A. __init__ and __del__
B. __enter__ and __exit__
C. __start__ and __stop__
D. __open__ and __close__
24. What is the purpose of the __slots__ attribute in a Python class?
A. To allow dynamic addition of attributes.
B. To save memory by preventing the creation of __dict__ for instances.
C. To define private methods.
D. To restrict class inheritance.
25. Which of the following is a requirement for an object to be used as a dictionary key?
A. It must be a mutable object.
B. It must be an integer or string.
C. It must be hashable.
D. It must be iterable.
26. What is the average time complexity of the 'x in s' operation where 's' is a Python set?
A. O(1)
B. O(log N)
C. O(N)
D. O(N^2)
27. What is the main difference between @staticmethod and @classmethod?
A. @staticmethod takes cls as the first argument.
B. @classmethod takes the class as the first implicit argument.
C. There is no difference.
D. @classmethod cannot be called from an instance.
28. Which built-in function is used to dynamically evaluate a Python expression from a string?
A. exec()
B. eval()
C. run()
D. compile()
29. How does Python handle memory management and garbage collection primarily?
A. Manual memory management using alloc/free.
B. Purely through tracing garbage collection.
C. Reference counting combined with a cyclic garbage collector.
D. Mark-and-sweep algorithm only.
30. Which special method is invoked when the '+' operator is used on two custom objects?
A. __plus__
B. __sum__
C. __add__
D. __concat__
Part C: True/False Questions
31. A tuple can contain mutable elements like lists, and those elements can be modified.
True
False
32. The 'is' operator checks if two variables have the same value, regardless of their memory
location.
True
False
33. In Python 3.7 and above, standard dictionaries maintain the insertion order of their keys.
True
False
34. You can catch multiple distinct exception types in a single 'except' block by passing them
as a tuple.
True
False
35. Private attributes (starting with __) in a Python class cannot be accessed from outside the
class by any means.
True
False
36. Using a mutable object (like a list) as a default argument value in a function definition is a
recommended best practice.
True
False
37. A generator function uses the 'yield' keyword to return an iterator that produces a
sequence of values lazily.
True
False
38. The '[Link]()' function returns the exact total memory footprint of an object,
including all the objects it recursively references.
True
False
39. It is possible to instantiate an Abstract Base Class (ABC) directly in Python.
True
False
40. In Python, functions are first-class citizens, meaning they can be passed as arguments to
other functions and returned from functions.
True
False
Answer Key
1. B 2. C 3. C 4. B 5. B
6. B 7. B 8. B 9. C 10. C
11. B 12. B 13. B 14. B 15. B
16. C 17. B 18. B 19. C 20. C
21. B 22. C 23. B 24. B 25. C
26. A 27. B 28. B 29. C 30. C
31. True 32. False 33. True 34. True 35. False
36. False 37. True 38. False 39. False 40. True