Question

Consider the following C function definition.

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?

Choose every correct option
Official answer and solution
Official answer
B and D
From
Official GATE 2024 answer key

Solution

Find the closed-form value of x after y iterations of x = 2*x + y and compare with 2102^{10} and 2202^{20}.

In each iteration of the loop (i = 0 to y - 1), x is updated to 2x+y2x + y. Starting from initial value x0x_0, after yy iterations:

xfinal=x0⋅2y+y(2y−1)=(x0+y)2y−yx_{\text{final}} = x_0 \cdot 2^y + y(2^y - 1) = (x_0 + y)2^y - y
  • For x0=20,y=10x_0 = 20, y = 10:
    xfinal=30⋅210−10x_{\text{final}} = 30 \cdot 2^{10} - 10
    Since 210<30⋅210−10<32⋅210=215<2202^{10} < 30 \cdot 2^{10} - 10 < 32 \cdot 2^{10} = 2^{15} < 2^{20}, both (A) and (C) are FALSE.
  • For x0=20,y=20x_0 = 20, y = 20:
    xfinal=40⋅220−20>220  ⟹  (B) is TRUE.x_{\text{final}} = 40 \cdot 2^{20} - 20 > 2^{20} \implies \textbf{(B) is TRUE}.
  • For x0=10,y=20x_0 = 10, y = 20:
    xfinal=30⋅220−20>220  ⟹  (D) is TRUE.x_{\text{final}} = 30 \cdot 2^{20} - 20 > 2^{20} \implies \textbf{(D) is TRUE}.
Final answer

Options (B), (D)

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2024 Computer Science and Information Technology - Set 1 (CS1)
Question
48 of 65
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.