The GATE Grind

GATE 2024 CS (CS1) – Question 48

Programming and Data Structures · Programming in C · 2 marks · Multiple select

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?

  1. If the inputs are x=20, y=10, then the return value is greater than $2^{20}$
  2. If the inputs are x=20, y=20, then the return value is greater than $2^{20}$
  3. If the inputs are x=20, y=10, then the return value is less than $2^{10}$
  4. If the inputs are x=10, y=20, then the return value is greater than $2^{20}$

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (A) If the inputs are x=20, y=10, then the return value is greater than $2^{20}$; (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 iteration roughly doubles x and adds y, so the result is about x·2^y. For (20,10): ≥20·1024 > 2^14 and with additions exceeds 2^20; (20,20) and (10,20) clearly exceed 2^20. C is false.