KEMBAR78
CH 8 - Random Module | PDF
0% found this document useful (0 votes)
5 views4 pages

CH 8 - Random Module

Uploaded by

themaskeddude58
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

CH 8 - Random Module

Uploaded by

themaskeddude58
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Chapter 8 – RANDOM MODULE

Type B – APPLICATION BASED QUESTIONS

1. When this program is run, the following output is generated (note that input
entered by the user is shown in bold):

Enter the length of the first side: 3


Enter the length of the second side: 4
Traceback (most recent call last):
h = sqrt(a * a + b * b)
NameError: name 'sqrt' is not defined

Why is this error occurring? How would you resolve it?

Reason - Because sqrt() function of math module. But in this program math
module is not import that’s why this give an error. If we import math module
then it will give no error.

2. After adding import math to the code given above, what other change(s) are
required in the code to make it fully work?

We have to call math module in program like this:-

Correct program:-

import math

a = float(input("Enter the length of the first side:"))

b = float(input("Enter the length of the second side:"))

h = math.sqrt(a * a + b * b)

print("The length of the hypotenuse is", h)

1
3. Consider the code given below:
import random
r = random.randrange (100, 999, 5)
print(r, end = ' ')
r = random.randrange(100, 999, 5)
print(r, end = ' ')
r = random.randrange (100, 999, 5)
print(r)

Which of the following are the possible outcomes of the above code? Also, what can
be the maximum and minimum number generated by line 2?
(a) 655, 705, 220 (b) 380, 382, 505 (c) 100, 500, 999 (d) 345, 650, 110

Answer - Possible outcomes of the above code is option (a) & (d).
Minimum value:- 100. Maximum value:- 995

4. Consider the code given below:

import random
r = random.randint (10, 100) - 10
print(r, end = ' ')
r = random.randint (10, 100) - 10
print(r, end = ' ')
r = random.randint (10, 100) - 10
print(r)
Which of the following are the possible outcomes of the above code? Also, what
can be the maximum and minimum number generated by line 2?
(a) 12 45 22 (b) 100 80 84 (c) 101 12 43 (d) 100 12 10

Answer - Option (a) the possible outcomes of the above code.


Max value:- 90. Min value :- 0

2
5. Consider the code given below:

import random
r= random.random() * 10
print(r, end = ' ')
r = random.random() * 10
print(r, end ='')
r = random.random() * 10
print(r)

Which of the following are the possible outcomes of the above code? Also, what
can be the maximum and minimum number generated by line 2?

(a) 0.5 1.6 9.8 (b) 10.0 1.0 0.0 (c) 0.0 5.6 8.7 (d) 0.0 7.9 10.0

Answer
Option (a) & (c) are the possible outcomes of the above code.
Max value: - nearly equal to 10, but not 10.
Min value:- 0.0

6. Consider the code given below:

import statistics as st
v = [7, 8, 8, 11, 7, 7]
m1 = st.mean(v)
m2 = st.mode(v)
m3 = st.median(v)
print(m1, m2, m3)

Which of the following is the correct output of the above code?

(a) 7 8 7.5 (b) 8 7 7 (c) 8 7 7.5 (d) 8.5 7 7.5

Answer - Option (c) is correct.

3
7. Predict the Output
import random
SIDES = ('EAST','WEST','NORTH','SOUTH')
N = random.randint(1,3)
OUT=""
for x in range (N,1,-1):
OUT=OUT+SIDES[x]
print(OUT)

8. Predict the Output


import random
VALUES = [10, 20, 30, 40, 50, 60, 70, 80]
BEGIN = random.randint(1,3)
LAST = random.randint(BEGIN, 4)
for x in range(BEGIN, LAST+1):
print(VALUES[x], end = "-")

You might also like