Function · Programming in C
68 questions · asked in 26 of 27 years (2000–2026)
MCQ 47 · NAT 211-mark 35 · 2-mark 33
2000Questions per year2026
Questions 51–55 of 68
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();
} Answer: (D)
swap changes only its own copies of the pointers x and y, so a and b in printab are not changed.
Trace the loop in printab. The test
- old : even, i becomes , so ,
- old : odd, i becomes ,
- old : even, i becomes , add
- old : odd, i becomes ,
- old : even, i becomes , add
- : the loop ends
So each call adds to both a and b.
First call: and .
a and b are static, so they keep their values between calls. Second call: and .
The output is a = 6, b = 3 and then a = 15, b = 12.
Answer: (D)
Trace the loop in printab. The test
(i++)%2 uses the old value of i, but the increment happens at once, so the statements after it see the new i. After i = 0:- old : even, i becomes , so ,
- old : odd, i becomes ,
continue (nothing added)- old : even, i becomes , add
- old : odd, i becomes ,
continue- old : even, i becomes , add
- : the loop ends
So each call adds to both a and b.
First call: and .
a and b are static, so they keep their values between calls. Second call: and .
The output is a = 6, b = 3 and then a = 15, b = 12.
Answer: (D)
Consider the following code written in a pass-by reference language like FORTAN and these statements about the code.
S2: On execution the code will generate a runtime error on line L1
S3: On execution the code will generate a runtime error on line L2
S4: The program will print 13 and 8
S5: The program will print 13 and -2
Exactly the following set of statement (s) is correct:
subroutine swap(ix,iy)
it = ix
L1 : ix = iy
L2 : iy = it
end
ia = 3
ib = 8
call swap (ia, ib+5)
print *, ia, ib
end S1: The complier will generate code to allocate a temporary nameless cell, initialize it to 13, and pass the address of the cell to swap S2: On execution the code will generate a runtime error on line L1
S3: On execution the code will generate a runtime error on line L2
S4: The program will print 13 and 8
S5: The program will print 13 and -2
Exactly the following set of statement (s) is correct:
Answer: (B)
In pass by reference the callee gets the address of each argument. For a plain variable such as ia, that is the address of ia. For an expression such as ib+5, the compiler evaluates it into a temporary nameless cell holding 13 and passes that cell's address. So S1 is true.
Inside swap, ix is ia and iy is the temporary cell. Trace:
L1: sets ia to 13.
L2: writes 3 into the temporary cell.
Both writes go to valid cells, so there is no runtime error at L1 or L2. S2 and S3 are false.
ib was never passed, only a copy of ib+5 in the temporary, so ib stays 8. The program prints 13 and 8. S4 is true. S5 (13 and -2) is false.
Answer: (B)
Inside swap, ix is ia and iy is the temporary cell. Trace:
L1: sets ia to 13.
L2: writes 3 into the temporary cell.
Both writes go to valid cells, so there is no runtime error at L1 or L2. S2 and S3 are false.
ib was never passed, only a copy of ib+5 in the temporary, so ib stays 8. The program prints 13 and 8. S4 is true. S5 (13 and -2) is false.
Answer: (B)
What is the output printed by the following program?
#include <stdio.h>
int f(int n, int k) {
if (n == 0) return 0;
else if (n % 2) return f(n/2, 2*k) + k;
else return f(n/2, 2*k) - k;
}
int main () {
printf("%d", f(20, 1));
return 0;
} Answer: (C)
The rule is: if n is odd, ; if n is even, ; and . In the figure each box shows a call written in terms of the next call below it, and the number in the small square on the arrow is the value that call returns to the one above.
, so going back up:
Answer: (C)
Consider the following C-program:
double foo (double); /* Line 1 */
int main()
{
double da, db;
// input da
db = foo(da);
}
double foo(double a)
{
return a;
} The above code compiled without any error or warning. If Line 1 is deleted, the above code will show:Answer: (D)
Without Line 1, foo is called before it is declared. Old C then assumes an implicit declaration , with return type int. The later definition has a different return type, so the compiler reports a conflicting-types error. Also, implicit declaration of functions is not allowed at all from C99 onwards.
Answer: (D)
Answer: (D)
Consider the following C-program:
void foo(int n, int sum)
{
int k = 0, j = 0;
if (n == 0) return;
k = n % 10;
j = n / 10;
sum = sum + k;
foo (j, sum);
printf ("%d,", k);
}
int main ()
{
int a = 2048, sum = 0;
foo (a, sum);
printf ("%d\n", sum);
getchar();
} What does the above program print?Answer: (D)
C passes
Going down:
foo(2048, 0): k = 8, j = 204, sum = 8; calls foo(204, 8).
foo(204, 8): k = 4, j = 20, sum = 12; calls foo(20, 12).
foo(20, 12): k = 0, j = 2, sum = 12; calls foo(2, 12).
foo(2, 12): k = 2, j = 0, sum = 14; calls foo(0, 14).
foo(0, 14): n == 0, returns without printing.
Coming back up, the calls print their k in order: 2, then 0, then 4, then 8.
In main, sum was passed by value, so it is still 0, and main prints 0. The output is 2,0,4,8,0.
Answer: (D)
n and sum by value, so each call works on its own copies. The print of k comes after the recursive call, so it runs as the calls return.Going down:
foo(2048, 0): k = 8, j = 204, sum = 8; calls foo(204, 8).
foo(204, 8): k = 4, j = 20, sum = 12; calls foo(20, 12).
foo(20, 12): k = 0, j = 2, sum = 12; calls foo(2, 12).
foo(2, 12): k = 2, j = 0, sum = 14; calls foo(0, 14).
foo(0, 14): n == 0, returns without printing.
Coming back up, the calls print their k in order: 2, then 0, then 4, then 8.
In main, sum was passed by value, so it is still 0, and main prints 0. The output is 2,0,4,8,0.
Answer: (D)
Keep practising