Give the contents of the val array after running the followi
Give the contents of the val array after running the following code and please explain why is this output:
int [] val = { 3, 10, 44 };
int i = 1;
val[i] = i + 1;
Question 5 options:
1)
3 2 44
2)
2 10 44
3)
3 44 44
4)
3 11 44
| |||
| |||
| |||
|
Solution
Answer:
1) 3 2 44
Reason:i=1
therefore val[i],i.e val[1]=i+1,i.e 1+1=2
hence val[1]=2
hence only first index of val is replaced by 2,as array start from 0 index the first index earlier contained 10 but now is replaced by 2.
Hence final value of val=3 2 44
![Give the contents of the val array after running the following code and please explain why is this output: int [] val = { 3, 10, 44 }; int i = 1; val[i] = i + 1 Give the contents of the val array after running the following code and please explain why is this output: int [] val = { 3, 10, 44 }; int i = 1; val[i] = i + 1](/WebImages/8/give-the-contents-of-the-val-array-after-running-the-followi-993700-1761511274-0.webp)