• edited by
42,483 views
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();
}
  1. $a = 0, b = 3$
    $a = 0, b = 3$
  2. $a = 3, b = 0$
    $a = 12, b = 9$
  3. $a = 3, b = 6$
    $a = 3, b = 6$
  4. $a = 6, b = 3$
    $a = 15, b = 12$

10 Answers

0 0 votes

It prints:

a = 6, b = 3
a = 15, b = 12

Why:

  • 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 = 3

Second call:

a = 6 + 9 = 15
b = 3 + 9 = 12
0 0 votes

Answer: (D)

a = 6, b = 3
a = 15, b = 12

Why:

  1. 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.
  2. 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.
  3. First call. a = -3 + 9 = 6 and b = -6 + 9 = 3, so it prints a = 6, b = 3.
  4. 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).

 

0 0 votes

Answer: (D)

a = 6, b = 3
a = 15, b = 12

Why:

  1. 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.
  2. 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.
  3. First call. a = -3 + 9 = 6 and b = -6 + 9 = 3, so it prints a = 6, b = 3.
  4. 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.

 

 

Answer:
Position:
Show:

Related questions

103 103 votes
8 answers 8 answers
24.9k
24.9k views
Ishrat Jahan asked Oct 31, 2014
24,856 views
Which one of the choices given below would be printed when the following program is executed? #include <stdio.h int a1[] = {6, 7, 8, 18, 34, 67}; int a2[] = {23, 5...
151 151 votes
7 answers 7 answers
41.5k
41.5k views
Ishrat Jahan asked Oct 31, 2014
41,528 views
Which one of the choices given below would be printed when the following program is executed ?#include <stdio.h struct test { int i; char *c; }st[] = {5, "become", 4, "be...
33 33 votes
7 answers 7 answers
18.6k
18.6k views
Ishrat Jahan asked Oct 28, 2014
18,560 views
Consider the C program below. What does it print?# include <stdio.h # define swap1 (a, b) tmp = a; a = b; b = tmp void swap2 ( int a, int b) { int tmp; tmp = a; a = b; b ...
17 17 votes
2 answers 2 answers
10.2k
10.2k views
Rucha Shelke asked Sep 26, 2014
10,239 views
Consider the following code written in a pass-by-reference language like FORTRAN and these statements about the code. subroutine swap(ix,iy) it = ix L1 : ix = iy L2 : iy ...