Examveda

What is the output of the following code: for (int i = 0; i < 3; ++i) { for (int j = 0; j < 2; ++j) { cout << i << j << " "; } } in C++?

A. 00 11 22 33 21 01

B. 00 10 20 01 11 22

C. 00 01 10 11 20 21

D. Compilation error

Answer: Option C

Solution (By Examveda Team)

Solution:

The code contains a nested for loop structure.

The outer loop iterates with variable i from 0 to 2 (inclusive), and the inner loop iterates with variable j from 0 to 1 (inclusive).

For each iteration of the outer loop (each value of i), the inner loop runs completely, iterating through both values of j.

The output is printed as follows:

When i = 0:
j = 0 → output is 00
j = 1 → output is 01

When i = 1:
j = 0 → output is 10
j = 1 → output is 11

When i = 2:
j = 0 → output is 20
j = 1 → output is 21

Combining all outputs, the final result is:
00 01 10 11 20 21

Join The Discussion

Comments (1)

  1. Mûtugi Maina
    Mûtugi Maina:
    11 months ago

    How do we arrive at this answer. From a beginner

Related Questions on Control Flow Statements in C plus plus

What is the correct syntax for the 'switch' statement in C++?

A. switch (expression) { case constant1: statement1; case constant2: statement2; }

B. switch (expression) { case constant1: statement1; break; case constant2: statement2; break; default: defaultStatement; }

C. switch (expression) { case constant1: statement1; default: defaultStatement; }

D. None of the above