Skip to content
Question of 97

Q.Observe the following C++ code and find the possible output(s) from the options

(i) to
(iv) following it. Also, write the minimum and maximum values that can possibly be assigned to the variable End. Note : Assume all the required header files are already being included in the code. The function random(N) generates any possible integer between 0 and N-1 (both values included). void main() { randomize(); int A[]={10,20,30,40,50,60,70,80}; int Start = random(2) + 1; int End = Start + random(4); for(int I=Start; I<=End, I++) cout<<A[I]<<"$"; }
(i) 10202030$
(ii) 20303040505060$
(iii) 304040506060
(iv) 405050607070
CBSECBSE Class XII Board 2019Subjective· 2mImportance★★★★★est
0% · 0/97 Questions
🔒 Locked · start free trial →

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.

  1. Possible values of Start

    random(2) → {0, 1}.

    Start = random(2) + 1 → {1, 2}.

  2. Possible values of End

    random(4) → {0, 1, 2, 3}.

    So End = Start + random(4) ranges from Start to Start + 3.

    • If Start = 1: End can be 1, 2, 3, or 4.
    • If Start = 2: End can be 2, 3, 4, or 5.

    Therefore the overall minimum End is 1, and the overall maximum End is 5.

  3. What the loop prints

    The loop runs for(int I=Start; I<=End; I++) and prints A[I] followed by "$".

    The array A is:

    Index: 0 → 10, 1 → 20, 2 → 30, 3 → 40, 4 → 50, 5 → 60, 6 → 70, 7 → 80.

    So the printed sequence depends on Start and End:

    StartEndIndices printedOutput
    11120$
    121,2203030
    131,2,320303040$
    141,2,3,4203030405050
    22230$
    232,3304040

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.