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 1–5 of 68
Consider the following program in C:
Note: Assume that the program compiles and runs successfully.
#include <stdio.h>
void func(int i, int j) {
if(i < j) {
int i = 0;
while (i < 10) {
j += 2;
i++;
}
}
printf("%d", i);
}
int main() {
int i = 9, j = 10;
func(i, j);
return 0;
} The output of the program is ________. (answer in integer)Note: Assume that the program compiles and runs successfully.
Answer: 9
Follow the call . Inside func, the parameters are . They are separate copies of the variables in main.
1. The condition , that is , is true. So the program goes into the block.
2. In the block, a new variable is declared. It is local to this block. It hides the outer parameter only inside the block.
3. The while loop keeps running while the inner . It runs times. Each time it does , which changes the outer parameter . It also adds 1 to the inner , which goes from up to . Then the loop stops.
4. When the block ends, the inner is gone. Nothing in the block ever changed the outer parameter , whose value is .
5. uses the outer parameter , which is still .
Answer: 9
1. The condition , that is , is true. So the program goes into the block.
2. In the block, a new variable is declared. It is local to this block. It hides the outer parameter only inside the block.
3. The while loop keeps running while the inner . It runs times. Each time it does , which changes the outer parameter . It also adds 1 to the inner , which goes from up to . Then the loop stops.
4. When the block ends, the inner is gone. Nothing in the block ever changed the outer parameter , whose value is .
5. uses the outer parameter , which is still .
Answer: 9
Consider the recursive functions represented by the following code segment:
Note: Ignore syntax errors (if any) in the function.
int bar(int n) {
if (n == 1) return 0;
else return 1 + bar(n/2);
}
int foo(int n) {
if (n == 1) return 1;
else return 1 + foo(bar(n));
} The smallest positive integer n for which returns is ________. (answer in integer) Note: Ignore syntax errors (if any) in the function.
Answer: 65536
Note that halves again and again until it reaches . It counts the halvings, which is .
for , and . So is plus the number of times is applied (taking ) to until we get .
Find the smallest for each value of :
- : smallest .
- : need , that is : smallest .
- : need : smallest .
- : need : smallest .
- : need : smallest .
Answer: 65536
for , and . So is plus the number of times is applied (taking ) to until we get .
Find the smallest for each value of :
- : smallest .
- : need , that is : smallest .
- : need : smallest .
- : need : smallest .
- : need : smallest .
Answer: 65536
Consider the following ANSI-C function.
Note: Ignore syntax errors (if any) in the function.
int func(int start, int end){
int length=end+1-start;
if((length<1)||(start<0)||(end<0)){ return(0); }
if(length%3==0){
return(func(start+1, end));
}else if(length%3==1){
return(1+func(start, end-1));
}else {
return(func(start+2, end));
}
} The maximum possible value that can be returned from this function is ________. (answer in integer) Note: Ignore syntax errors (if any) in the function.
Answer: 1
Follow how changes in each recursive call. Only the branch adds to the result. The other two branches just call the function again and add nothing:
- If the current length is : call again with the length smaller by (nothing is added). The new length is .
- If the current length is : add and call again with the length smaller by . The new length is .
- If the current length is : call again with the length smaller by (nothing is added). The new length is .
Once the length enters the cycle , it can never come back to residue . The only moves from and both stay inside this same two-value cycle. So the "" branch can run only once in any chain of calls. This happens only if the length of the very first call is . After that one possible addition, the calls stay in the cycle, which adds nothing, until the base case ends them.
So the maximum value that can be returned for any valid input is .
Answer: 1
- If the current length is : call again with the length smaller by (nothing is added). The new length is .
- If the current length is : add and call again with the length smaller by . The new length is .
- If the current length is : call again with the length smaller by (nothing is added). The new length is .
Once the length enters the cycle , it can never come back to residue . The only moves from and both stay inside this same two-value cycle. So the "" branch can run only once in any chain of calls. This happens only if the length of the very first call is . After that one possible addition, the calls stay in the cycle, which adds nothing, until the base case ends them.
So the maximum value that can be returned for any valid input is .
Answer: 1
#include <stdio.h>
int foo(int S[],int size){
if(size == 0) return 0;
if(size == 1) return 1;
if(S[0] != S[1]) return 1+foo(S+1,size-1);
return foo(S+1,size-1);
}
int main(){
int A[]={0,1,2,2,2,0,0,1,1};
printf("%d",foo(A,9));
return 0;
}
The value printed by the given C program is _________. (Answer in integer).Answer: 5
Trace the recursion on . Each element is compared with the one next to it. Add each time they are different:
Now go back up the calls: , , , , , , , .
Answer: 5
Now go back up the calls: , , , , , , , .
Answer: 5
Consider the following C program:
#include <stdio.h>
int g(int n) {
return (n+10);
}
int f(int n) {
return g(n*2);
}
int main() {
int sum, n;
sum=0;
for (n=1; n<3; n++)
sum += g(f(n));
printf ("%d", sum);
return 0;
}
The output of the given C program is ________. (Answer in integer)Answer: 46
Functions: and .
Loop runs from up to :
First pass : ; ;
Second pass : ; ;
Answer: 46
Loop runs from up to :
First pass : ; ;
Second pass : ; ;
Answer: 46
Keep practising