Q.Observe the following C++ code and find the possible output(s) from the options
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 prints array elements from index Start to End, where Start is 1 or 2, and End is Start plus a random number from 0 to 3. The only matching output is option (ii). Minimum possible End = 1, maximum possible End = 5.
The key is to understand what random(N) does: it returns an integer from 0 to N−1, inclusive. So random(2) gives either 0 or 1. Adding 1 means Start is either 1 or 2.
Then random(4) gives 0, 1, 2, or 3. So End = Start + random(4) can be as low as Start + 0 and as high as Start + 3.
Let’s work through the possibilities.
-
Possible values of
Startrandom(2)→ {0, 1}.Start = random(2) + 1→ {1, 2}. -
Possible values of
Endrandom(4)→ {0, 1, 2, 3}.So
End = Start + random(4)ranges fromStarttoStart + 3.- If
Start = 1:Endcan be 1, 2, 3, or 4. - If
Start = 2:Endcan be 2, 3, 4, or 5.
Therefore the overall minimum
Endis 1, and the overall maximumEndis 5. - If
-
What the loop prints
The loop runs
for(int I=Start; I<=End; I++)and printsA[I]followed by"$".The array
Ais:Index: 0 → 10, 1 → 20, 2 → 30, 3 → 40, 4 → 50, 5 → 60, 6 → 70, 7 → 80.
So the printed sequence depends on
StartandEnd:Start End Indices printed Output 1 1 1 20$ 1 2 1,2 20 1 3 1,2,3 2040$ 1 4 1,2,3,4 2040 2 2 2 30$ 2 3 2,3 30
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.