LearnFrenzy
Sign in Take a free test

Modules and Packages · Exercise 2 of 4

Modules and Packages Predict the Output

  • 15 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each Python program and choose what it prints. Every program was run on Python 3, so the answers are the real output. Programs that need their own module create it in a temporary folder first.

Question 1 Easy

What is the output of the following Python program?

main.py
import math
print(math.sqrt(16), math.floor(-2.5), math.ceil(-2.5), math.trunc(-2.5), math.pi > 3.14)
A 4.0 -3 -2 -2 True
B 4 -2 -3 -2 True
C 4.0 -2 -2 -3 True
D 4.0 -3 -2 -3 False
Question page & discussion
Question 2 Easy

What is the output of the following Python program?

main.py
from math import factorial as fact, gcd
print(fact(5), gcd(12, 18))
print("math" in dir(), "fact" in dir(), "factorial" in dir())
A 120 6 True True True
B 120 6 False True False
C 120 6 True False True
D 24 6 False True False
Question page & discussion
Question 3 Easy

What is the output of the following Python program?

main.py
import math as m
print(m.__name__, hasattr(m, "tau"), round(m.tau, 2), type(m).__name__)
A m True 6.28 module
B math False 6.28 module
C math True 6.28 module
D math True 6.28 math
Question page & discussion
Question 4 Medium

What is the output of the following Python program?

main.py
import sys
import math
import math as again
print(again is math, "math" in sys.modules, sys.modules["math"] is math)
A False True False
B True False True
C False False False
D True True True
Question page & discussion
Question 5 Easy

This file is run directly. What does it print?

main.py
import json
print(json.__name__, __name__)
A __main__ __main__
B json json
C json main
D json __main__
Question page & discussion
Question 6 Medium

What is the output of the following Python program?

main.py
import math
print([n for n in dir(math) if n.startswith("is")])
A ['isclose', 'isfinite', 'isinf', 'isnan']
B ['isclose', 'isfinite', 'isinf', 'isnan', 'isqrt']
C ['isqrt', 'isnan', 'isinf', 'isfinite', 'isclose']
D []
Question page & discussion
Question 7 Medium

What is the output of the following Python program?

main.py
from collections import deque

recent = deque([1, 2, 3], maxlen=3)
recent.append(4)
recent.appendleft(0)
print(recent, len(recent))
A deque([2, 3, 4], maxlen=3) 3
B deque([0, 2, 3], maxlen=3) 3
C deque([0, 1, 2, 3, 4]) 5
D deque([0, 3, 4], maxlen=3) 3
Question page & discussion
Question 8 Medium

What is the output of the following Python program?

main.py
from itertools import permutations, combinations, product
print(len(list(permutations("abc"))), list(combinations([1, 2, 3], 2)), len(list(product("ab", repeat=3))))
A 6 [(1, 2), (1, 3), (2, 3)] 8
B 3 [(1, 2), (1, 3), (2, 3)] 6
C 6 [(1, 2), (2, 1), (1, 3)] 8
D 6 [(1, 2), (1, 3), (2, 3)] 6
Question page & discussion
Question 9 Medium

What is the output of the following Python program?

main.py
from datetime import date, timedelta

d = date(2026, 12, 30) + timedelta(days=5)
print(d, d.strftime("%d %b %Y"), d.weekday(), (d - date(2026, 1, 1)).days)
A 2026-12-35 35 Dec 2026 0 368
B 2027-01-04 04 Jan 2027 0 368
C 2027-01-04 04 Jan 2027 1 369
D 2027-01-04 4 January 2027 0 368
Question page & discussion
Question 10 Medium

What is the output of the following Python program?

main.py
import json

s = json.dumps({"b": 1, "a": [True, None]}, sort_keys=True)
print(s)
print(json.loads(s)["a"], type(json.loads("3.0")).__name__)
A {'a': [True, None], 'b': 1} [True, None] float
B {"b": 1, "a": [true, null]} [true, null] str
C {"a": [true, null], "b": 1} [True, None] float
D {"a": [true, null], "b": 1} ['true', 'null'] float
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links