Python Random Module Quiz

Python
0 Passed
0% acceptance

A 35-question quiz covering random number generation, selecting elements, shuffling sequences, and managing seeds.

35 Questions
~70 minutes
1

Question 1

What is the primary function of the `random` module in Python?

A
To generate pseudo-random numbers and perform random selections.
B
To generate cryptographically secure keys.
C
To sort lists randomly.
D
To compress data.
2

Question 2

Analyze the code below. What is the possible range of values for `x`?

python
import random
x = random.randint(1, 10)
A
1 to 10 (inclusive)
B
1 to 9 (10 is exclusive)
C
0 to 10
D
0 to 9
3

Question 3

Which function returns a random floating-point number in the range [0.0, 1.0)?

A
random.random()
B
random.float()
C
random.uniform(0, 1)
D
random.rand()
4

Question 4

Analyze the code below. What happens to `my_list`?

python
import random
my_list = [1, 2, 3, 4]
random.shuffle(my_list)
A
The elements of `my_list` are reordered randomly in-place.
B
A new shuffled list is returned, leaving `my_list` unchanged.
C
It raises an error because lists are immutable.
D
It sorts the list.
5

Question 5

What is the purpose of `random.seed(a)`?

A
To initialize the random number generator so that it produces a reproducible sequence.
B
To generate a random file.
C
To make the random numbers more secure.
D
To speed up generation.
6

Question 6

Which function would you use to pick a single random element from a non-empty sequence?

A
random.choice()
B
random.pick()
C
random.select()
D
random.one()
7

Question 7

Analyze the code below. What is the difference between `randint(1, 10)` and `randrange(1, 10)`?

python
import random
a = random.randint(1, 10)
b = random.randrange(1, 10)
A
`randint` includes 10, while `randrange` excludes 10.
B
`randint` excludes 10, while `randrange` includes 10.
C
They are identical.
D
`randrange` returns a float.
8

Question 8

How do you select 3 unique elements from a list `population`?

A
random.sample(population, 3)
B
random.choices(population, k=3)
C
random.choose(population, 3)
D
random.pick(population, 3)
9

Question 9

Analyze the code below. What will be the output?

python
import random
random.seed(42)
print(random.random())
random.seed(42)
print(random.random())
A
The same number twice.
B
Two different numbers.
C
0.0 then 1.0.
D
Error.
10

Question 10

Which function returns a random float N such that `a <= N <= b`?

A
random.uniform(a, b)
B
random.float(a, b)
C
random.range(a, b)
D
random.between(a, b)
11

Question 11

What happens if you call `random.choice([])` (an empty list)?

A
IndexError: Cannot choose from an empty sequence
B
Returns None
C
Returns 0
D
ValueError
12

Question 12

Analyze the code below. What is the issue?

python
import random
t = (1, 2, 3)
random.shuffle(t)
A
TypeError: 'tuple' object does not support item assignment
B
It works and shuffles the tuple.
C
It returns a new shuffled tuple.
D
It converts the tuple to a list.
13

Question 13

Which function allows sampling *with* replacement (duplicates allowed)?

A
random.choices()
B
random.sample()
C
random.select()
D
random.poll()
14

Question 14

Analyze the code below. What does `k=5` specify?

python
random.choices(['red', 'black'], weights=[18, 18], k=5)
A
The number of elements to choose.
B
The step size.
C
The seed value.
D
The maximum value.
15

Question 15

Why should you NOT use the `random` module for generating passwords or tokens?

A
It is not cryptographically secure; values can be predicted if the state is known.
B
It is too slow.
C
It only generates numbers, not strings.
D
It requires an internet connection.
16

Question 16

What does `random.randrange(0, 10, 2)` generate?

A
A random even number from 0, 2, 4, 6, 8.
B
Any number from 0 to 10.
C
A random odd number.
D
A float.
17

Question 17

Analyze the code below. What is the output?

python
import random
print(random.randint(5, 5))
A
5
B
Error: Empty range
C
None
D
6
18

Question 18

Which distribution function returns a random number from a Gaussian (normal) distribution?

A
random.gauss(mu, sigma)
B
random.normal()
C
random.bell()
D
random.std()
19

Question 19

What happens if you use `random.sample()` with a `k` larger than the population size?

A
ValueError: Sample larger than population
B
It repeats elements.
C
It returns the whole population.
D
It returns None.
20

Question 20

Analyze the code below. What does `random.random() * 5` produce?

python
val = random.random() * 5
A
A float between 0.0 and 5.0 (exclusive of 5.0).
B
An integer between 0 and 5.
C
A float between 1.0 and 5.0.
D
A float between 0.0 and 1.0.
21

Question 21

How can you save and restore the state of the random number generator?

A
Using `random.getstate()` and `random.setstate()`.
B
Using `random.save()` and `random.load()`.
C
Using `pickle`.
D
You cannot save the state.
22

Question 22

What is the default seed if you don't call `random.seed()`?

A
The current system time.
B
0
C
1
D
None
23

Question 23

Analyze the code below. What is the return type?

python
import random
x = random.choice("Hello")
A
String (a single character)
B
List
C
Integer
D
None
24

Question 24

Which function would you use to simulate a weighted coin toss (e.g., 70% heads, 30% tails)?

A
random.choices(['H', 'T'], weights=[70, 30])
B
random.choice(['H', 'T'])
C
random.sample(['H', 'T'], 1)
D
random.coin(0.7)
25

Question 25

What does `random.triangular(low, high, mode)` do?

A
Returns a random float from a triangular distribution.
B
Returns a random triangle shape.
C
Returns 3 random numbers.
D
Calculates the area of a triangle.
26

Question 26

Analyze the code below. What is the output?

python
import random
print(random.randrange(10))
A
A random integer from 0 to 9.
B
A random integer from 1 to 10.
C
A random integer from 0 to 10.
D
10
27

Question 27

Is the `random` module thread-safe?

A
Yes, generally, but care is needed in high-concurrency.
B
No, it crashes in threads.
C
Only on Linux.
D
It cannot be used in threads.
28

Question 28

How do you generate a random integer with `k` bits?

A
random.getrandbits(k)
B
random.bits(k)
C
random.randint(0, 2**k)
D
random.binary(k)
29

Question 29

Analyze the code below. What is the result?

python
import random
lst = [1, 2, 3]
random.shuffle(lst)
print(lst)
A
A permutation of [1, 2, 3] (e.g., [2, 1, 3]).
B
None
C
[1, 2, 3]
D
Error
30

Question 30

Which method is used to instantiate a new random number generator isolated from the global one?

A
random.Random()
B
random.New()
C
random.Generator()
D
random.create()
31

Question 31

What is the range of `random.uniform(1.5, 1.5)`?

A
It always returns 1.5.
B
It raises a ValueError.
C
It returns 0.
D
It returns None.
32

Question 32

Analyze the code below. What is the output?

python
import random
random.seed("hello")
print(random.randint(1, 10))
A
A deterministic number (always the same for this seed).
B
Error: Seed must be an integer.
C
A completely random number every time.
D
None
33

Question 33

Which function would you use to shuffle a list and return a *new* list without modifying the original?

A
random.sample(lst, len(lst))
B
random.shuffle(lst)
C
random.permute(lst)
D
random.new_shuffle(lst)
34

Question 34

What is the underlying algorithm used by Python's `random` module?

A
Mersenne Twister
B
Linear Congruential Generator
C
True Randomness
D
Xorshift
35

Question 35

Analyze the code below. What is the result?

python
import random
print(random.choice({'a': 1, 'b': 2}))
A
KeyError or IndexError (dictionaries are not sequences).
B
A random key.
C
A random value.
D
A random item tuple.

QUIZZES IN Python