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 6–10 of 68
Consider the following C program:
#include <stdio.h>
void fX();
int main(){
fX();
return 0;}
void fX(){
char a;
if((a=getchar()) != '\n')
fX();
if(a != '\n')
putchar(a);} Assume that the input to the program from the command line is 1234 followed by a newline character. Which one of the following statements is CORRECT?Answer: (C)
Every call of fX() reads one character. If the character is not a newline, it calls fX() again before it prints anything. Each call keeps its own local variable .
The calls read: '1' '2' '3' '4' '\n'. The last call reads the newline. It makes no new call and prints nothing.
As the calls return, they print their characters in reverse order: 4, 3, 2, 1. After that, main returns normally.
Answer: The program will terminate with 4321 as output
The calls read: '1' '2' '3' '4' '\n'. The last call reads the newline. It makes no new call and prints nothing.
As the calls return, they print their characters in reverse order: 4, 3, 2, 1. After that, main returns normally.
Answer: The program will terminate with 4321 as output
Consider the following C program. Assume parameters to a function are evaluated from right to left.
#include <stdio.h>
int g(int p) { printf("%d", p); return p; }
int h(int q) { printf("%d", q); return q; }
void f(int x, int y) {
g(x);
h(y);
}
int main() {
f(g(10),h(20));
} Which one of the following options is the CORRECT output of the above C program?Answer: (A)
The arguments of f are evaluated from right to left:
- First, h(20) is called and it prints 20
- Next, g(10) is called and it prints 10
Now f(10, 20) runs:
- g(x) prints 10
- h(y) prints 20
Output: 20101020
Answer: 20101020
- First, h(20) is called and it prints 20
- Next, g(10) is called and it prints 10
Now f(10, 20) runs:
- g(x) prints 10
- h(y) prints 20
Output: 20101020
Answer: 20101020
The integer value printed by the ANSI-C program given below is ______.
#include <stdio.h>
int funcp(){
static int x = 1;
x++;
return x;
}
int main(){
int x,y;
x = funcp();
y = funcp()+x;
printf("%d\n", (x+y));
return 0;
}
Answer: 7
The static x inside funcp keeps its value from one call to the next. It starts at 1.
- First call, x = funcp() the static x becomes 2 so x = 2
- Second call, y = funcp() + x the static x becomes 3 so y = 3 + 2 = 5
Then x + y = 7
Answer: 7
- First call, x = funcp() the static x becomes 2 so x = 2
- Second call, y = funcp() + x the static x becomes 3 so y = 3 + 2 = 5
Then x + y = 7
Answer: 7
Consider the following program:
Which one of the following options represents the activation tree corresponding to the main function?
int main()
{
f1();
f2(2);
f3();
return(0);
}
int f1()
{
return(1);
}
int f2(int X)
{
f3();
if (X==1)
return f1();
else
return (X*f2(X-1));
}
int f3()
{
return(5);
}
Which one of the following options represents the activation tree corresponding to the main function?
Answer: (A)
Follow the function calls:
- main makes three calls: f1(), f2(2) and f3() so it has 3 children.
- f2(2) first calls f3(). Because X 1, it then calls f2(1).
- f2(1) first calls f3(). Because X == 1, it then calls f1().
The tree is: main {f1, f2, f3}; f2 {f3, f2}; the inner f2 {f3, f1}. This is the tree in (A).
Answer: A
- main makes three calls: f1(), f2(2) and f3() so it has 3 children.
- f2(2) first calls f3(). Because X 1, it then calls f2(1).
- f2(1) first calls f3(). Because X == 1, it then calls f1().
The tree is: main {f1, f2, f3}; f2 {f3, f2}; the inner f2 {f3, f1}. This is the tree in (A).
Answer: A
Consider the following ANSI C function:
int SimpleFunction(int Y[], int n, int x)
{
int total = Y[0], loopIndex;
for (loopIndex=1; loopIndex<=n-1; loopIndex++)
total=x*total +Y[loopIndex];
return total;
} Let Z be an array of 10 elements with Z[i]=1, for all i such that . The value returned by SimpleFunction(Z,10,2) is __________Answer: 1023
This function uses Horner's rule. It finds the value of .
Now take Y[i] = 1 for every i, x = 2 and n = 10:
Answer: 1023
Now take Y[i] = 1 for every i, x = 2 and n = 10:
Answer: 1023
Keep practising