Question:medium

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.
Updated On: Jan 14, 2026
  • Spade#Diamond#
  • Spade#Heart#
  • Diamond#Club#
  • Heart#Spade#
Show Solution

The Correct Option is A

Solution and Explanation

The list Cards contains 4 items: "Heart", "Spade", "Club", "Diamond".
The loop iterates with i taking values 0 and 1 (from range(2)).
Inside the loop, the index is determined by random.randint(1, i+2).
For i = 0: random.randint(1, 2) yields 1 or 2.
This corresponds to cards "Spade" (index 1) or "Club" (index 2).
For i = 1: random.randint(1, 3) yields 1, 2, or 3.
This corresponds to cards "Spade", "Club", or "Diamond".
Option (A), Spade#Diamond#, is valid:
First selection: index 1 → Spade. Second selection: index 3 → Diamond.
Other options are invalid because they either start with Heart (index 0), which is not selectable,
or repeat indices that are not possible within the given ranges.
Thus, option (A) is confirmed as valid.
Was this answer helpful?
0