Q.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="#")
(A) Spade#Diamond#
(B) Spade#Heart#
(C) Diamond#Club#
(D) Heart#Spade#
import random
Cards=["Heart","Spade","Club","Diamond"]
for i in range(2):
print(Cards[random.randint(1,i+2)],end="#")
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 code prints two random card suits separated by #, but the first draw is always from a list that starts at index 1, so the first output can never be "Heart" — only "Spade", "Club", or "Diamond" are possible for the first term.
Let’s walk through what this code actually does, step by thought.
The list Cards has four elements:
"Heart" at index 0, "Spade" at index 1, "Club" at index 2, "Diamond" at index 3.
The loop runs exactly twice (for i in range(2)). Inside, the code picks a random integer using random.randint(1, i+2) and uses that integer as an index into Cards.
The critical detail is the range of indices that randint can produce in each iteration.
First iteration (i = 0):
random.randint(1, 0+2) = random.randint(1, 2).
So the possible indices are 1 or 2 only.
Index 1 → "Spade"
Index 2 → "Club"
Index 0 ("Heart") and index 3 ("Diamond") are impossible in the first draw.
Second iteration (i = 1):
random.randint(1, 1+2) = random.randint(1, 3).
Now the possible indices are 1, 2, or 3.
Index 1 → "Spade"
Index 2 → "Club"
Index 3 → "Diamond"
Again, index 0 ("Heart") is impossible.
The end="#" in the print statement means the two outputs are joined by # with no trailing newline. So the output looks like FirstDraw#SecondDraw#.
Now look at the options:
(A) Spade#Diamond# — possible? First draw Spade (index 1) is allowed. Second draw Diamond (index 3) is allowed. So this is 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.