Question
Consider the following C program:
#include <stdio.h>
int jumble(int x, int y){
x = 2 * x + y;
return x;
}
int main(){
int x = 2, y = 5;
y = jumble(y, x);
x = jumble(y, x);
printf("%d \n", x);
return 0;
}
The value printed by the program is ________.
Official answer and solution
- Official answer
- 26
- From
- Official GATE 2019 answer key
Solution
Trace the function calls and values step by step in C.
The function jumble(int x, int y) is defined as:
int jumble(int x, int y){
x = 2 * x + y;
return x;
}
Note that parameters are passed by value, so modifications to x inside jumble do not affect the variables in the caller.
In main():
- Initially, and .
- Execute
y = jumble(y, x):- Argument passed for parameter
xis . - Argument passed for parameter
yis . - Inside
jumble: . - The function returns .
- In
main, is assigned . (At this point, ).
- Argument passed for parameter
- Execute
x = jumble(y, x):- Argument passed for parameter
xis . - Argument passed for parameter
yis . - Inside
jumble: . - The function returns .
- In
main, is assigned .
- Argument passed for parameter
printf("%d \n", x)prints the value of , which is .
Final answer
26
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2019 Computer Science and Information Technology (CS)
- Question
- 28 of 65, printed as CS Q.18
- 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.