Skip to main content

GATE CSE 2024Programming in C › General C Programming ConceptsVerified

GATE CSE 2024 PYQ: ​​Consider the following C \mathrm{C} C function definition. int f ( int x , int y ){ for ( int i = 0 ; i <…

​​Consider the following C\mathrm{C} function definition.

  1. int f (int x, int y){
  2. for (int i=0 ; i<y ; i++ ) {
  3. x= x + x + y;
  4. }
  5. return x;
  6. }

Which of the following statements is/are TRUE about the above function?

  1. A
    If the inputs are x=20,y=10x=20, y=10, then the return value is greater than 2202^{20}
  2. B
    If the inputs are x=20,y=20x=20, y=20, then the return value is greater than 2202^{20}
    Correct answer
  3. C
    If the inputs are x=20,y=10x=20, y=10, then the return value is less than 2102^{10}
  4. D
    If the inputs are x=10,y=20x=10, y=20, then the return value is greater than 2202^{20}
    Correct answer

Answer

(B, D) If the inputs are x = 20 , y = 20 x=20, y=20 x = 20 , y = 20 , then the return value is greater than 2 20 2^{20} 2 20; If the inputs are x = 10 , y = 20 x=10, y=20 x = 10 , y = 20 , then the return value is greater than 2 20 2^{20} 2 20

Approach

Trace the loop symbolically. Each iteration maps the current value $t$ to $2t + y$. Starting from $x$, the value after $k$ iterations is $t_k = 2^{k}(x+y) - y$ (verified by induction). Since the loop runs $y$ times, the returned value is $2^{y}(x+y) - y$. Substitute each option's $(x,y)$ and compare with the respective powers of two.

The loop executes exactly $y$ times, each time doubling the running value and adding $y$, giving closed form $2^{y}(x+y) - y$. With $y=20$ the multiplier $2^{20}$ along with a positive, non-trivial coefficient of $(x+y)$ forces the result above $2^{20}$ for both B and D, whereas $y=10$ keeps the result (30710) below $2^{20}$ but above $2^{10}$. Hence only B and D are true.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.