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 31–35 of 68
Suppose c=(c[0],...,c[k-1]) is an array of length k, where all the entries are from the set {0,1}. For any positive integers a and n, consider the following pseudocode.
If k=4, c=(1,0,1,1), a=2 and n=8, then the output of DOSOMETHING(c,a,n) is _____.
DOSOMETHING(c, a, n)
z ← 1
for i ← 0 to k − 1
do z ← z² mod n
if c[i] = 1
then z ← (z × a) mod n
return z If k=4, c=(1,0,1,1), a=2 and n=8, then the output of DOSOMETHING(c,a,n) is _____.
Answer: 0
Start with z = 1, a = 2, n = 8 and c = (1, 0, 1, 1):
- i = 0: z = 1, c = 1 z = 2
- i = 1: z = 4, c = 0
- i = 2: z = 16 mod 8 = 0, c = 1 z = 0
- i = 3: z = 0
(The code finds: .)
Answer: 0
- i = 0: z = 1, c = 1 z = 2
- i = 1: z = 4, c = 0
- i = 2: z = 16 mod 8 = 0, c = 1 z = 0
- i = 3: z = 0
(The code finds: .)
Answer: 0
Consider the function func shown below:
int func(int num) {
int count = 0;
while (num) {
count++;
num >>= 1;
}
return (count);
} The value returned by func(435)is __________.Answer: 9
Each pass of the loop shifts num right by one bit. It stops when num is 0. So the function counts how many bits num has.
(256 + 128 + 32 + 16 + 2 + 1) 9 bits.
Answer: 9
(256 + 128 + 32 + 16 + 2 + 1) 9 bits.
Answer: 9
Consider the following function
double f (double x) {
if ( abs (x*x - 3) < 0.01) return x;
else return f (x / 2 + 1.5/x);
} Give a value q (to 2 decimals) such that f(q) will return q:______Answer: 1.72 to 1.74
The function gives back x only when . This means . (The step x/2 + 1.5/x in the recursion is Newton's method used to find .)
Take q = 1.73. Then and so f(1.73) returns 1.73.
Answer: 1.73
Take q = 1.73. Then and so f(1.73) returns 1.73.
Answer: 1.73
Consider the C function given below
int f(int j)
{
static int i = 50;
int k;
if (i == j)
{
printf("something");
k = f(i);
return 0;
}
else return 0;
} Which one of the following is TRUE?Answer: (D)
The variable i is static, so it keeps the value 50. Take j = 50. Then the check i == j is true, and the function calls f(i), which is f(50), once more. In that call i == j is true again, so the same thing repeats. The recursion never stops, and the stack overflows.
For any other value of j, the function just returns 0.
Answer: The function will exhaust the runtime stack or run into an infinite loop when j = 50
For any other value of j, the function just returns 0.
Answer: The function will exhaust the runtime stack or run into an infinite loop when j = 50
What is the return value of f(p,p), if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value.
int f (int &x, int c) {
c = c - 1;
if (c==0) return 1;
x = x + 1;
return f(x,c) * x;
} Answer: (B)
Here x is the same variable as p, because it is passed by reference. c is only a copy. Every call adds 1 to x before it recurses. It multiplies by x only after the call returns.
The value of p changes like this: 5 6 7 8 9. After that c becomes 0 and the call returns 1. While returning, each of the 4 levels multiplies by x, and x is now 9.
Result:
Answer: 6561
The value of p changes like this: 5 6 7 8 9. After that c becomes 0 and the call returns 1. While returning, each of the 4 levels multiplies by x, and x is now 9.
Result:
Answer: 6561
Keep practising