Skip to content
Official PYQ · 2000

GATE CS 2000 Q2.20 — Programming in C

Explain with AI

The value of jj at the end of the execution of the following C program:

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);
} 

is:

Choose one option
View worked solution

Answer: A

The variable count inside the function incr is declared with the static storage class. In C, a static local variable:

  • Is initialized only once (at the beginning of program execution).
  • Retains its value between successive function calls.

Let's trace the execution of the for loop in main(), which runs for i=0,1,2,3,4i = 0, 1, 2, 3, 4:

IterationValue of iiPrevious countNew count = count + iReturned Value (jj)
10000 (initial)0+0=00 + 0 = 000
211000+1=10 + 1 = 111
322111+2=31 + 2 = 333
433333+3=63 + 3 = 666
544666+4=106 + 4 = 101010

After the fifth iteration, ii becomes 55, the loop condition i <= 4 becomes false, and the loop terminates.

Thus, the final value of jj at the end of execution is 10.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions