GATE | GATE IT 2006 | Question 50
Which one of the choices given below would be printed when the following program is executed?
Attention reader! Don’t stop learning now. Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.
Learn all GATE CS concepts with Free Live Classes on our youtube channel.
(A) a = 0, b = 3
a = 0, b = 3
(B) a = 3, b = 0
a = 12, b = 9
(C) a = 3, b = 6
a = 3, b = 6
(D) a = 6, b = 3
a = 15, b = 12
Things to ponder:
• swap function doesn’t actually swaps two variables, rather just swaps their addresses in local variables x and y – which is effectively nothing once swap function returns.
• printab function adds 9 to static variables a and b. The number 9 comes from the fact that the while loop executes those arithmetic statements only when i = 1, 3, 5.
Hence, when printab is called for the first time, these are the local variable values: a = −3 + 9 and b = −6 + 9. On the second time, a = −3 + 9 + 9 and b = −6 + 9 + 9 are the desired values.
Therefore, correct answer would be (D) a = 6, b = 3; a = 15, b = 12.
This solution is contributed by vineet purswani .
Quiz of this Question