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 21–25 of 68
Consider the C functions foo and bar given below:
int foo (int val ) {
int x = 0;
while (val > 0) {
x = x + foo(val--);
}
return val ;
}
int bar (int val ) {
int x = 0;
while (val > 0) {
x = x + bar(val-1);}
return val ;
} Invocations of foo(3) and bar(3) will result in:Answer: (C)
Let us check each function:
- foo(3): the call foo(val--) sends the old value 3. So foo(3) keeps calling foo(3) again and again stack overflow abnormal termination.
- bar(3) bar(2) bar(1) bar(0), which returns 0. Back in bar(1), val never changes, so the condition while (val 0) stays true and the loop runs forever infinite loop.
Answer: Abnormal termination and infinite loop respectively
- foo(3): the call foo(val--) sends the old value 3. So foo(3) keeps calling foo(3) again and again stack overflow abnormal termination.
- bar(3) bar(2) bar(1) bar(0), which returns 0. Back in bar(1), val never changes, so the condition while (val 0) stays true and the loop runs forever infinite loop.
Answer: Abnormal termination and infinite loop respectively
The output of executing the following C program is ________.
#include <stdio.h>
int total (int v) {
static int count = 0;
while (v) {
count += v & 1;
v >>= 1;
}
return count;
}
void main ( ) {
static int x = 0;
int i = 5;
for (; i> 0; i--) {
x=x + total (i) ;
}
printf ("%d\n", x) ; } Answer: 23
The function total(v) counts the 1-bits in v and adds them to static count. The count is never reset, and the function returns it.
- i = 5 (101): count = 2 x = 2
- i = 4 (100): count = 3 x = 5
- i = 3 (11): count = 5 x = 10
- i = 2 (10): count = 6 x = 16
- i = 1 (1): count = 7 x = 23
Answer: 23
- i = 5 (101): count = 2 x = 2
- i = 4 (100): count = 3 x = 5
- i = 3 (11): count = 5 x = 10
- i = 2 (10): count = 6 x = 16
- i = 1 (1): count = 7 x = 23
Answer: 23
Consider the following C program.
void f(int,short);
void main()
{
int i=100;
short s=12;
short *p=&s;
__________ ;//calltof()
} Which one of the following expressions, when placed in the blank above, will NOT result in a typec hecking error?Answer: (D)
The function f takes an (int, short) pair and returns void.
- f(s, *s): s is a short and not a pointer, so *s is not valid.
- i = f(i, s): f returns void, so its result cannot be assigned.
- f(i, *s): here too *s is not valid.
- f(i, *p): i is an int and *p is the short value s. This is correct.
Answer: f(i,*p)
- f(s, *s): s is a short and not a pointer, so *s is not valid.
- i = f(i, s): f returns void, so its result cannot be assigned.
- f(i, *s): here too *s is not valid.
- f(i, *p): i is an int and *p is the short value s. This is correct.
Answer: f(i,*p)
What will be the output of the following C program?
void count(int n){
static int d=1;
printf("%d ",n);
printf("%d ",d);
d++;
if(n>1) count(n-1);
printf("%d ",d);
}
void main(){
count(3);
}Answer: (A)
d is static, so there is only one copy and it starts at 1.
- count(3): prints 3, 1; d becomes 2; calls count(2)
- count(2): prints 2, 2; d becomes 3; calls count(1)
- count(1): prints 1, 3; d becomes 4; prints 4
- Back in count(2): prints 4. Back in count(3): prints 4
Output: 3 1 2 2 1 3 4 4 4
Answer: 312213444
- count(3): prints 3, 1; d becomes 2; calls count(2)
- count(2): prints 2, 2; d becomes 3; calls count(1)
- count(1): prints 1, 3; d becomes 4; prints 4
- Back in count(2): prints 4. Back in count(3): prints 4
Output: 3 1 2 2 1 3 4 4 4
Answer: 312213444
What will be the output of the following pseudo-code when parameters are passed by reference and dynamic scoping is assumed?
a=3;
void n(x){x=x*a; print(x);}
void m(y){a=1;a=y-a;n(a);print(a);}
void main(){m(a);}Answer: (D)
Treat "a = 1" in m as a local a that belongs to m. The name y points to the global a, which is 3.
- In m: local a is 1. Then a = y a = 3 1 = 2.
- In n(a): x points to the local a of m. Under dynamic scoping, the a used inside n is also the a of m, because it is the latest active a. So x = x a = 2 2 = 4. Since x is the same variable as a, a becomes 4. It prints 4.
- Back in m: print(a) prints 4.
Note: if a = 1 is read as an assignment to the global a (which y aliases), the output would be 0, 0, which is not an option. The official key D assumes the local reading above.
Answer: 4,4
- In m: local a is 1. Then a = y a = 3 1 = 2.
- In n(a): x points to the local a of m. Under dynamic scoping, the a used inside n is also the a of m, because it is the latest active a. So x = x a = 2 2 = 4. Since x is the same variable as a, a becomes 4. It prints 4.
- Back in m: print(a) prints 4.
Note: if a = 1 is read as an assignment to the global a (which y aliases), the output would be 0, 0, which is not an option. The official key D assumes the local reading above.
Answer: 4,4
Keep practising