Q.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 ? 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 : (A) 3-4-5-4- (B) 2-2-4-5- (C) 4-3-3-5- (D) 5-1-2-4-
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →The loop runs exactly 4 iterations (i = 0,1,2,3). In iteration i, randrange(i,5) can only pick an index from i to 4, so the smallest printable value keeps rising. Option (D) 5-1-2-4- is impossible — its 2nd and 3rd values need indices that are already out of reach.
Concept — randrange(i, 5) shrinks the choices every pass
random.randrange(start, stop) returns an integer from start up to stop - 1. Here the start is the loop variable i, so with every iteration the lowest reachable index climbs by one, while the highest stays 4 (index 5 — value 6 — is never reachable).
import random
a = [1,2,3,4,5,6]
for i in range(4):
j = random.randrange(i,5)
print(a[j],end='-')
print()
range(4) produces i = 0, 1, 2, 3 → the loop runs 4 times, printing 4 values each followed by -.
Possibility table
| Iteration i | Possible j = randrange(i,5) | Possible printed value a[j] |
|---|---|---|
| 0 | 0, 1, 2, 3, 4 | 1, 2, 3, 4, 5 |
| 1 | 1, 2, 3, 4 | 2, 3, 4, 5 |
| 2 | 2, 3, 4 | 3, 4, 5 |
| 3 | 3, 4 | 4, 5 |
Two hard constraints follow: the value 1 can appear only in position 1, and the value 6 can never appear.
Checking each option position by position
| Option | Pos 1 (i=0) | Pos 2 (i=1) | Pos 3 (i=2) | Pos 4 (i=3) | Verdict |
|---|---|---|---|---|---|
| (A) 3-4-5-4- | 3 → j=2 ✓ | 4 → j=3 ✓ | 5 → j=4 ✓ | 4 → j=3 ✓ | possible |
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.