The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+14 votes
1.1k views

The value of $j$ 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:

  1. $10$
  2. $4$
  3. $6$
  4. $7$
asked in Programming by Veteran (59.5k points)
edited by | 1.1k views

2 Answers

+19 votes
Best answer

Answer: (A)


At $i=0, j=0$
At $i=1, j=1$
At $i=2, j=3$
At $i=3, j=6$
At $i=4, j=10$

answered by Boss (34.1k points)
edited by
0
$\frac{(n)*(n+1)}{2}$ Put n = 4.
+1
This is because count being a static variable is not reinitialized at every call post the first call of incr(i)
0
Above code is for $j=\sum_{i=0}^{n}i = \frac{n(n+1)}{2}$
here $n=4$,
$\Rightarrow j= \frac{4*5}{2}=10$
+3 votes
ans 10
answered by Active (5.2k points)


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

37,111 questions
44,694 answers
127,234 comments
43,753 users