ProgrammingforOutputProblems
October 6, 2023ProgrammingforOutputProblems
October 6, 2023ProgrammingforOutputProblems
Question 15

Consider the following C function:
int f(int n)
{
static int i = 1;
if(n >= 5) return n;
n = n+i;
i++;
return f(n);
}
The value returned by f(1) is
int f(int n)
{
static int i = 1;
if(n >= 5) return n;
n = n+i;
i++;
return f(n);
}
The value returned by f(1) is
5


6


7


8

Question 15 Explanation:
The variable “i” is static variable and the value of that variable will be retained between multiple function calls.
first line of f() is executed only once.
Depending upon the value “n” and “i”, the function f() will until n>=5.
You can find the tracing of function f() from the below
Execution of f(1)
i = 1
n = 2
i = 2
Call f(2)
i = 2
n = 4
i = 3
Call f(4)
i = 3
n = 7
i = 4
Call f(7)
since n >= 5 return n(7) with return value = 7
first line of f() is executed only once.
Depending upon the value “n” and “i”, the function f() will until n>=5.
You can find the tracing of function f() from the below
Execution of f(1)
i = 1
n = 2
i = 2
Call f(2)
i = 2
n = 4
i = 3
Call f(4)
i = 3
n = 7
i = 4
Call f(7)
since n >= 5 return n(7) with return value = 7
Correct Answer: C
Question 15 Explanation:
The variable “i” is static variable and the value of that variable will be retained between multiple function calls.
first line of f() is executed only once.
Depending upon the value “n” and “i”, the function f() will until n>=5.
You can find the tracing of function f() from the below
Execution of f(1)
i = 1
n = 2
i = 2
Call f(2)
i = 2
n = 4
i = 3
Call f(4)
i = 3
n = 7
i = 4
Call f(7)
since n >= 5 return n(7) with return value = 7
first line of f() is executed only once.
Depending upon the value “n” and “i”, the function f() will until n>=5.
You can find the tracing of function f() from the below
Execution of f(1)
i = 1
n = 2
i = 2
Call f(2)
i = 2
n = 4
i = 3
Call f(4)
i = 3
n = 7
i = 4
Call f(7)
since n >= 5 return n(7) with return value = 7
Subscribe
Login
0 Comments