Knowee
Questions
Features
Study Tools

What is the output of the following snippet? #include <iostream> using namespace std; int main() { int *it[3]; for(int i = 0; i < 3; i++) { it[i] = new int [i + 1]; for(int j = 0; j < i + 1; j++) it[i][j] = 10 * i + j; } cout << it[2][2]; for(int i = 0; i < 3; i++) delete [] it[i]; } 22 11 33 Compilation fails

Question

What is the output of the following snippet?

#include <iostream>

using namespace std;

int main() { int *it[3];

for(int i = 0; i &lt; 3; i++) {
    it[i] = new int [i + 1];
    for(int j = 0; j &lt; i + 1; j++)
        it[i][j] = 10 * i + j;
}
cout &lt;&lt; it[2][2];
for(int i = 0; i &lt; 3; i++) 
    delete [] it[i];

}

22

11

33

Compilation fails

🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the given C++ code snippet will be 22.

Here's the step by step explanation:

  1. The code declares an array of integer pointers it[3].

  2. Then, it enters a loop that iterates three times (i = 0, 1, 2).

  3. In each iteration, it allocates a dynamic array of integers of size i + 1 and assigns its address to it[i].

  4. Then, it enters another loop that iterates i + 1 times and assigns the value 10 * i + j to each element of the dynamic array.

  5. After the loops, it prints the value of the third element of the third dynamic array, which is 10 * 2 + 2 = 22.

  6. Finally, it deallocates the dynamic arrays.

So, the correct answer is 22.

This problem has been solved

Similar Questions

What is the output for the following code?

What does the following code snippet do?

What is the output of the following code in Python?

What is the output for the following code?Note: This question helps in clearing conceptual technical interviews for service-based companies.

What will be the output of the following PHP code?

1/4

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.