118 118 votes Which one of the choices given below would be printed when the following program is executed? #include <stdio.h> void swap (int *x, int *y) { static int *temp; temp = x; x = y; y = temp; } void printab () { static int i, a = -3, b = -6; i = 0; while (i <= 4) { if ((i++)%2 == 1) continue; a = a + i; b = b + i; } swap (&a, &b); printf("a = %d, b = %d\n", a, b); } main() { printab(); printab(); } $a = 0, b = 3$ $a = 0, b = 3$ $a = 3, b = 0$ $a = 12, b = 9$ $a = 3, b = 6$ $a = 3, b = 6$ $a = 6, b = 3$ $a = 15, b = 12$ Programming in C gateit-2006 programming programming-in-c normal parameter-passing + – Ishrat Jahan 42.5k views answer comment Share Follow Print See all 21 Comments 21 21 Comments reply Show 18 previous comments Ekalavyaa commented Dec 6, 2024 i edited by Ekalavyaa 4 days ago reply Follow flag the first printab(); execution is enough to know the answer we don't need to solve the whole question but this particualr question tests so many concepts that we already know in c programming 0 0 replyShare Abhishek-1011 commented Feb 16, 2025 reply Follow flag Solution 6 6 replyShare Siddiqui_Danish commented Feb 3 i edited by Siddiqui_Danish Feb 3 reply Follow flag Never saw more clearer and better solution than this I dont know why it doesnt have more upvotes 0 0 replyShare Please log in or register to add a comment.
0 0 votes Answer:- advaith_xyz answered Nov 21, 2024 advaith_xyz comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes It prints:a = 6, b = 3 a = 15, b = 12Why:a and b are static, so they retain their values between calls to printab().i is reset to 0 at the start of each call.The loop adds 1, 3, and 5 to both a and b, for a total of 9.swap(&a, &b) does not swap the values. It only swaps local pointer variables because x and y are passed by value.First call:a = -3 + 9 = 6 b = -6 + 9 = 3Second call:a = 6 + 9 = 15 b = 3 + 9 = 12 Mohd_Siddiq answered Sep 25 Mohd_Siddiq comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes Answer: (D)a = 6, b = 3 a = 15, b = 12Why:swap does nothing visible. It receives copies of the two pointers and swaps those copies. The variables a and b are never touched, so the call has no effect on what gets printed.The loop in printab. i++ is a post-increment, so the comparison uses the old value of i, and i is incremented afterward. Tracing it:i=0: the check uses 0, so no continue. i becomes 1, then a += 1, b += 1.i=1: the check uses 1, so continue. i becomes 2.i=2: no continue. i becomes 3, then a += 3, b += 3.i=3: continue. i becomes 4.i=4: no continue. i becomes 5, then a += 5, b += 5.i=5: the loop condition 5 <= 4 fails, so the loop ends. Each call therefore adds 1 + 3 + 5 = 9 to both a and b.First call. a = -3 + 9 = 6 and b = -6 + 9 = 3, so it prints a = 6, b = 3.Second call. a, b and i are static, so they keep their values between calls. The initializers a = -3 and b = -6 run only once. i is reset to 0 by the explicit i = 0; statement, so the loop runs the same way again. Then a = 6 + 9 = 15 and b = 3 + 9 = 12, so it prints a = 15, b = 12.The other options fail because they assume the swap worked (A, B) or that the static values reset on the second call (C). Aman2028 answered Oct 3 Aman2028 comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes Answer: (D)a = 6, b = 3 a = 15, b = 12Why:swap does nothing visible. It receives copies of the two pointers and swaps those copies. The variables a and b are never touched, so the call has no effect on what gets printed.The loop in printab. i++ is a post-increment, so the comparison uses the old value of i, and i is incremented afterward. Tracing it:i=0: the check uses 0, so no continue. i becomes 1, then a += 1, b += 1.i=1: the check uses 1, so continue. i becomes 2.i=2: no continue. i becomes 3, then a += 3, b += 3.i=3: continue. i becomes 4.i=4: no continue. i becomes 5, then a += 5, b += 5.i=5: the loop condition 5 <= 4 fails, so the loop ends. Each call therefore adds 1 + 3 + 5 = 9 to both a and b.First call. a = -3 + 9 = 6 and b = -6 + 9 = 3, so it prints a = 6, b = 3.Second call. a, b and i are static, so they keep their values between calls. The initializers a = -3 and b = -6 run only once. i is reset to 0 by the explicit i = 0; statement, so the loop runs the same way again. Then a = 6 + 9 = 15 and b = 3 + 9 = 12, so it prints a = 15, b = 12. Aman2028 answered Oct 3 Aman2028 comment Share Follow 0 reply Please log in or register to add a comment.