Skip to content
Official PYQ · 2004

GATE CS 2004 Q2 — Runtime Environments

Explain with AI

Consider the following function

void swap(int a, int b)  
{       
    int temp;
    temp = a;
    a = b;
    b = temp;  
}  

In order to exchange the values of two variables xx and yy.

Choose one option
View worked solution

Answer: D

In the C programming language, parameters are passed by value by default.

Let's analyze what happens when we try to use the given swap function:

  1. Call swap(x, y): The values of the arguments x and y are copied into the local parameters a and b of the function. Inside the function, the values of a and b are successfully swapped, but these changes are local to the function's activation record. Once the function returns, the local variables a and b are destroyed, and the original variables x and y in the calling function remain completely unaffected.
  2. Call swap(&x, &y): This will result in a compilation error because the function signature is void swap(int a, int b), which expects integer values, not pointers (addresses) of type int *.

Therefore, the given swap(x, y) function cannot be used to exchange the values of x and y because the parameters are passed by value.

Thus, Option D is the correct statement.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Runtime Environments questions