English

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? - Computer Science (Python)

Advertisements
Advertisements

Question

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

  • 3−4−5−4−

  • 2−2−4−5−

  • 4−3−3−5−

  • 5−1−2−4−

MCQ
Advertisements

Solution

5−1−2−4−

Explanation:

random.randrange(i,5) yields j in [i..4], so the second iteration (i=1) can only pick indices ≥1, it cannot produce index 0, therefore the sequence containing "1" in the second position (5-1-2-4-) is impossible.

shaalaa.com
  Is there an error in this question or solution?
2025-2026 (March) Set 4
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×