Knowee
Questions
Features
Study Tools

What is the output of the following code?#include <iostream>using namespace std;int main() { int a[] = {5,1,15,20,25}; int i, j, k; i = ++a[1]; j = a[1]++; k = a[i++]; cout << i << " " << j << " " << k; return 0;}Options: Pick one correct answer from below3 2 152 3 202 1 151 2 20

Question

What is the output of the following code?#include <iostream>using namespace std;int main() { int a[] = {5,1,15,20,25}; int i, j, k; i = ++a[1]; j = a[1]++; k = a[i++]; cout << i << " " << j << " " << k; return 0;}Options: Pick one correct answer from below3 2 152 3 202 1 151 2 20

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

Solution

The correct answer is 3 2 15.

Here's the step-by-step explanation:

  1. The array a is initialized with the values {5,1,15,20,25}.

  2. i = ++a[1]; This line first increments the value at a[1] (which is initially 1) by 1, making it 2. Then, it assigns this value to i. So, i is now 2 and a[1] is also 2.

  3. j = a[1]++; This line first assigns the value at a[1] (which is now 2) to j, and then increments a[1] by 1. So, j is now 2 and a[1] is 3.

  4. k = a[i++]; This line first assigns the value at a[i] (which is a[2] and is 15) to k, and then increments i by 1. So, k is now 15 and i is 3.

  5. cout << i << " " << j << " " << k; This line prints the values of i, j, and k, which are 3, 2, and 15 respectively.

This problem has been solved

Similar Questions

What will be the output of the following code snippet?

What is the output of the following code in Python?

What will be the output of the following C code?

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 C++ 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.