Test Series - python

Test Number 83/108

Q: 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
Solution: 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.
Q: The output of the following Python code is either 1 or 2.

import random
random.randint(1,2)
A. True
B. False
C. none
D. ..
Solution: 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’.
Q: 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
Solution: 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]).
Q: 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
Solution: 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.
Q: 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
Solution: 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’.
Q: 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
Solution: The function random.randint() does not accept a decimal value as a parameter. Hence the function shown above will throw an error.
Q: Which of the following functions helps us to randomize the items of a list?
A. seed
B. randomise
C. shuffle
D. uniform
Solution: 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.
Q: 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
Solution: 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.
Q: 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)
Solution: The function random.random() generates a random value in the interval [0,1), that is, including zero but excluding one.

You Have Score    /9