Exams › GATE › Technical
Consider the following C program.
#include<stdio.h>
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
", a);
}
The output of the program is _________.
- 2016
- 0
- 4
- 42
Correct answer: 42
Solution
The function 'mystery' swaps the pointers but does not affect the values of the variables they point to. After the calls to 'mystery', the value of 'a' is ultimately set to the value of 'd', which is 42, making that the final output.
Related GATE Technical questions
⚔️ Practice GATE Technical free + battle 1v1 →