Question 1Easy
What is the purpose of the check if __name__ == "__main__": in a module?
- It runs the block only when the file is executed directly
- It makes the module importable from other packages
- It is required for any Python script to run at all
- 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
- When a file is run directly, Python sets
__name__ to '__main__'. - When the file is imported,
__name__ is the module's name, so the block is skipped. - 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 3Easy
What is the output?
from collections import defaultdict
d = defaultdict(int)
for c in "aab":
d[c] += 1
print(dict(d))
- {'a': 1, 'b': 1}
- {'a': 2, 'b': 1}
- KeyError
- {'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
- defaultdict(int) gives 0 for missing keys.
- 'a' counted twice, 'b' once.
- 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, 3, 6, 10]
- [10]
- [0, 1, 3, 6]
- [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
accumulate gives running sums.- 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}))
- {"ok": "true", "n": "null"}
- {'ok': True, 'n': None}
- {"ok": true, "n": null}
- {"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
- json.dumps turns a Python dict into JSON text.
- True becomes the JSON word true, and None becomes null.
- 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))
- -2 -3
- -3 -3
- -2 -2
- -3 -2
Answer: D. -3 -2
Explanation
floor rounds toward negative infinity (-3) and ceil rounds toward positive infinity (-2).
Step-by-step solution
- math.floor(-2.5) gives the largest whole number at or below -2.5, which is -3.
- math.ceil(-2.5) gives the smallest whole number at or above -2.5, which is -2.
- 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))
- 24
- 34
- 10
- 240
Answer: D. 240
Explanation
With an initial value of 10, reduce computes ((((10 * 1) * 2) * 3) * 4) = 240.
Step-by-step solution
reduce starts with initial value 10.- 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.