Playing as a guest? to earn XP and track your progress.
🧰 Python Modules Quiz
🧰 Python Modules
What is a module in Python?
A hardware component
A Python file (.py) containing ready-made definitions — functions, constants, classes — that can be reused by importing it
A keyword
A type of loop
A module is a .py file whose definitions can be reused in other programs through import.
Which is NOT an advantage of using modules?
Names inside a module don't clash with names in your program
Reusability
Modularity
Programs no longer need to be tested
Modules give reusability, modularity, a separate namespace and time savings, but programs still need testing.
Which of these is a third-party module that must be installed using pip?
math
random
numpy
statistics
math, random and statistics are built into the standard library; numpy, pandas and matplotlib are installed with pip.
A .py file you write yourself and import is called a:
User-defined module
Keyword
Built-in module
Third-party package
User-defined modules are .py files written by you, such as mytools.py.
After 'import math', how do you call the square root function?
sqrt(16)
math.sqrt(16)
math->sqrt(16)
import.sqrt(16)
With import math, functions are called with dot notation: module name, a dot, then the function name.
What does 'import statistics as st' do?
Renames the file
Imports only the mean() function
Imports all names directly
Imports the statistics module with the short alias st
as gives the module an alias, so you can write st.mean(...).
After 'from math import sqrt, pi', what does print(floor(2.5)) give?
3
2
NameError — floor was not imported
2.0
Only sqrt and pi were imported; floor is not available.
After 'import math', what does sqrt(9) give?
NameError: name 'sqrt' is not defined
3.0
9
3
With import math you must write math.sqrt(9).
After 'from math import sqrt', what does math.sqrt(9) give?
ModuleNotFoundError
NameError: name 'math' is not defined
3.0
3
Only the name sqrt was imported, not the name math; call sqrt(9) directly.
Which import style carries the HIGHEST risk of name clashes?
import math as m
from math import sqrt
import math
from math import *
from math import * brings in every name, which may overwrite your own names.
What does dir(math) do?
Deletes the math module
Shows the folder of the module
Lists all the names inside the math module
Runs the math module
dir() lists the names in a module; help() describes a function.
What is the value of math.pi?
3.141592653589793
22/7
3.14
2.718281828459045
math.pi is π to about 15 decimal places; math.e is 2.718281828459045.
What does math.sqrt(49) return?
7
7.0
Error
49
math.sqrt() always returns a float.
What does print(math.ceil(3.01), math.floor(3.99)) display?
3 4
4 4
3 3
4 3
ceil rounds UP to 4; floor rounds DOWN to 3.
What are math.ceil(-3.7) and math.floor(-3.7)?
-4 and -3
-3 and -3
-3 and -4
-4 and -4
ceil goes up to -3; floor goes down to -4.
What are math.ceil(-0.5) and math.floor(0.5)?
0 and 0
0 and 1
-1 and 0
-1 and 1
ceil(-0.5) rounds up to 0, and floor(0.5) rounds down to 0.
What does print(2 ** 5, math.pow(2, 5)) display?
32 32
32 32.0
32.0 32.0
10 10.0
** with integers gives an int, but math.pow() always returns a float.
What does print(abs(-7), math.fabs(-7)) display?
7 7
-7 7.0
7.0 7.0
7 7.0
The built-in abs() keeps the type (int), while math.fabs() always returns a float.
What does math.fabs(-12) return?
12
-12.0
12.0
Error
fabs() returns the absolute value as a float.
What does math.pow(4, 0.5) return?
2.0
2
8.0
16.0
4 to the power 0.5 is the square root of 4, returned as a float: 2.0.
What does math.sqrt(math.pow(3, 2) + math.pow(4, 2)) return?
5
5.0
25.0
7.0
9.0 + 16.0 = 25.0, and its square root is 5.0.
What does print(math.sqrt(64) + math.pow(2, 3)) display?
16
72.0
14.0
16.0
8.0 + 8.0 = 16.0.
In which unit do math.sin(), math.cos() and math.tan() take their angle?
Gradians
Percent
Radians
Degrees
Trigonometric functions use radians; convert with math.radians().
Which expression correctly gives the sine of 90 degrees (1.0)?
math.sin(math.radians(90))
math.sin(90, 'deg')
math.sin(90)
math.radians(math.sin(90))
math.sin(90) would treat 90 as radians; convert the angle first.
What does math.sqrt(-4) give?
-2.0
ValueError: math domain error
2.0
0
The square root of a negative number is outside the domain of math.sqrt().
What is the range of values returned by random.random()?
0.0 to 1.0, both included
0 to 100
1 to 10
0.0 ≤ n < 1.0
random() returns a float from 0.0 up to, but never including, 1.0.
What values can random.randint(1, 6) return?
0 to 5
0 to 6
1 to 6
1 to 5
randint() includes BOTH ends, like a dice roll.
What values can random.randrange(1, 6) return?
1 to 5
0 to 6
0 to 5
1 to 6
randrange() behaves like range(): the stop value is excluded.
What values can random.randrange(5) return?
1 to 5
0 to 4
0 to 5
5 only
With one argument, randrange() gives 0 ≤ n < stop.
Which values can random.randrange(0, 20, 5) return?
0, 5, 10, 15, 20
5, 10, 15, 20
Any number from 0 to 20
0, 5, 10, 15
It picks a value from range(0, 20, 5); 20 is excluded.
Can random.randrange(10, 100, 10) ever return 100?
Only if seeded
Yes
No — the stop value is excluded; the maximum is 90
Only rarely
Like range(), randrange never includes the stop value.
Which statement comparing randint() and randrange() is correct?
randint includes the upper limit and takes exactly 2 arguments; randrange excludes stop and allows a step
randrange includes stop
randint excludes the upper limit
Both allow a step
randint(1, 10) gives 1 to 10; randrange(1, 10) gives 1 to 9.
What range of values does n = int(random.random() * 11) + 10 produce?
11 to 20
10 to 20
10 to 21
0 to 10
random() * 11 gives 0.0 to 10.999…; int() gives 0 to 10; adding 10 gives 10 to 20.
What are the minimum and maximum values of x = random.randrange(5, 50, 5) + 1?
5 and 45
5 and 50
6 and 51
6 and 46
randrange gives 5, 10, …, 45; adding 1 gives a minimum of 6 and a maximum of 46.
Which statement generates a random 4-digit OTP?
random.random(4)
random.randint(0, 9999)
random.randint(1000, 9999)
random.randrange(1000, 9999, 0)
randint(1000, 9999) gives integers from 1000 to 9999, all of which have 4 digits.
For this code, which output is POSSIBLE?
AR = [20, 30, 40, 50, 60, 70] FROM = random.randint(1, 3) TO = random.randint(2, 4) for K in range(FROM, TO + 1): print(AR[K], end="#")
30#40#50#
50#60#70#
40#50#70#
10#40#70#
FROM = 1 and TO = 3 print indices 1-3 (30, 40, 50). Index 5 (70) can never be printed, since TO is at most 4.
In the same AR program, what are the maximum values of FROM and TO?
FROM = 1, TO = 2
FROM = 3, TO = 4
FROM = 3, TO = 5
FROM = 4, TO = 5
randint includes both ends: FROM goes up to 3 and TO up to 4.
For this code, which output is possible?
COLOURS = ["RED", "GREEN", "BLUE", "PINK", "GOLD"] N = random.randrange(1, 4) for i in range(N): print(COLOURS[i], end="-")
RED-GREEN-BLUE-PINK-
GREEN-BLUE-
BLUE-
RED-GREEN-BLUE-
N can be 1, 2 or 3, and the output always starts with RED. N = 3 gives RED-GREEN-BLUE-; N = 4 is impossible.
For this code, which output is NOT possible?
L = [10, 20, 30, 40, 50] start = random.randint(0, 2) for i in range(start, start + 2): print(L[i], end=" ")
30 40
10 20
40 50
20 30
start is at most 2, so the last possible pair is L[2], L[3] = 30 40; 40 50 would need start = 3.
What does statistics.mean([2, 4, 6, 8]) return?
5
6
20
4
The mean (average) is the sum divided by the count: 20 / 4 = 5.
What does statistics.median([7, 1, 5]) return?
4.33
5
7
1
The median is the middle of the SORTED data: [1, 5, 7] gives 5.
What does statistics.median([9, 3, 7, 1]) return?
3
5.5
7
5.0
Sorted: [1, 3, 7, 9]; for an even count, the median is the average of the two middle values, (3 + 7) / 2 = 5.0.
What does statistics.mode([4, 2, 4, 6, 2, 4]) return?
3.67
2
4
6
The mode is the most frequent value; 4 occurs three times.
For marks = [72, 85, 90, 85, 60, 78], what are the median and mode?
81.5 and 85
85 and 85
81.5 and 90
78.33 and 85
Sorted: [60, 72, 78, 85, 85, 90]; the median is (78 + 85) / 2 = 81.5, and 85 occurs most often.
What does statistics.mode(["red", "blue", "red"]) return?
2
'red'
Error — mode works only on numbers
'blue'
mode() works on strings too; 'red' is the most frequent value.
A student says the median of [7, 1, 5] is 1 because it is the middle element. What is wrong?
Nothing
The median is the average, 4.33
The median is the largest value, 7
The data must be sorted first; the median is 5
The median is the middle of the sorted data, not of the list as given.
What does statistics.mean([]) give?
[]
None
StatisticsError
0
There is no average of an empty list, so a StatisticsError is raised.
Which module must be imported to use mode()?
statistics
math
random
mode
mean(), median() and mode() belong to the statistics module.
geometry.py defines rect_area(l, b), which returns l * b. After 'import geometry', what does geometry.rect_area(4, 5) return?
Error
20
9
45
A user-defined module is used like any other; rect_area returns 4 × 5 = 20.
For a user-defined module to be imported with 'import geometry', where should geometry.py be saved?
Inside the math module
In the Recycle Bin
Anywhere on the internet
In the same folder as the program that imports it
In the notes' example, main.py is saved in the same folder as geometry.py.
For a = 1, b = -5, c = 6, what does the quadratic-roots program print?
Real and equal roots: 2.5
Real and different roots: 2.0 3.0
Real and different roots: 3.0 2.0
Roots are imaginary
d = 25 - 24 = 1 > 0; the roots are (5 + 1) / 2 = 3.0 and (5 - 1) / 2 = 2.0.
What does the circle program print as the area for radius 7 (rounded to 2 decimal places)?
153.94
154
43.98
49.0
π × 7² ≈ 153.94; the circumference, 2πr, is ≈ 43.98.