About admin

I am a Computer Science teacher in one of the renowned schools in India. I teach Java programming language to my students, and I maintain a website happycompiler.com

Sample Questions on randint() function in Python

Go through the Python code snippets given below, and find out the possible output(s) from the suggested options. Also write the least value and the highest value generated by the programs.

import random as r
Str = "HEMSH@@LADGP"
Num = r.randint(0, 3)
N = len(Str) - 1
while Str[N] != 'L':
    print(Str[N] + Str[Num] + "#", end = "")
    Num = Num - 1
    N = N - 1

(a) GM#GE#DH#AP#
(b) PS#GM#DE#AH#
(c) P@#GM#D@#AH#
(d) @H#GP#DG#AD#

import random as r
City = ["DEL", "CHN", "KOL", "BOM", "BNG"]
Fly = 0
for i in range(0, 3):
    Fly = r.randint(0, 1) + 1
    print(City[Fly], ":", end = "")

(a) DEL:CHN:KOL:
(b) CHN:KOL:CHN:
(c) KOL:BOM:BNG:
(d) KOL:CHN:KOL:

import random as r
Arr = [5, 8, 2]
Chance = r.randint(1, 2) + 12
for c in range(0, 3):
    N = r.randint(0, 2)
    print(Arr[N] + Chance, "@", end = "")

(a) 15 @18 @15 @
(b) 19 @19 @24 @
(c) 22 @16 @19 @
(d) 16 @22 @15 @

import random as r
val = 35
P = 7
Num = 0
for i in range(1, 5):
    Num = val + r.randint(0, P - 1)
    print(Num, " $ ", end = "")
    P = P - 1

(a) 41 $ 38 $ 38 $ 37 $
(b) 38 $ 40 $ 37 $ 34 $
(c) 36 $ 35 $ 42 $ 37 $
(d) 40 $ 37 $ 39 $ 35 $

import random as r
t =("Book", 7, "5G", "HSMS")
for i in range(0, 4):
    x =r.randint(0, 2) + 1
    print(t[x], "@", end = "")

(a) Book @5G @7 @HSMS @
(b) HSMS @Book @Book@5G @
(c) HSMS @5G @5G @7 @
(d) 7 @HSMS @HSMS @Book @