LESSON 1 RANDOM OUTPUT
1. Study the following program and select the possible output(s) from the options (i) to (iv)
following it. Also, write the maximum and the minimum values that can be assigned to the variable
Y. (2)
import random
X= random.random()
Y= random.randint(0,4)
print(int(X),":",Y+int(X))
(i)0 : 0
(ii) 1 : 6
(iii)2 : 4
(iv) 0 : 3Ans.
(i) and (iv) are the possible outputs. Minimum value that can be assigned is – Y = 0. Maximum value
that can be assigned is – Y = 3
2. Observe the following program and answer the questions that follow:
What is the minimum and maximum number of times the loop will execute?
b. Find out, which line of output(s) out of (i) to (iv) will not be expected from the program?
i. 0#1
ii. 1#2
iii. 2#3
iv. 3#4
Answer: a. Minimum Number = 1
Maximum Number = 3
b. Line iv is not expected to be a part of the output.
3. Given the following code, which is repeated four times. What could be
the possible set of outputs out of the given four sets. (2)
import random
print(15+random.random()*5)
(i) 17.dddd, 19.dddd, 20.dddd, 15.dddd
(ii) 15.dddd, 17.dddd, 19.dddd, 18.dddd
(iii) 14.dddd, 16.dddd, 18.dddd, 20.dddd
(iv) 15.dddd, 15.dddd, 15.dddd, 15.dddd
Ans. Option (ii) and (iv) are correct outputs
random generates number N between range 0,0<=N<=1.0
when it is multiplied with 5, the range becomes 0.0 to 5
when 15 added to it, the range becomes 15 to 20
so only option (ii) & (iv) fulfill the condition of range between 15 to 20
Max value 3 and minimum value 1 for variable NUM
4. What are the possible outcome(s) executed from the following code? Also specify the maximum and
minimum values that can be assigned to variable N. import random
import random
NAV = ["LEFT","FRONT","RIGHT","BACK"]
NUM = random.randint(1,3)
NAVG = ""
for C in range(NUM,1,-1):
NAVG = NAVG+NAV[C]
print (NAVG)
(i) BACKRIGHT (ii) BACKRIGHTFRONT
(iii) BACK (iv) LEFTFRONTRIGHT
Answer (i) BACKRIGHT
Max value 3 and minimum value 1 for variable NUM
5.What possible outputs are expected to be displayed on the screen at the time of execution of the
program from the following code? Also specify the minimum and maximum values that can be assigned
to the variable c.
import random
temp=[10,20,30,40,50,60]
c=random.randint(0,4)
for I in range(0, c):
print(temp[i],”#”)
(i) 10#20# (ii) 10#20#30#40#50#
(iii). 10#20#30# (iv) 50#60# 2
Ans (i) & (iii) Minimum value of C is 0 and Maximum
value is 3