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?
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 and .
In each iteration of the loop (i = 0 to y - 1), x is updated to . Starting from initial value , after iterations:
- For :Since , both (A) and (C) are FALSE.
- For :
- For :
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.