Practice short problems on Python Random Module
- Categories Python, Python Modules
Python Random Module Examples:
1.What are the possible outcome(s) from the following code?
import random PICK=random.randint (1,3) CITY= ["DELHI", "MUMBAI", "CHENNAI", "KOLKATA"] for I in CITY: for J in range (0, PICK): print (I, end = "") print ()
(i) | (ii) | (iii) | (iv) |
---|---|---|---|
DELHIDELHI | DELHI | DELHI | DELHI |
MUMBAIMUMBAI | DELHIMUMBAI | MUMBAI | MUMBAIMUMBAI |
CHENNAICHENNAI | DELHIMUMBAICHENNAI | CHENNAI | KOLKATAKOLKATAKOLKATA |
KOLKATAKOLKATA | KOLKATA |
Ans. Option (i) and (iii) are possible
2.What are the possible outcome(s) from the following code? Also, specify the maximum and minimum values that can be assigned to variable SEL.
import random
SEL=random. randint (1, 3)
ANIMAL = ["DEER", "Monkey", "COW", "Kangaroo"]
for A in ANIMAL:
for AA in range (0, SEL):
print (A, end ="")
print ()
(i) | (ii) | (iii) | (iv) |
---|---|---|---|
DEERDEER | DEER | DEER | DEER |
MONKEYMONKEY | DELHIMONKEY | MONKEY | MONKEYMONKEY |
COWCOW | DELHIMONKEYCOW | COW | KANGAROOKANGAROOKANGAROO |
KANGAROOKANGAROO | KANGAROO |
Ans. Maximum value of SEL is 3 and minimum is 1
(iv) is the correct option.
3.What are the possible outcome(s) executed from the following code ?
import random
def main():
p = "MY PROGRAM"
i = 0
while p[i] != "R":
l = random.randint(0,3) + 5
print (p[l],end ="_")
i += 1
main()
(i) | (ii) | (iii) | (iv) |
---|---|---|---|
M_M_Y_P | R_G_A_G | G_G_R_O | O_G_G_A |
Ans. (i) will not be printed
4.Which is the incorrect outcome executed from the following code? Also, specify the maximum and minimum values that can be assigned to variable
x = 3
N = random.randint (1, x)
for i in range (N):
print (i, "#", i + 1)
(i) | (ii) | (iii) | (iv) |
---|---|---|---|
0#1 | 0#1 | 0#1 | 0#1 |
1#2 | 1#2 | 1#2 | |
2#3 | 2#3 | ||
3#4 |
Ans. (iii) will not be printed
The maximum value of N is 3 and minimum is 1
5.Which is the incorrect outcome executed from the following code?
import random
movie_list = ['The Jungle Book', 'Finding Nemo', 'Harry Potter', 'The Incredibles', 'Frozen']
movie1 = random.choice(movie_list)
movie2 = random.choice(movie_list)
print ( movie1 ," or " ,movie2)
- Finding Nemo or Harry Potter
- Harry Potter or Toy Story
- Frozen or The Jungle Book
- Toy Story or Spider Man
Ans. (ii) and (iv) are incorrect option.
6.What are the possible outcome(s) executed from the following code ?
import random
color_list = ["Pink", "Red", "Sky blue", "Yellow", "Green"]
random.shuffle(color_list)
print ( color_list )
- [‘Sky blue’, ‘Green’, ‘Yellow’, ‘Pink’, ‘Red’]
- [‘Blue’, ‘Green’, ‘Pink’, ’Brown’, ‘Red’, ‘Black’]
- [Yellow, Sky blue, Green, Pink, Red]
- (‘Yellow’, ‘Red’, ‘Sky blue’, ‘Green’, ‘Pink’)
Ans. (i) , (iv) is a correct output
7.Which is the incorrect outcome(s) executed from the following code?
import random
list=[14, 62, 30, 57, 91]
str= ('Simply')
print ( random.choice(list),"and" , random.choice(str) )
- 57 and i
- 62 and S
- 20 and p
- 30 and M
Ans. (iii) and (iv) are incorrect options
8.What are the possible outcome(s) executed from the following code ? Also specify the maximum and minimum values that can be assigned to variable PICKER.
import random
N=3
PICKER = random.randint (1, N)
COLOR = ["BLUE", "PINK", "GREEN", "RED"]
for I in COLOR :
for J in range (0, PICKER):
print (I, end = " ")
print ()
(i) | (ii) | (iii) | (iv) |
---|---|---|---|
BLUE | BLUE | PINK | BLUEBLUE |
PINK | BLUEPINK | PINKGREEN | PINKPINK |
GREEN | BLUEPINKGREEN | GREENRED | GREENGREEN |
RED | BLUEPINKGREENRED | GREENGREEN |
Ans. Option (i) and (iv) are possible
The maximum value of PICKER is 3 and minimum is 1
9.What are the possible outcome(s) for “Jumble String”, when following code will be executed?
import random
stringN = "SimplyCoding"
char_list = list(stringN)
random.shuffle(char_list)
stringN = ''.join(char_list)
print ( stringN )
- ( CmodlnpgiSyi )
- miSiypgoCdln
- ‘lCSgpimiondy’
- kpCyodigntS
Ans. (ii) will be printed
10.Which is the correct outcome executed from the following code?
import random
n1= random.randrange(1, 10, 2)
n2= random.randrange(1, 10, 3)
print (n1,"and",n2)
- 2 and 7
- 3 and 4
- 8 and 10
- 8 and 9
Ans. (ii) is correct
You may also like
Crack CBSE Python Questions on Tokens
24 April, 2020
Python Problems on Strings
24 April, 2020
CBSE Python Questions on Operators
24 April, 2020