Exit Chapter 13 · 🧰 Python Modules — Quiz

Playing as a guest? to earn XP and track your progress.

Question 1 of 52 0 correct so far

🧰 Python Modules Quiz

Q1.

What is a module in Python?

Explanation

A module is a .py file whose definitions can be reused in other programs through import.

Q2.

Which is NOT an advantage of using modules?

Explanation

Modules give reusability, modularity, a separate namespace and time savings, but programs still need testing.

Q3.

Which of these is a third-party module that must be installed using pip?

Explanation

math, random and statistics are built into the standard library; numpy, pandas and matplotlib are installed with pip.

Q4.

A .py file you write yourself and import is called a:

Explanation

User-defined modules are .py files written by you, such as mytools.py.

Q5.

After 'import math', how do you call the square root function?

Explanation

With import math, functions are called with dot notation: module name, a dot, then the function name.

Q6.

What does 'import statistics as st' do?

Explanation

as gives the module an alias, so you can write st.mean(...).

Q7.

After 'from math import sqrt, pi', what does print(floor(2.5)) give?

Explanation

Only sqrt and pi were imported; floor is not available.

Q8.

After 'import math', what does sqrt(9) give?

Explanation

With import math you must write math.sqrt(9).

Q9.

After 'from math import sqrt', what does math.sqrt(9) give?

Explanation

Only the name sqrt was imported, not the name math; call sqrt(9) directly.

Q10.

Which import style carries the HIGHEST risk of name clashes?

Explanation

from math import * brings in every name, which may overwrite your own names.

Q11.

What does dir(math) do?

Explanation

dir() lists the names in a module; help() describes a function.

Q12.

What is the value of math.pi?

Explanation

math.pi is π to about 15 decimal places; math.e is 2.718281828459045.

Q13.

What does math.sqrt(49) return?

Explanation

math.sqrt() always returns a float.

Q14.

What does print(math.ceil(3.01), math.floor(3.99)) display?

Explanation

ceil rounds UP to 4; floor rounds DOWN to 3.

Q15.

What are math.ceil(-3.7) and math.floor(-3.7)?

Explanation

ceil goes up to -3; floor goes down to -4.

Q16.

What are math.ceil(-0.5) and math.floor(0.5)?

Explanation

ceil(-0.5) rounds up to 0, and floor(0.5) rounds down to 0.

Q17.

What does print(2 ** 5, math.pow(2, 5)) display?

Explanation

** with integers gives an int, but math.pow() always returns a float.

Q18.

What does print(abs(-7), math.fabs(-7)) display?

Explanation

The built-in abs() keeps the type (int), while math.fabs() always returns a float.

Q19.

What does math.fabs(-12) return?

Explanation

fabs() returns the absolute value as a float.

Q20.

What does math.pow(4, 0.5) return?

Explanation

4 to the power 0.5 is the square root of 4, returned as a float: 2.0.

Q21.

What does math.sqrt(math.pow(3, 2) + math.pow(4, 2)) return?

Explanation

9.0 + 16.0 = 25.0, and its square root is 5.0.

Q22.

What does print(math.sqrt(64) + math.pow(2, 3)) display?

Explanation

8.0 + 8.0 = 16.0.

Q23.

In which unit do math.sin(), math.cos() and math.tan() take their angle?

Explanation

Trigonometric functions use radians; convert with math.radians().

Q24.

Which expression correctly gives the sine of 90 degrees (1.0)?

Explanation

math.sin(90) would treat 90 as radians; convert the angle first.

Q25.

What does math.sqrt(-4) give?

Explanation

The square root of a negative number is outside the domain of math.sqrt().

Q26.

What is the range of values returned by random.random()?

Explanation

random() returns a float from 0.0 up to, but never including, 1.0.

Q27.

What values can random.randint(1, 6) return?

Explanation

randint() includes BOTH ends, like a dice roll.

Q28.

What values can random.randrange(1, 6) return?

Explanation

randrange() behaves like range(): the stop value is excluded.

Q29.

What values can random.randrange(5) return?

Explanation

With one argument, randrange() gives 0 ≤ n < stop.

Q30.

Which values can random.randrange(0, 20, 5) return?

Explanation

It picks a value from range(0, 20, 5); 20 is excluded.

Q31.

Can random.randrange(10, 100, 10) ever return 100?

Explanation

Like range(), randrange never includes the stop value.

Q32.

Which statement comparing randint() and randrange() is correct?

Explanation

randint(1, 10) gives 1 to 10; randrange(1, 10) gives 1 to 9.

Q33.

What range of values does n = int(random.random() * 11) + 10 produce?

Explanation

random() * 11 gives 0.0 to 10.999…; int() gives 0 to 10; adding 10 gives 10 to 20.

Q34.

What are the minimum and maximum values of x = random.randrange(5, 50, 5) + 1?

Explanation

randrange gives 5, 10, …, 45; adding 1 gives a minimum of 6 and a maximum of 46.

Q35.

Which statement generates a random 4-digit OTP?

Explanation

randint(1000, 9999) gives integers from 1000 to 9999, all of which have 4 digits.

Q36.

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="#")

Explanation

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.

Q37.

In the same AR program, what are the maximum values of FROM and TO?

Explanation

randint includes both ends: FROM goes up to 3 and TO up to 4.

Q38.

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="-")

Explanation

N can be 1, 2 or 3, and the output always starts with RED. N = 3 gives RED-GREEN-BLUE-; N = 4 is impossible.

Q39.

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=" ")

Explanation

start is at most 2, so the last possible pair is L[2], L[3] = 30 40; 40 50 would need start = 3.

Q40.

What does statistics.mean([2, 4, 6, 8]) return?

Explanation

The mean (average) is the sum divided by the count: 20 / 4 = 5.

Q41.

What does statistics.median([7, 1, 5]) return?

Explanation

The median is the middle of the SORTED data: [1, 5, 7] gives 5.

Q42.

What does statistics.median([9, 3, 7, 1]) return?

Explanation

Sorted: [1, 3, 7, 9]; for an even count, the median is the average of the two middle values, (3 + 7) / 2 = 5.0.

Q43.

What does statistics.mode([4, 2, 4, 6, 2, 4]) return?

Explanation

The mode is the most frequent value; 4 occurs three times.

Q44.

For marks = [72, 85, 90, 85, 60, 78], what are the median and mode?

Explanation

Sorted: [60, 72, 78, 85, 85, 90]; the median is (78 + 85) / 2 = 81.5, and 85 occurs most often.

Q45.

What does statistics.mode(["red", "blue", "red"]) return?

Explanation

mode() works on strings too; 'red' is the most frequent value.

Q46.

A student says the median of [7, 1, 5] is 1 because it is the middle element. What is wrong?

Explanation

The median is the middle of the sorted data, not of the list as given.

Q47.

What does statistics.mean([]) give?

Explanation

There is no average of an empty list, so a StatisticsError is raised.

Q48.

Which module must be imported to use mode()?

Explanation

mean(), median() and mode() belong to the statistics module.

Q49.

geometry.py defines rect_area(l, b), which returns l * b. After 'import geometry', what does geometry.rect_area(4, 5) return?

Explanation

A user-defined module is used like any other; rect_area returns 4 × 5 = 20.

Q50.

For a user-defined module to be imported with 'import geometry', where should geometry.py be saved?

Explanation

In the notes' example, main.py is saved in the same folder as geometry.py.

Q51.

For a = 1, b = -5, c = 6, what does the quadratic-roots program print?

Explanation

d = 25 - 24 = 1 > 0; the roots are (5 + 1) / 2 = 3.0 and (5 - 1) / 2 = 2.0.

Q52.

What does the circle program print as the area for radius 7 (rounded to 2 decimal places)?

Explanation

π × 7² ≈ 153.94; the circumference, 2πr, is ≈ 43.98.