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 11–15 of 68
Consider the following ANSI C function:
int SomeFunction (int x, int y)
{
if ((x==1) || (y==1)) return 1;
if (x==y) return x;
if (x > y) return SomeFunction(x-y, y);
if (y > x) return SomeFunction (x, y-x);
} The value returned by SomeFunction(15, 255) is __________Answer: 15
This function finds the GCD by subtracting again and again. It also returns 1 if either value becomes 1.
Trace SomeFunction(15, 255): take 255 15 again and again, which gives (15, 240) (15, 15) and the function returns 15.
So gcd(15, 255) = 15
Answer: 15
Trace SomeFunction(15, 255): take 255 15 again and again, which gives (15, 240) (15, 15) and the function returns 15.
So gcd(15, 255) = 15
Answer: 15
Consider the following ANSI C program
#include <stdio.h>
int foo(int x, int y, int q)
{
if ((x <= 0) && (y <= 0))
return q;
if (x <= 0)
return foo(x, y-q, q);
if (y <= 0)
return foo(x-q, y, q);
return foo(x, y-q, q) + foo(x-q, y, q);
}
int main( )
{
int r = foo(15, 15, 10);
printf("%d", r);
return 0;
} The output of the program upon execution is ____Answer: 60
Let F(x, y) = foo(x, y, 10). Each of the two values goes down from 15 5 5.
- F(5, 5) = 10
- F(5, 5) = F(5, 5) = 10, and in the same way F(5, 5) = 10
- F(5, 5) = F(5, 5) + F(5, 5) = 20
- F(15, 5) = F(5, 5) = 10, so F(15, 5) = F(15, 5) + F(5, 5) = 30
- By symmetry, F(5, 15) = 30
F(15, 15) = F(15, 5) + F(5, 15) = 60
Answer: 60
- F(5, 5) = 10
- F(5, 5) = F(5, 5) = 10, and in the same way F(5, 5) = 10
- F(5, 5) = F(5, 5) + F(5, 5) = 20
- F(15, 5) = F(5, 5) = 10, so F(15, 5) = F(15, 5) + F(5, 5) = 30
- By symmetry, F(5, 15) = 30
F(15, 15) = F(15, 5) + F(5, 15) = 60
Answer: 60
Consider the following C functions.
The return value of fun2(5) is ______
int fun1(int n) {
static int i = 0;
if (n > 0) {
++i;
fun1(n-1);
}
return(i);
} int fun2(int n) {
static int i = 0;
if (n > 0) {
i = i + fun1(n);
fun2(n-1);
}
return(i);
}The return value of fun2(5) is ______
Answer: 55
Each function has its own static i, and neither one is ever reset. Every call to fun1(n) raises fun1's i by n and returns that value.
fun2(5) adds these values to fun2's i: fun1(5), fun1(4), , fun1(1).
- fun1(5) = 5, fun1(4) = 9, fun1(3) = 12, fun1(2) = 14, fun1(1) = 15
So fun2's i = 5 + 9 + 12 + 14 + 15 = 55
Answer: 55
fun2(5) adds these values to fun2's i: fun1(5), fun1(4), , fun1(1).
- fun1(5) = 5, fun1(4) = 9, fun1(3) = 12, fun1(2) = 14, fun1(1) = 15
So fun2's i = 5 + 9 + 12 + 14 + 15 = 55
Answer: 55
Consider the following C functions.
The value returned by pp(3,4) is _____
int tob(int b, int* arr){
int i;
for(i=0; b>0; i++){
if(b%2) arr[i]=1;
else arr[i]=0;
b = b/2;
}
return(i);
} int pp(int a,int b) {
int arr[20];
int i, tot = 1, ex, len;
ex = a;
len = tob(b,arr);
for(i=0; i<len; i++){
if(arr[i]==1)
tot = tot * ex;
ex = ex * ex;
}
return(tot);
}The value returned by pp(3,4) is _____
Answer: 81
tob(b, arr) saves the binary bits of b, lowest bit first, and returns how many bits there are. Here b = 4 = 100 arr = [0, 0, 1], len = 3.
pp finds by repeated squaring: ex becomes 3 9 81. tot is multiplied only for a bit equal to 1, the third bit tot = 81.
Answer: 81
pp finds by repeated squaring: ex becomes 3 9 81. tot is multiplied only for a bit equal to 1, the third bit tot = 81.
Answer: 81
Consider the following C program:
#include <stdio.h>
int jumble(int x, int y) {
x = 2 * x + y;
return x;
}
int main() {
int x = 2, y = 5;
y = jumble(y, x);
x = jumble(y, x);
printf("%d\n", x);
return 0;
} The value printed by program is __________ .Answer: 26
The function jumble(x, y) gives back 2x + y.
- First call, y = jumble(5, 2) = 25 + 2 = 12
- Second call, x = jumble(12, 2) = 212 + 2 = 26
Answer: 26
- First call, y = jumble(5, 2) = 25 + 2 = 12
- Second call, x = jumble(12, 2) = 212 + 2 = 26
Answer: 26
Keep practising