The value of j at the end of the execution of the following C program. C int…
2000
The value of j at the end of the execution of the following C program.
C
int incr(int i)
{
static int count = 0;
count = count + i;
return (count);
}
main()
{
int i,j;
for (i = 0; i <=4; i++)
j = incr(i);
}
- A.
10
- B.
4
- C.
6
- D.
7
Attempted by 25 students.
Show answer & explanation
Correct answer: A
The correct answer is: 10.
The variable count inside incr() is declared static, so it is initialized only once and retains its value between function calls.
The loop calls incr(i) for i = 0, 1, 2, 3, and 4. The values of count after each call are:
0, 1, 3, 6, 10.
Each call returns the current value of count and assigns it to j. After the final call, j = 10.