Consider the following Python program that generates and prints random numbers…

2026

Consider the following Python program that generates and prints random numbers using different functions from the random module.

import random
X = random.randint(2, 4)
Y = random.randrange(4, 12, 2)
Z = 1 + int(3 * random.random())

for i in range(X, Y, Z):
    print(i, end='#')

Answer the following questions based on the given program.

(A) What are the minimum and maximum possible values that can be assigned to X?

(B) What is the maximum possible value that can be assigned to Y?

(C) What are the possible values that can be assigned to Z?

(D) Which of the following cannot be a possible output of the program?

(1) 2#5# (2) 2#4# (3) 1#2#3# (4) 4#5#6#7#

Show answer & explanation

(A) Minimum and Maximum values of X:

  • random.randint(2, 4) returns values including both limits.

  • Minimum = 2, Maximum = 4

(B) Maximum value of Y:

  • random.randrange(4, 12, 2) generates: 4, 6, 8, 10

  • Maximum value = 10

(C) Possible values of Z:

  • random.random() → [0,1)

  • int(3 * random.random()) → 0, 1, 2

  • Therefore, Z = 1 + (0,1,2) → 1, 2, 3

(D) Output Analysis:
The loop range(X, Y, Z) generates values starting from X, incremented by Z, and stops before Y.

  • 2#5# → Possible (e.g., X=2, Y=6, Z=3)

  • 2#4# → Possible (e.g., X=2,Y=6,Z=2)

  • 1#2#3# → Not Possible (X ≥ 2, so 1 cannot appear)

  • 4#5#6#7# → Possible (e.g., X=4, Y=8, Z=1)

Explore the full course: Hpsc Pgt Computer Science