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():

  1. Initially, x=2x = 2 and y=5y = 5.
  2. Execute y = jumble(y, x):
    • Argument passed for parameter x is y=5y = 5.
    • Argument passed for parameter y is x=2x = 2.
    • Inside jumble: x=2×5+2=12x = 2 \times 5 + 2 = 12.
    • The function returns 1212.
    • In main, yy is assigned 1212. (At this point, x=2,y=12x = 2, y = 12).
  3. Execute x = jumble(y, x):
    • Argument passed for parameter x is y=12y = 12.
    • Argument passed for parameter y is x=2x = 2.
    • Inside jumble: x=2×12+2=26x = 2 \times 12 + 2 = 26.
    • The function returns 2626.
    • In main, xx is assigned 2626.
  4. printf("%d \n", x) prints the value of xx, which is 2626.
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.