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
How do we arrive at this answer. From a beginner