Exams › GATE › Technical
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?
- If the inputs are x=20, y=10, then the return value is greater than 2²⁰
- If the inputs are x=20, y=20, then the return value is greater than 2²⁰
- If the inputs are x=2⁰, y=10, then the return value is less than 2¹⁰
- If the inputs are x=1⁰, y=20, then the return value is greater than 2²⁰
Correct answer: If the inputs are x=20, y=20, then the return value is greater than 2²⁰
Solution
The function doubles the value of x and adds y in each iteration of the loop, leading to exponential growth. For x=20 and y=20, the number of iterations (20) allows the value of x to increase significantly, resulting in a return value that exceeds 2²⁰.
Related GATE Technical questions
⚔️ Practice GATE Technical free + battle 1v1 →