Question 272 marksShort AnswerSection B

What possible output(s) from the given options will NOT be displayed when the following code is executed ? Also, mention, for how many iterations the for loop in the given code will run ?

``python

import random

a = [1,2,3,4,5,6]

for i in range(4):

j = random.randrange(i,5)

print(a[j],end='-')

print()

``

Python Libraries (random module) and Iterative Constructs
Random Module and Loop Tracing

Options

(A)3-4-5-4-
(B)2-2-4-5-
(C)4-3-3-5-
(D)5-1-2-4-

Official answer

Option (D) 5-1-2-4- will NOT be displayed, and the for loop runs for exactly 4 iterations.


Why: range(4) gives i = 0,1,2,3 (4 iterations). For each i, j = random.randrange(i,5) can only produce values from i up to 4 (5 excluded). So the value printed at each position is restricted to a shrinking range as i increases, and option D's second value (1) violates the valid range for i=1.

Answer keywords:random.randrange()range()for looplist indexingcode tracingiteration countrandom module

Marking scheme

  1. 1.1 mark: correctly identifying option (D) '5-1-2-4-' as the output that will NOT be displayed, with valid justification.
  2. 2.1 mark: correctly stating that the for loop runs for 4 iterations.

Hint

Work out the valid range of a[j] separately for each loop iteration (i=0,1,2,3) using randrange(i,5), which excludes 5.

Quick oral answer

The loop runs 4 times since range(4) gives i=0 to 3; at each i, randrange(i,5) restricts a[j] to a shrinking set of valid values, and option D fails because its second value, 1, falls outside the valid range for i=1.

Common mistakes

  1. 1.Assuming randrange(i,5) can produce 5 itself — forgetting the stop value in randrange() is always excluded.
  2. 2.Checking only the first value of each option against {1,2,3,4,5} and ignoring that the valid range narrows for later positions (i=1,2,3).
  3. 3.Miscounting loop iterations as 5 instead of 4 by confusing range(4) with range(1,5).

Analysis and explanation

This is a code-tracing question combining the random library with loop control and list indexing.


Loop count

range(4) produces i = 0, 1, 2, 3 — exactly 4 values, so the for loop executes 4 times, printing 4 numbers separated by hyphens.


Range of a[j] at each position

randrange(i, 5) returns an integer from i to 4 inclusive (5 excluded), so:

  • i=0: j in {0,1,2,3,4}\{0,1,2,3,4\} → a[j] in {1,2,3,4,5}\{1,2,3,4,5\}
  • i=1: j in {1,2,3,4}\{1,2,3,4\} → a[j] in {2,3,4,5}
  • i=2: j in {2,3,4}\{2,3,4\} → a[j] in {3,4,5}\{3,4,5\}
  • i=3: j in {3,4}\{3,4\} → a[j] in {4,5}

Checking each option against these ranges

  • (A) 3-4-5-4-: 3∈{1..5}3 \in \{1..5\}, 4∈{2..5}4 \in \{2..5\}, 5∈{3..5}5 \in \{3..5\}, 4∈{4,5}4 \in \{4,5\} → all valid, CAN be displayed.
  • (B) 2-2-4-5-: 2∈{1..5}2 \in \{1..5\}, 2∈{2..5}2 \in \{2..5\}, 4∈{3..5}, 5∈{4,5}5 \in \{4,5\} → all valid, CAN be displayed.
  • (C) 4-3-3-5-: 4∈{1..5}4 \in \{1..5\}, 3∈{2..5}3 \in \{2..5\}, 3∈{3..5}3 \in \{3..5\}, 5∈{4,5}5 \in \{4,5\} → all valid, CAN be displayed.
  • (D) 5-1-2-4-: 5∈{1..5}5 \in \{1..5\} ✓, but 1∉{2,3,4,5}1 \notin \{2,3,4,5\} (second position, i=1) — invalid, so it will NEVER be displayed.

Exam trap

Students often forget that randrange(i,5) EXCLUDES the stop value 5, and separately forget that the valid range of j shrinks as i increases — leading to wrongly validating option D.

Interesting facts

Python's random.randrange(start, stop) mimics the same half-open interval convention as range() — stop is always excluded — a deliberate design choice for consistency across the language.

The random module in CPython uses the Mersenne Twister algorithm internally, which has a period of 2^19937 − 1, making its output sequence statistically excellent for simulations though not cryptographically secure.

Question details

Q27 of 48 questions

Board
CBSE
Class
Class 12
Subject
Computer Science
Year
2026
Section
Section B
Marks
2
Type
Short Answer
Chapter
Python Libraries (random module) and Iterative Constructs
Topic
Random Module and Loop Tracing
Bloom's level
analyze
Syllabus topic
Unit 1: Computational Thinking and Programming – 2 — Python Libraries: random.randrange() combined with for loops and list indexing
Author
CBSE
Reviewed by
Padhantu Academic Team

Spotted a mistake or something unclear?

Tell us and we will fix it.

Frequently asked questions

Does random.randrange(i,5) ever return 5?

No. Like range(), randrange(start, stop) always excludes the stop value, so randrange(i,5) can return values only from i up to 4.

How many times does the for loop in this code execute?

It executes 4 times because range(4) generates i = 0, 1, 2, 3.