Project ForgeSign up free
Programming in C
Programming Fundamentals
GATE 2016
Hard

Consider the following C program.

void mystery(int *ptra, int *ptrb) {
  int *temp;
  temp = ptrb; ptrb = ptra; ptra = temp;
}
int main() {
  int a=2016, b=0, c=4, d=42;
  mystery(&a, &b);
  if (a < c) mystery(&c, &a);
  mystery(&a, &d);
  printf("%d\n", a);
}

The output of the program is ____.

2016

Explanation

mystery() swaps its own LOCAL pointer copies (ptra, ptrb) — this has zero effect on the caller's actual pointers or the values they point to, since the pointers themselves are passed BY VALUE. So every call to mystery() is a no-op as far as main's variables are concerned. a stays 2016 throughout, so a<c (2016<4) is false, and the final printf prints a=2016.

Source: GATE CS 2016 (Session 1 / Set A) (2016)

This question is part of a 15-day revision cycle — sign in and Project Forge will bring it back the day before you'd forget it.

Start revising free