...
Programming-for-Output-Problems
October 6, 2023
Programming-for-Output-Problems
October 6, 2023
Programming-for-Output-Problems
October 6, 2023
Programming-for-Output-Problems
October 6, 2023

Programming-for-Output-Problems

Question 16
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
A
5

B
6
C
7
D
8
Question 16 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
Correct Answer: C
Question 16 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

Leave a Reply

Your email address will not be published. Required fields are marked *