Modules & Standard Library Questions with Answers

7 practice questions on Modules & Standard Library (Python) with answers and step-by-step solutions. Try 7 samples below, then practise all 7 free in the AptiNest app.

Sample questions

Question 1Easy

What is the purpose of the check if __name__ == "__main__": in a module?

  1. It runs the block only when the file is executed directly
  2. It makes the module importable from other packages
  3. It is required for any Python script to run at all
  4. It marks the main class that the module exports

Answer: A. It runs the block only when the file is executed directly

Explanation

When a file is run as a script its __name__ is '__main__'. When it is imported, __name__ is the module name, so the block is skipped.

Step-by-step solution

  1. When a file is run directly, Python sets __name__ to '__main__'.
  2. When the file is imported, __name__ is the module's name, so the block is skipped.
  3. So the block runs only when the file is run directly, the correct answer.

Why the other options are wrong

  • B. Modules can be imported without this check. The check only controls which code runs.
  • C. Scripts run without this check, so it is optional.
  • D. Python does not use this check to mark a main class.

Tip: Put code that should run only as a script under if __name__ == '__main__':.

Question 2Easy

What is the output?

from collections import Counter
print(Counter("banana").most_common(1))
  1. ('a', 3)
  2. {'a': 3}
  3. [('n', 2)]
  4. [('a', 3)]

Answer: D. [('a', 3)]

Explanation

Counter counts each character: a=3, n=2, b=1.
most_common(1) returns a list holding the single most frequent (item, count) pair.

Step-by-step solution

  1. Counter('banana'): a:3, n:2, b:1.
  2. most_common(1) returns a list of tuples.
  3. Result [('a', 3)].

Why the other options are wrong

  • A. Missing the list wrapper.
  • B. It's not a dict.
  • C. 'n' has count 2.

Question 3Easy

What is the output?

from collections import defaultdict
d = defaultdict(int)
for c in "aab":
    d[c] += 1
print(dict(d))
  1. {'a': 1, 'b': 1}
  2. {'a': 2, 'b': 1}
  3. KeyError
  4. {'a': 2}

Answer: B. {'a': 2, 'b': 1}

Explanation

defaultdict(int) creates a missing key with value int() = 0 on first access, so counting works without a KeyError.

Step-by-step solution

  1. defaultdict(int) gives 0 for missing keys.
  2. 'a' counted twice, 'b' once.
  3. dict(d) gives {'a': 2, 'b': 1}.

Why the other options are wrong

  • A. 'a' appears twice.
  • C. defaultdict never raises KeyError here.
  • D. 'b' is also counted.

Question 4Medium

What is the output?

from itertools import accumulate
print(list(accumulate([1, 2, 3, 4])))
  1. [1, 3, 6, 10]
  2. [10]
  3. [0, 1, 3, 6]
  4. [1, 2, 3, 4]

Answer: A. [1, 3, 6, 10]

Explanation

accumulate yields running totals: 1, 1+2, 1+2+3, 1+2+3+4.

Step-by-step solution

  1. accumulate gives running sums.
  2. 1, 1+2=3, 3+3=6, 6+4=10.

Why the other options are wrong

  • B. That is only the final total.
  • C. It does not start with 0.
  • D. That is the input.

Question 5Medium

What is the output?

import json
print(json.dumps({"ok": True, "n": None}))
  1. {"ok": "true", "n": "null"}
  2. {'ok': True, 'n': None}
  3. {"ok": true, "n": null}
  4. {"ok": True, "n": None}

Answer: C. {"ok": true, "n": null}

Explanation

json.dumps converts Python True to JSON true and None to JSON null, and always uses double-quoted keys.

Step-by-step solution

  1. json.dumps turns a Python dict into JSON text.
  2. True becomes the JSON word true, and None becomes null.
  3. The keys are in double quotes, so the output is {"ok": true, "n": null}, the correct answer.

Why the other options are wrong

  • A. The true and null values are not quoted strings in JSON.
  • B. JSON uses double quotes and lowercase true and null, not Python's single quotes and True and None.
  • D. True and None are Python words; JSON uses true and null.

Tip: json.dumps turns Python True, False and None into JSON true, false and null, and keys are always in double quotes.

Question 6Medium

What is the output?

import math
print(math.floor(-2.5), math.ceil(-2.5))
  1. -2 -3
  2. -3 -3
  3. -2 -2
  4. -3 -2

Answer: D. -3 -2

Explanation

floor rounds toward negative infinity (-3) and ceil rounds toward positive infinity (-2).

Step-by-step solution

  1. math.floor(-2.5) gives the largest whole number at or below -2.5, which is -3.
  2. math.ceil(-2.5) gives the smallest whole number at or above -2.5, which is -2.
  3. So the output is -3 -2, the correct answer.

Why the other options are wrong

  • A. -2 -3 is wrong; floor goes down to -3 and ceil goes up to -2.
  • B. -3 -3 is wrong; ceil(-2.5) is -2.
  • C. -2 -2 is wrong; floor(-2.5) is -3.

Tip: Floor goes down toward negative infinity, and ceil goes up toward positive infinity.

Question 7Hard

What is the output?

from functools import reduce
print(reduce(lambda a, b: a * b, [1, 2, 3, 4], 10))
  1. 24
  2. 34
  3. 10
  4. 240

Answer: D. 240

Explanation

With an initial value of 10, reduce computes ((((10 * 1) * 2) * 3) * 4) = 240.

Step-by-step solution

  1. reduce starts with initial value 10.
  2. 10x1=10, x2=20, x3=60, x4=240.

Why the other options are wrong

  • A. Ignores the initial value 10.
  • B. This adds instead of multiplying.
  • C. 10 is just the start.

Practise all 7 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Python topics · All topics