Function · Programming in C
68 questions · asked in 26 of 27 years (2000–2026)
MCQ 47 · NAT 211-mark 35 · 2-mark 33
Questions 41–45 of 68
#include <stdio.h>
int fun(int n, int * f_p) {
int t, f;
if (n <=1) {
*f_p =1;
return 1;
}
t = fun (n-1, f_p);
f = t+*f_p;
*f_p = t;
return f;
}
int main() {
int x = 15;
printf ("%d\n", fun(5,& x));
return 0;
} The value printed isAnswer: (B)
fun(1): n <= 1, so x = 1 and it returns 1. (The starting value 15 is overwritten.)
fun(2): t = 1, f = 1 + 1 = 2, x = 1, returns 2.
fun(3): t = 2, f = 2 + 1 = 3, x = 2, returns 3.
fun(4): t = 3, f = 3 + 2 = 5, x = 3, returns 5.
fun(5): t = 5, f = 5 + 3 = 8, x = 5, returns 8.
This is the Fibonacci sequence 1, 2, 3, 5, 8. The program prints 8.
Answer: (B)
void f (int n)
{
if (n <= 1) {
printf ("%d", n);
}
else {
f (n/2);
printf ("%d", n%2);
}
} Which of the following implementations will produce the same output for f(173) as the above code?P1
void f (int n)
{
if (n/2) {
f(n/2);
}
printf ("%d", n%2);
} P2
void f (int n)
{
if (n <=1) {
printf ("%d", n);
}
else {
printf ("%d", n%2);
f (n/2);
}
} Answer: (C)
The given code calls f(n/2) first and then prints . So the higher bits print first and it prints n in binary. For it prints n itself. Dividing 173 by 2 repeatedly gives 86, 43, 21, 10, 5, 2, 1. The remainders from the last call back are 1, 0, 1, 0, 1, 1, 0, 1, so f(173) prints .
P1 also calls f(n/2) before printing , but it stops when , that is for . For it prints , the same as the original. So P1 prints .
P2 prints before calling f(n/2), so it prints the bits from the lowest to the highest: and then the last call prints 1. The output is . This differs from , because the binary form of 173 is not a palindrome.
So only P1 gives the same output.
Answer: (C)
void f (int n)
{
if (n <=1) {
printf ("%d", n);
}
else {
f (n/2);
printf ("%d", n%2);
}
} What does f(173) print?
Answer: (D)
The call f(n) first calls f(n/2), and only after it returns prints . So the bits of n print from the most significant bit to the least significant bit. When it just prints n.
The chain of calls is . The call f(1) prints 1. Then, as the calls return, they print for , which is .
The output is . This is the binary form of 173, since .
Answer: (D)
int f1(int n)
{
if(n == 0 || n == 1)
return n;
else
return (2*f1(n-1) + 3*f1(n-2));
}
int f2(int n)
{
int i;
int X[N], Y[N], Z[N] ;
X[0] = Y[0] = Z[0] = 0;
X[1] = 1; Y[1] = 2; Z[1] = 3;
for(i = 2; i <= n; i++)
{
X[i] = Y[i-1] + Z[i-2];
Y[i] = 2*X[i];
Z[i] = 3*X[i];
}
return X[n] ;
} f1(8) and f2(8) return the valuesAnswer: (C)
f1 uses the recurrence with .
In f2, the starting values are , and they satisfy and for (, , ). The loop keeps this true for every , because and . So and , and
.
This is the same recurrence with the same starting values, so f1 and f2 return the same value. Solve it:
The characteristic equation is , so and . The general solution is
Use and : and . So and . Put these in (1):
, so .
The same by hand:
Answer: (C)
void reverse(void) {
int c;
if (?1) reverse ();
?2
}
main() {
printf("Enter Text "); printf("\n") ;
reverse(); printf("\n");
}Answer: (D)
reverse has its own local variable c. A call first reads one character into c, and calls itself if that character is not the newline. Only after the inner call returns does it print its own c. So the characters are printed in the opposite order to how they were read.This needs ?1 to read and test in one step:
((c = getchar()) != '\n'). The brackets around c = getchar() are needed because = has lower priority than !=. Without them, c would get the result of the comparison (0 or 1).?2 must print c:
putchar(c). For input abc, the calls read a, b, c and the newline. The last call stops and prints the newline, then the earlier calls print c, b, a as they return.Why the others fail:
(A) reads a character but never stores it in c, and
getchar(c) is not a valid call (getchar takes no argument and prints nothing).(B) also uses
getchar(c) for ?2, which prints nothing.(C) tests c before anything has been read into it, and nothing reads input.
Answer: (D)
Keep practising