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 16–20 of 68
Consider the following C program:
void convert(int n) {
if (n<0)
printf("%d",n);
else {
convert(n/2);
printf("%d",n%2);
}
} Which one of the following will happen when the function convert is called with any positive integer n as argument?Answer: (D)
When n is positive, n/2 keeps shrinking until it reaches 0. The base case needs n 0, and that never happens. convert(0) just calls convert(0) again and again.
The printf comes after the recursive call, so nothing ever gets printed. The program does not stop (until the stack overflows).
Answer: It will not print anything and will not terminate
The printf comes after the recursive call, so nothing ever gets printed. The program does not stop (until the stack overflows).
Answer: It will not print anything and will not terminate
Consider the following C program:
#include <stdio.h>
int r(){
int static num=7;
return num--;
}
int main() {
for(r();r();r()) {
printf("%d ",r());
};
return 0;
} Which one of the following values will be displayed on execution of the programs?Answer: (B)
num is static. It begins at 7. r() gives back num, then lowers it by 1.
- start: r() 7 (num = 6)
- test: r() 6, true; body prints r() 5
- update: r() 4; test: r() 3, true; body prints r() 2
- update: r() 1; test: r() 0, false loop ends
Output: 5 2
Answer: 52
- start: r() 7 (num = 6)
- test: r() 6, true; body prints r() 5
- update: r() 4; test: r() 3, true; body prints r() 2
- update: r() 1; test: r() 0, false loop ends
Output: 5 2
Answer: 52
Consider the following C program:
#include <stdio.h>
int counter = 0;
int calc (int a, int b) {
int c;
counter++;
if (b==3) return (a*a*a);
else {
c = calc(a, b/3);
return (c*c*c);
}
}
int main (){
calc(4, 81);
printf ("%d", counter);
} The output of this program is _____.Answer: 4
The variable counter goes up by one in every call. The calls go like this: calc(4, 81) calc(4, 27) calc(4, 9) calc(4, 3), which is the base case. So there are 4 calls in total.
Answer: 4
Answer: 4
Consider the following program written in pseudo-code. Assume that x and y are integers.
Count(x,y) {
if (y != 1){
if (x != 1) {
print("*");
Count(x/2, y);
}
else {
y = y-1;
Count(1024, y);
}
}
} The number of times that the print statement is executed by the call Count(1024,1024) is _____.Answer: 10230
Take each value of y from 1024 down to 2. For each one, x is halved from 1024 to 1, and the program prints one star at every halving. That gives 10 stars. After that, y is reduced by 1 and x is set back to 1024. The recursion stops when y = 1.
Total stars = 10 1023 = 10230
Answer: 10230
Total stars = 10 1023 = 10230
Answer: 10230
Consider the following two functions.
void fun1(int n) {
if(n == 0) return;
printf("%d", n);
fun2(n - 2);
printf("%d", n);
} void fun2(int n) {
if(n == 0) return;
printf("%d", n);
fun1(++n);
printf("%d", n);
} The output printed when fun1(5) is called isAnswer: (A)
Trace (both functions print n before and after the call; fun2 calls fun1(++n), so its last print shows n + 1):
fun1(5): 5 fun2(3): 3 fun1(4): 4 fun2(2): 2 fun1(3): 3 fun2(1): 1 fun1(2): 2 fun2(0) returns.
On return, it prints: 2, 2, 3, 3, 4, 4, 5.
Output: 5 3 4 2 3 1 2 2 2 3 3 4 4 5
Answer: 53423122233445
fun1(5): 5 fun2(3): 3 fun1(4): 4 fun2(2): 2 fun1(3): 3 fun2(1): 1 fun1(2): 2 fun2(0) returns.
On return, it prints: 2, 2, 3, 3, 4, 4, 5.
Output: 5 3 4 2 3 1 2 2 2 3 3 4 4 5
Answer: 53423122233445
Keep practising