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()
``
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()
``
Options
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.
Marking scheme
- 1.1 mark: correctly identifying option (D) '5-1-2-4-' as the output that will NOT be displayed, with valid justification.
- 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.Assuming randrange(i,5) can produce 5 itself — forgetting the stop value in randrange() is always excluded.
- 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.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:jin →a[j]ini=1:jin →a[j]in {2,3,4,5}i=2:jin →a[j]ini=3:jin →a[j]in {4,5}
Checking each option against these ranges
- (A) 3-4-5-4-: , , , → all valid, CAN be displayed.
- (B) 2-2-4-5-: , , 4∈{3..5}, → all valid, CAN be displayed.
- (C) 4-3-3-5-: , , , → all valid, CAN be displayed.
- (D) 5-1-2-4-: ✓, but (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.