Question:

What possible output from the given options is expected to be displayed when the following code is executed?
import random
Cards = ["Heart", "Spade", "Club", "Diamond"]
for i in range(2):
    print(Cards[random.randint(1, i+2)], end="#")

Show Hint

Always check the index range for randint() carefully.
Remember: Python list indexing starts from 0.
  • Spade#Diamond#
  • Spade#Heart#
  • Diamond#Club#
  • Heart#Spade#
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is A

Solution and Explanation

The list Cards has 4 items: "Heart", "Spade", "Club", "Diamond".
The loop runs for i in range(2), so i takes values 0 and 1.
Inside the loop, the index used is random.randint(1, i+2).
When i = 0: random.randint(1, 2) picks either 1 or 2.
So, possible cards are "Spade" (index 1) or "Club" (index 2).
When i = 1: random.randint(1, 3) picks 1, 2, or 3.
So, possible cards are "Spade", "Club", or "Diamond".
Option (A) shows Spade#Diamond#, which is valid:
First draw: index 1 → Spade, Second draw: index 3 → Diamond.
Other options either start with Heart (index 0), which cannot be picked,
or repeat an index not possible with given ranges.
Therefore, option (A) is valid.
Was this answer helpful?
0
0