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
Solution
The correct answer is 3 2 15.
Here's the step-by-step explanation:
-
The array
ais initialized with the values{5,1,15,20,25}. -
i = ++a[1];This line first increments the value ata[1](which is initially 1) by 1, making it 2. Then, it assigns this value toi. So,iis now 2 anda[1]is also 2. -
j = a[1]++;This line first assigns the value ata[1](which is now 2) toj, and then incrementsa[1]by 1. So,jis now 2 anda[1]is 3. -
k = a[i++];This line first assigns the value ata[i](which isa[2]and is 15) tok, and then incrementsiby 1. So,kis now 15 andiis 3. -
cout << i << " " << j << " " << k;This line prints the values ofi,j, andk, which are 3, 2, and 15 respectively.
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?
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.