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 26–30 of 68
Consider thefollowingprogram:
int f(int*p, int n)
{
if (n<=1)return 0;
else return max(f(p+1,n-1),p[0]-p[1]);
}
int main()
{
int a[]={3,5,2,6,4};
printf("%d", f(a,5));
} Note: max(x,y) returns the maxi mumof x and y. The value printed by this program is____________ .Answer: 3
The array is . The call returns 0 if , else . Trace from the innermost call:
- (base case)
So f returns the larger of 0 and the biggest difference over neighbouring pairs. Here the differences are , so the printed value is 3.
Answer: 3
Consider the following function written in the C programming language.
void foo(char *a){
if ( *a && *a != ' '){
foo(a+1);
putchar(*a);
}
} The output of the above function on input isAnswer: (D)
The function calls itself on the next character first, and only then prints the current character. So the characters come out in reverse order. The calls stop when the string ends or when a space is found.
Only "ABCD" gets processed. It is printed backwards: DCBA
Answer: DCBA
Only "ABCD" gets processed. It is printed backwards: DCBA
Answer: DCBA
Consider the following C function.
int fun(int n){
int x=1,k;
if (n==1) return x;
for (k=1; k < n; ++k)
x = x + fun(k) * fun(n-k);
return x;
} The return value of fun(5) is ________.Answer: 51
Recurrence used: , with base case fun(1) = 1. Now find each value in turn:
- fun(2) = 1 + 1 = 2
- fun(3) = 1 + 2 + 2 = 5
- fun(4) = 1 + 5 + 4 + 5 = 15
- fun(5) = 1 + 15 + 10 + 10 + 15 = 51
Answer: 51
- fun(2) = 1 + 1 = 2
- fun(3) = 1 + 2 + 2 = 5
- fun(4) = 1 + 5 + 4 + 5 = 15
- fun(5) = 1 + 15 + 10 + 10 + 15 = 51
Answer: 51
Consider the following recursive C function.
void get(int n)
{
if (n<1) return;
get(n-1);
get(n-3);
printf("%d", n);
} If get(6) function is being called in main()then how many times will the get()function be invoked before returning to the main()?Answer: (B)
Let C(n) be the number of calls made by get(n). When n 1, C(n) = 1. Otherwise, C(n) = 1 + C(n1) + C(n3).
Now find the values one by one:
- C(1) = 1 + 1 + 1 = 3
- C(2) = 1 + 3 + 1 = 5
- C(3) = 1 + 5 + 1 = 7
- C(4) = 1 + 7 + 3 = 11
- C(5) = 1 + 11 + 5 = 17
- C(6) = 1 + 17 + 7 = 25
Answer: 25
Now find the values one by one:
- C(1) = 1 + 1 + 1 = 3
- C(2) = 1 + 3 + 1 = 5
- C(3) = 1 + 5 + 1 = 7
- C(4) = 1 + 7 + 3 = 11
- C(5) = 1 + 11 + 5 = 17
- C(6) = 1 + 17 + 7 = 25
Answer: 25
Consider the following C program.
#include <stdio.h>
int f1(void);
int f2(void);
int f3(void);
int x = 10;
int main( )
{
int x = 1;
x += f1( ) + f2( ) + f3( ) + f2( );
printf("%d", x);
return 0;
}
int f1() { int x = 25; x++; return x;}
int f2() { static int x = 50; x++; return x;}
int f3() { x *= 10; return x;} The output of the program is ________.Answer: 230
Local x in main starts at 1.
- f1() returns 26, because it uses its own local x.
- f2() returns 51, because the static x goes up to 51.
- f3() works on the global x: 10 10 = 100
- The second f2() call returns 52.
So x = 1 + 26 + 51 + 100 + 52 = 230
Answer: 230
- f1() returns 26, because it uses its own local x.
- f2() returns 51, because the static x goes up to 51.
- f3() works on the global x: 10 10 = 100
- The second f2() call returns 52.
So x = 1 + 26 + 51 + 100 + 52 = 230
Answer: 230
Keep practising