Python Question and Answers – Random module – 1

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Random module – 1”.

1. To include the use of functions which are present in the random library, we must use the option:
a) import random
b) random.h
c) import.random
d) random.random
View Answer

Answer: a
Explanation: The command import random is used to import the random module, which enables us to use the functions which are present in the random library.

2. The output of the following Python code is either 1 or 2.

import random
random.randint(1,2)

a) True
b) False
View Answer

Answer: a
Explanation: The function random.randint(a,b) helps us to generate an integer between ‘a’ and ‘b’, including ‘a’ and ‘b’. In this case, since there are no integers between 1 and 2, the output will necessarily be either 1 or 2’.

advertisement
advertisement

3. What will be the output of the following Python code?

import random
random.choice(2,3,4)

a) An integer other than 2, 3 and 4
b) Either 2, 3 or 4
c) Error
d) 3 only
View Answer

Answer: c
Explanation: The code shown above displays the incorrect syntax of the function random.choice(). This functions takes its numeric parameter in the form of a list. Hence the correct syntax world be: random.choice([2,3,4]).
advertisement

4. What will be the output of the following Python code?

import random
random.choice([10.4, 56.99, 76])

a) Error
b) Either 10.4, 56.99 or 76
c) Any number other than 10.4, 56.99 and 76
d) 56.99 only
View Answer

Answer: b
Explanation: The function random.choice(a,b,c,d) returns a random number which is selected from a, b, c and d. The output can be either a, b, c or d. Hence the output of the snippet of code shown above can be either 10.4, 56.99 or 76.
advertisement

5. What will be the output of the following Python function (random module has already been imported)?

random.choice('sun')

a) sun
b) u
c) either s, u or n
d) error
View Answer

Answer: c
Explanation: The above function works with alphabets just as it does with numbers. The output of this expression will be either s, u or n.

6. What will be the output of the following Python function, assuming that the random module has already been imported?

random.uniform(3,4)

a) Error
b) Either 3 or 4
c) Any integer other than 3 and 4
d) Any decimal value between 3 and 4
View Answer

Answer: d
Explanation: This question depicts the basic difference between the functions random.randint(a, b) and random.uniform(a, b). While random.randint(a,b) generates an integer between ‘a’ and ‘b’, including ‘a’ and ‘b’, the function random.uniform(a,b) generates a decimal value between ‘a’ and ‘b’.

7. What will be the output of the following Python function if the random module has already been imported?

random.randint(3.5,7)

a) Error
b) Any integer between 3.5 and 7, including 7
c) Any integer between 3.5 and 7, excluding 7
d) The integer closest to the mean of 3.5 and 7
View Answer

Answer: a
Explanation: The function random.randint() does not accept a decimal value as a parameter. Hence the function shown above will throw an error.

8. Which of the following functions helps us to randomize the items of a list?
a) seed
b) randomise
c) shuffle
d) uniform
View Answer

Answer: c
Explanation: The function shuffle, which is included in the random module, helps us to randomize the items of a list. This function takes the list as a parameter.

9. What will be the output of the following Python code?

random.seed(3)
random.randint(1,5)
2
random.seed(3)
random.randint(1,5)

a) 3
b) 2
c) Any integer between 1 and 5, including 1 and 5
d) Any integer between 1 and 5, excluding 1 and 5
View Answer

Answer: b
Explanation: We use the seed function when we want to use the same random number once again in our program. Hence the output of the code shown above will be 2, since 2 was generated previously following which we used the seed function.

10. What is the interval of the value generated by the function random.random(), assuming that the random module has already been imported?
a) (0,1)
b) (0,1]
c) [0,1]
d) [0,1)
View Answer

Answer: d
Explanation: The function random.random() generates a random value in the interval [0,1), that is, including zero but excluding one.

11. What will be the output of the following Python code?

random.randrange(0,91,5)

a) 10
b) 18
c) 79
d) 95
View Answer

Answer: a
Explanation: The function shown above will generate an output which is a multiple of 5 and is between 0 and 91. The only option which satisfies these criteria is 10. Hence the only possible output of this function is 10.

12. Both the functions randint and uniform accept ____________ parameters.
a) 0
b) 1
c) 3
d) 2
View Answer

Answer: d
Explanation: Both of these functions, that is, randint and uniform are included in the random module and both of these functions accept 2 parameters. For example: random.uniform(a,b) where ‘a’ and ‘b’ specify the range.

13. The randrange function returns only an integer value.
a) True
b) False
View Answer

Answer: a
Explanation: The function randrange returns only an integer value. Hence this statement is true.

14. What will be the output of the following Python code?

random.randrange(1,100,10)

a) 32
b) 67
c) 91
d) 80
View Answer

Answer: c
Explanation: The output of this function can be any value which is a multiple of 10, plus 1. Hence a value like 11, 21, 31, 41…91 can be the output. Also, the value should necessarily be between 1 and 100. The only option which satisfies this criteria is 91.

15. What will be the output of the following Python function, assuming that the random library has already been included?

random.shuffle[1,2,24]

a) Randomized list containing the same numbers in any order
b) The same list, that is [1,2,24]
c) A list containing any random numbers between 1 and 24
d) Error
View Answer

Answer: d
Explanation: The function shown above will result in an error because this is the incorrect syntax for the usage of the function shuffle(). The list should be previously declared and then passed to this function to get an output.
An example of the correct syntax:

>>> l=['a','b','c','d']
>>> random.shuffle(l)
>>> print(l)

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.