Answer: B;D
To find the value returned by the function f(x, y), let us trace the value of x after each iteration of the loop.
Let xi denote the value of x after the i-th iteration of the loop, with x0=x being the initial value.
The loop runs for i=0 to y−1, which is exactly y iterations. The recurrence relation is:
xi=2xi−1+y
Let us expand this recurrence step-by-step:
x1x2x3xk=2x0+y=2(2x0+y)+y=22x0+(2+1)y=2(22x0+3y)+y=23x0+(4+2+1)y⋮=2kx0+(2k−1+2k−2+⋯+1)y=2kx0+(2k−1)y
Since the loop runs for y iterations, the final value returned is xy:
xy=2yx+(2y−1)y=2y(x+y)−y
Now, let us evaluate each option using this closed-form expression:
- Options A & C: x=20,y=10
x10=210(20+10)−10=30⋅210−10=30⋅1024−10=30710
- Since 30710<220 (where 220=1,048,576), Option A is FALSE.
- Since 30710>210 (where 210=1024), Option C is FALSE.
- Option B: x=20,y=20
x20=220(20+20)−20=40⋅220−20
Since 40⋅220−20>220, Option B is TRUE.
- Option D: x=10,y=20
x20=220(10+20)−20=30⋅220−20
Since 30⋅220−20>220, Option D is TRUE.