GATE 2024 CS (CS1) – Question 48
Consider the C function:
int f(int x, int y) {
for (int i = 0; i < y; i++) {
x = x + x + y;
}
return x;
}Which of the following statements is/are TRUE about the above function?
Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (B) If the inputs are x=20, y=20, then the return value is greater than $2^{20}$; (D) If the inputs are x=10, y=20, then the return value is greater than $2^{20}$
Explanation
Each loop iteration does $x\leftarrow 2x+y$, and the loop runs $y$ times. Unrolling gives the closed form
$$x_{final}=2^y\,x+y\,(2^y-1).$$
- **(A)** $x=20,y=10$: $2^{10}\cdot20+10\cdot1023=20480+10230=30710$. This is **not** greater than $2^{20}=1048576$. False.
- **(B)** $x=20,y=20$: $20\cdot2^{20}+20(2^{20}-1)>2^{20}$. True.
- **(C)** $x=20,y=10$: $30710$ is not less than $2^{10}=1024$. False.
- **(D)** $x=10,y=20$: $10\cdot2^{20}+20(2^{20}-1)>2^{20}$. True.
Answer: B, D.