Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Practice short problems on Python Random Module

The document contains a series of Python coding problems focused on the Random module, providing examples of possible outputs and identifying correct and incorrect options. It discusses outcomes based on random selections and iterations over lists, along with specifying maximum and minimum values for certain variables. Each problem is followed by an answer indicating the correct or incorrect outcomes.

Uploaded by

iamyashvi027
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Practice short problems on Python Random Module

The document contains a series of Python coding problems focused on the Random module, providing examples of possible outputs and identifying correct and incorrect options. It discusses outcomes based on random selections and iterations over lists, along with specifying maximum and minimum values for certain variables. Each problem is followed by an answer indicating the correct or incorrect outcomes.

Uploaded by

iamyashvi027
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Practice short problems on Python Random

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

(i) (ii) (iii) (iv)

DELHIDELHI DELHI DELHI DELHI


MUMBAIMUMBAI DELHIMUMBAI MUMBAI MUMBAIMUMBAI
CHENNAICHENNAI DELHIMUMBAICHENNAI CHENNAI KOLKATAKOLKATAKOLKATA
KOLKATAKOLKATA KOLKATA
print ()

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
(i) (ii) (iii) (iv)

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)
1. Finding Nemo or Harry Potter
2. Harry Potter or Toy Story
3. Frozen or The Jungle Book
4. 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 )

1. [‘Sky blue’, ‘Green’, ‘Yellow’, ‘Pink’, ‘Red’]


2. [‘Blue’, ‘Green’, ‘Pink’, ’Brown’, ‘Red’, ‘Black’]
3. [Yellow, Sky blue, Green, Pink, Red]
4. (‘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) )

1. 57 and i
2. 62 and S
3. 20 and p
4. 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
(i) (ii) (iii) (iv)

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 )

1. ( CmodlnpgiSyi )
2. miSiypgoCdln
3. ‘lCSgpimiondy’
4. 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)

1. 2 and 7
2. 3 and 4
3. 8 and 10
4. 8 and 9
Ans. (ii) is correct

You might also like