Python questions

Data types, comprehensions, functions, OOP and output questions.

1. Which of these data types is immutable?

  1. tuple
  2. dict
  3. list
  4. set
Answer

A. tuple Tuples cannot be changed after creation.

2. What does "//" do in Python 3?

  1. Normal division
  2. Comment
  3. Floor division
  4. Power
Answer

C. Floor division 7 // 2 is 3.

3. Which keyword defines a function?

  1. func
  2. function
  3. define
  4. def
Answer

D. def Functions are defined with def.

4. How do you start a single-line comment?

  1. #
  2. /*
  3. //
  4. --
Answer

A. # Python comments begin with #.

5. Which method adds an item to the end of a list?

  1. insert_end()
  2. push()
  3. add()
  4. append()
Answer

D. append() list.append(x) adds x at the end.

6. What is the result of type(3.0)?

  1. <class 'double'>
  2. <class 'int'>
  3. <class 'number'>
  4. <class 'float'>
Answer

D. <class 'float'> 3.0 is a float.

7. Which statement handles exceptions?

  1. catch / finally only
  2. try / catch
  3. do / catch
  4. try / except
Answer

D. try / except Python uses try, except, else and finally.

8. What does len() return for a dictionary?

  1. The largest key
  2. The size in bytes
  3. The number of keys plus values
  4. The number of keys
Answer

D. The number of keys len(d) counts key-value pairs.

9. Which keyword creates an anonymous function?

  1. anon
  2. fn
  3. def
  4. lambda
Answer

D. lambda lambda creates a small anonymous function.

10. What is PEP 8?

  1. A Python version
  2. A package manager
  3. A testing library
  4. The style guide for Python code
Answer

D. The style guide for Python code PEP 8 describes coding conventions.

11. Which of these creates a set?

  1. [1, 2, 3]
  2. {1, 2, 3}
  3. (1, 2, 3)
  4. {}
Answer

B. {1, 2, 3} {} creates an empty dict; {1, 2, 3} is a set.

12. What does "self" refer to in a class method?

  1. The class itself
  2. The current object
  3. A global variable
  4. The parent class
Answer

B. The current object self is the instance the method was called on.

13. Which method initialises a new object of a Python class?

  1. __init__
  2. constructor
  3. __create__
  4. __del__
Answer

A. __init__ __init__ initialises a new object.

14. Which built-in returns both index and value while looping?

  1. enumerate()
  2. index()
  3. range()
  4. zip()
Answer

A. enumerate() enumerate yields (index, value) pairs.

15. What does a generator function use to produce values one at a time?

  1. produce
  2. emit
  3. return only
  4. yield
Answer

D. yield yield pauses the function and hands back a value.

16. Python is:

  1. Compiled to machine code only
  2. Untyped
  3. Dynamically typed
  4. Statically typed
Answer

C. Dynamically typed Types are checked at runtime.

17. Which module is used for regular expressions?

  1. regex
  2. pattern
  3. re
  4. string
Answer

C. re The built-in module is re.

18. Which tool installs third-party Python packages?

  1. pip
  2. npm
  3. pyget
  4. apt-python
Answer

A. pip pip installs packages from PyPI.

19. What does the "with" statement help manage?

  1. Resources such as files, closing them automatically
  2. Threads only
  3. Exceptions only
  4. Loops
Answer

A. Resources such as files, closing them automatically with uses context managers to clean up.

20. Which of these is a mutable sequence?

  1. list
  2. tuple
  3. str
  4. bytes
Answer

A. list Lists can be modified in place.

21. What is the average time complexity of "x in s" for a Python set?

  1. O(n²)
  2. O(n)
  3. O(1)
  4. O(log n)
Answer

C. O(1) Sets are hash-based.

22. What does the GIL in CPython do?

  1. Manages memory only for lists
  2. Speeds up all threads equally
  3. Compiles Python to C
  4. Allows only one thread to run Python bytecode at a time
Answer

D. Allows only one thread to run Python bytecode at a time The Global Interpreter Lock serialises bytecode execution.

23. How do you import only one name from a module?

  1. from module import name
  2. import module.name only
  3. using name
  4. include name
Answer

A. from module import name from x import y imports one name.

24. What is the output?

print(len([1, 2, [3, 4]]))
  1. 4
  2. 2
  3. Error
  4. 3
Answer

D. 3 The inner list counts as one element.

25. What is the output?

print("ab" * 3)
  1. ab3
  2. ababab
  3. Error
  4. aaabbb
Answer

B. ababab Multiplying a string repeats it.

26. What is the output?

x = [1, 2, 3, 4, 5]
print(x[1:4], x[-1])
  1. [2, 3, 4, 5] 1
  2. [2, 3] 5
  3. [1, 2, 3] 5
  4. [2, 3, 4] 5
Answer

D. [2, 3, 4] 5 Slicing [1:4] takes indices 1 to 3; x[-1] is the last item.

27. What is the output?

a = [1, 2, 3]
b = a
b.append(4)
print(a)
  1. [4]
  2. Error
  3. [1, 2, 3, 4]
  4. [1, 2, 3]
Answer

C. [1, 2, 3, 4] b and a refer to the same list.

28. What is the output?

print([i * i for i in range(5) if i % 2 == 0])
  1. [1, 9]
  2. [0, 1, 4, 9, 16]
  3. [0, 4, 16]
  4. [4, 16]
Answer

C. [0, 4, 16] Even i (0, 2, 4) squared.

29. What is the output?

d = {"a": 1, "b": 2}
d["c"] = 3
print(list(d.keys()))
  1. {'a', 'b', 'c'}
  2. ['a', 'b', 'c']
  3. ['c', 'a', 'b']
  4. [1, 2, 3]
Answer

B. ['a', 'b', 'c'] Dictionaries keep insertion order.

30. What is the output?

print(7 // 2, 7 % 2, 2 ** 5)
  1. 3 1 32
  2. 4 1 32
  3. 3.5 1 10
  4. 3 1 10
Answer

A. 3 1 32 Floor division 3, remainder 1, power 32.

31. What is the output?

s = "Osmania"
print(s[::-1])
  1. Osmania
  2. ainamsO
  3. aimnasO
  4. Error
Answer

B. ainamsO A step of -1 reverses the string.

32. What is the output?

def f(x, y=2):
    return x ** y
print(f(3), f(2, 3))
  1. 9 9
  2. 6 8
  3. 9 8
  4. 8 9
Answer

C. 9 8 f(3) uses the default y = 2.

33. What is the output?

print(bool(""), bool("0"), bool([]))
  1. False True False
  2. False True True
  3. False False False
  4. True True False
Answer

A. False True False Empty string and list are falsy; "0" is a non-empty string.

34. What is the output?

t = (1, 2, 3)
a, *b = t
print(a, b)
  1. 1 (2, 3)
  2. Error
  3. (1,) [2, 3]
  4. 1 [2, 3]
Answer

D. 1 [2, 3] Starred assignment collects the rest into a list.

35. What is the output?

def add(item, box=[]):
    box.append(item)
    return box
add(1)
print(add(2))
  1. [2]
  2. [1]
  3. [1, 2]
  4. Error
Answer

C. [1, 2] The default list is created once and shared between calls.

36. What is the output?

nums = [4, 1, 3]
print(sorted(nums), nums)
  1. [1, 3, 4] [4, 1, 3]
  2. None [1, 3, 4]
  3. [4, 3, 1] [4, 1, 3]
  4. [1, 3, 4] [1, 3, 4]
Answer

A. [1, 3, 4] [4, 1, 3] sorted() returns a new list; the original is unchanged.

37. What is the output?

x = 10
def g():
    x = 5
    return x
g()
print(x)
  1. None
  2. Error
  3. 10
  4. 5
Answer

C. 10 The x inside g is local; the global x stays 10.

38. What is the output?

print(list(zip([1, 2, 3], "ab")))
  1. Error
  2. [(1, 'a'), (2, 'b'), (3, None)]
  3. [(1, 'a'), (2, 'b')]
  4. [1, 'a', 2, 'b']
Answer

C. [(1, 'a'), (2, 'b')] zip stops at the shortest input.

39. What is the output?

class Dog:
    def __init__(self, name):
        self.name = name
    def __str__(self):
        return "Dog:" + self.name
print(Dog("Tom"))
  1. <__main__.Dog object>
  2. Error
  3. Tom
  4. Dog:Tom
Answer

D. Dog:Tom print uses __str__.

40. What is the output?

print({x % 3 for x in range(10)})
  1. {1, 2}
  2. {0, 1, 2}
  3. {0, 1, 2, 3}
  4. [0, 1, 2]
Answer

B. {0, 1, 2} A set comprehension keeps unique remainders.

41. What is the output?

try:
    print(int("12a"))
except ValueError:
    print("bad")
finally:
    print("done")
  1. bad
  2. 12 done
  3. done
  4. bad done
Answer

D. bad done int("12a") raises ValueError; finally always runs.

42. What is the output?

gen = (n * 2 for n in range(3))
print(sum(gen), sum(gen))
  1. Error
  2. 0 6
  3. 6 0
  4. 6 6
Answer

C. 6 0 A generator can be consumed only once.

43. What is the output?

words = ["apple", "fig", "banana"]
print(max(words, key=len), min(words))
  1. banana fig
  2. banana apple
  3. fig apple
  4. apple fig
Answer

B. banana apple Longest word is banana; alphabetically smallest is apple.

44. What is the output?

from collections import Counter
print(Counter("hello").most_common(1))
  1. {'l': 2}
  2. ('l', 2)
  3. [('h', 1)]
  4. [('l', 2)]
Answer

D. [('l', 2)] l appears twice.

45. What is the output?

x = [1, 2, 3]
y = x[:]
y[0] = 100
print(x[0], y[0])
  1. 100 100
  2. 100 1
  3. 1 1
  4. 1 100
Answer

D. 1 100 x[:] makes a copy.

46. What is the output?

print(round(2.5), round(3.5), 10 / 4)
  1. 3 4 2.5
  2. 2 4 2.5
  3. 3 3 2.5
  4. 2 3 2
Answer

B. 2 4 2.5 Python rounds halves to the nearest even number.

47. What is the output?

class A:
    def who(self): return "A"
class B(A):
    def who(self): return "B" + super().who()
print(B().who())
  1. AB
  2. A
  3. BA
  4. B
Answer

C. BA super() calls the parent method.

48. What is the output?

f = lambda a, b: a if a > b else b
print(f(3, 8))
  1. 8
  2. True
  3. Error
  4. 3
Answer

A. 8 The lambda returns the larger value.

49. In Python, which description best fits “list”?

  1. lazy sequence of integers
  2. mutable ordered sequence
  3. create an anonymous function expression
  4. immutable ordered sequence
Answer

B. mutable ordered sequence list is used to mutable ordered sequence.

50. In Python, which description best fits “tuple”?

  1. produce values lazily on iteration
  2. immutable ordered sequence
  3. select subsequences with start stop and step
  4. lazy sequence of integers
Answer

B. immutable ordered sequence tuple is used to immutable ordered sequence.

51. In Python, which description best fits “dict”?

  1. mapping of keys to values
  2. unordered collection of distinct hashable elements
  3. select subsequences with start stop and step
  4. immutable ordered sequence
Answer

A. mapping of keys to values dict is used to mapping of keys to values.

52. In Python, which description best fits “set”?

  1. immutable ordered sequence
  2. unordered collection of distinct hashable elements
  3. combine elements from multiple iterables
  4. mutable ordered sequence
Answer

B. unordered collection of distinct hashable elements set is used to unordered collection of distinct hashable elements.

53. In Python, which description best fits “slice”?

  1. select subsequences with start stop and step
  2. mapping of keys to values
  3. iterate with indices and values
  4. produce values lazily on iteration
Answer

A. select subsequences with start stop and step slice is used to select subsequences with start stop and step.

54. In Python, which description best fits “range”?

  1. lazy sequence of integers
  2. create an anonymous function expression
  3. iterate with indices and values
  4. select subsequences with start stop and step
Answer

A. lazy sequence of integers range is used to lazy sequence of integers.

55. In Python, which description best fits “enumerate”?

  1. mapping of keys to values
  2. iterate with indices and values
  3. create an anonymous function expression
  4. lazy sequence of integers
Answer

B. iterate with indices and values enumerate is used to iterate with indices and values.

56. In Python, which description best fits “zip”?

  1. combine elements from multiple iterables
  2. lazy sequence of integers
  3. mapping of keys to values
  4. produce values lazily on iteration
Answer

A. combine elements from multiple iterables zip is used to combine elements from multiple iterables.

57. In Python, which description best fits “lambda”?

  1. iterate with indices and values
  2. create an anonymous function expression
  3. produce values lazily on iteration
  4. mutable ordered sequence
Answer

B. create an anonymous function expression lambda is used to create an anonymous function expression.

58. In Python, which description best fits “generator”?

  1. produce values lazily on iteration
  2. mutable ordered sequence
  3. unordered collection of distinct hashable elements
  4. lazy sequence of integers
Answer

A. produce values lazily on iteration generator is used to produce values lazily on iteration.

59. Which Python concept matches this description: “mutable ordered sequence”?

  1. set
  2. lambda
  3. dict
  4. list
Answer

D. list The definition describes list.

60. Which Python concept matches this description: “immutable ordered sequence”?

  1. lambda
  2. range
  3. dict
  4. tuple
Answer

D. tuple The definition describes tuple.

61. Which Python concept matches this description: “mapping of keys to values”?

  1. generator
  2. dict
  3. tuple
  4. list
Answer

B. dict The definition describes dict.

62. Which Python concept matches this description: “unordered collection of distinct hashable elements”?

  1. tuple
  2. lambda
  3. set
  4. range
Answer

C. set The definition describes set.

63. Which Python concept matches this description: “select subsequences with start stop and step”?

  1. slice
  2. tuple
  3. enumerate
  4. dict
Answer

A. slice The definition describes slice.

64. Which Python concept matches this description: “lazy sequence of integers”?

  1. generator
  2. tuple
  3. range
  4. zip
Answer

C. range The definition describes range.

65. Which Python concept matches this description: “iterate with indices and values”?

  1. slice
  2. lambda
  3. tuple
  4. enumerate
Answer

D. enumerate The definition describes enumerate.

66. Which Python concept matches this description: “combine elements from multiple iterables”?

  1. generator
  2. set
  3. dict
  4. zip
Answer

D. zip The definition describes zip.

67. Which Python concept matches this description: “create an anonymous function expression”?

  1. lambda
  2. range
  3. dict
  4. set
Answer

A. lambda The definition describes lambda.

68. Which Python concept matches this description: “produce values lazily on iteration”?

  1. zip
  2. dict
  3. generator
  4. lambda
Answer

C. generator The definition describes generator.

Mock test

More Technical MCQs topics