TCS
Company
undefined
What will be the output of the program ?
#include
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;m = a[i++];
printf("%d, %d, %d", i, j, m);
return 0;
}
1. 2, 1, 15
2. 1, 2, 5
3. 3, 2, 15
4. 2, 3, 20
Read Solution (Total 7)
-
- ans is option a .2,1,5
solution is
i=++a[1] is 2
j=a[i++] is 1
so m=a[i++] be a[2] which is 15 - 6 years agoHelpfull: Yes(2) No(3)
- the correct answer is option (3)
- 6 years agoHelpfull: Yes(2) No(0)
- Ans:
3,2,15
i is incremented twice so the value of i tends to be 3 - 6 years agoHelpfull: Yes(2) No(0)
- out put is
2,3,15
because in question
i=++a[1] is pre increment operator in array holds value a[1]=1 so i take a value 1 then follow the pre increment operator so i=2
Now j = a[1]++; we know a[1]=1 holds value is 1. but use the post increment operator so value is increase 1 and now j=2
In m = a[i++]; we find already i=2 in array a[2] holds value 15 so m=15
but in i also use the post increment operator then value of i increase 1 now the value of i=3
so i=3,j=2,m=15 - 6 years agoHelpfull: Yes(1) No(2)
- Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized to
a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 .
Step 2: int i, j, m; The variable i,j,m are declared as an integer type.
Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2
Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.
Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)
Step 6: printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m
Hence the output of the program is 3, 2, 15 - 5 years agoHelpfull: Yes(1) No(0)
- 4 . 2 3 20
- 6 years agoHelpfull: Yes(0) No(1)
- the correct ans is option c.(3,2,15)
check with c compiler - 5 years agoHelpfull: Yes(0) No(0)
TCS Other Question