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 56–60 of 68
What does the following C-statement declare?
int ( * f) (int * ) ; Answer: (C)
Start at the name and move outwards: (*f) tells us that f is a pointer. Next, (int *) tells us it points to a function that takes an int pointer as its argument. The int at the front is the return type of that function.
Answer: A pointer to a function that takes an integer pointer as argument and returns an integer
Answer: A pointer to a function that takes an integer pointer as argument and returns an integer
Consider the following C program:
#include <stdio.h>
typedef struct {
char *a;
char *b;
} t;
void f1 (t s);
void f2 (t *p);
main()
{
static t s = {"A", "B"};
printf ("%s %s\n", s.a, s.b);
f1(s);
printf ("%s %s\n", s.a, s.b);
f2(&s);
}
void f1 (t s)
{
s.a = "U";
s.b = "V";
printf ("%s %s\n", s.a, s.b);
return;
}
void f2(t *p)
{
p -> a = "V";
p -> b = "W";
printf("%s %s\n", p -> a, p -> b);
return;
} What is the output generated by the program ?Answer: (B)
The first line printed is
The function uses call by value, so its changes stay local only.
From is printed.
Back in main, is printed.
Then, in is printed.
So, the answer is (B).
Choose the correct option to fill the ?1 and ?2 so that the program prints an input string in reverse order. Assume that the input string is terminated by a new line character.
#include <stdio.h>
void wrt_it (void);
int main (void)
{
printf("Enter Text");
printf ("\n");
wrt_it();
printf ("\n");
return 0;
}
void wrt_it (void)
{
int c;
if (?1)
wrt_it();
?2
} Answer: (D)
Each call of
So ?1 must read a character into and compare it with the newline:
Trace for the input
Why the others fail:
(A)
(B) The
(C) The test uses before any character is read, so it holds garbage, and
Answer: (D)
wrt_it has its own local variable . A call reads one character into , calls itself for the rest of the line, and only after that returns does it print its own . The printing happens on the way back, so the characters come out in reverse order.So ?1 must read a character into and compare it with the newline:
(c=getchar()) != '\n'. And ?2 must print it: putchar(c);.Trace for the input
abc followed by a newline: the calls read a, b, c. The fourth call reads the newline, the test fails, so it prints the newline and returns. The earlier calls then print c, b, a. The output is cba.Why the others fail:
(A)
getchar takes no argument, so getchar(c); does not compile. Even if it did, nothing is printed.(B) The
; after (c=getchar()) ends the condition early, so != '\n' is a syntax error.(C) The test uses before any character is read, so it holds garbage, and
putchar(c) prints garbage.Answer: (D)
What is the output of the following program?
#include <stdio.h>
int funcf (int x);
int funcg (int y);
main ()
{
int x = 5, y = 10, count;
for (count = 1; count <= 2; ++count) {
y += funcf(x) + funcg(x);
printf ("%d", y);
}
}
funcf (int x) {
int y;
y = funcg(x);
return (y);
}
funcg (int x) {
static int y = 10;
y += 1;
return (y + x);
}
Answer: (A)
The variable inside
Each loop pass calls
Pass 1: static becomes 11, returns . Then it becomes 12, returns . Sum , so . Prints 43.
Pass 2: static becomes 13, returns 18. Then 14, returns 19. Sum , so . Prints 80.
The two printed values are 43 and 80.
Answer: (A)
funcg starts at 10 once and keeps its value between calls. It is a different variable from the in main.Each loop pass calls
funcg(5) twice: once directly and once through funcf. C does not fix which operand of runs first, so either call can get the smaller return value. But the two return values always add up to the same total, so the order does not change the result.Pass 1: static becomes 11, returns . Then it becomes 12, returns . Sum , so . Prints 43.
Pass 2: static becomes 13, returns 18. Then 14, returns 19. Sum , so . Prints 80.
The two printed values are 43 and 80.
Answer: (A)
Consider the following C function:
int f(int n)
{
static int i = 1;
if (n >= 5)
return n;
n = n+i;
i++;
return f(n);
} The value returned by f(1) isAnswer: (C)
The static variable i is created once, with value , and keeps its value across the recursive calls. Each call returns the value of the next call, so follow the calls until .
: , then
: , then
: , then
: , so it returns
So .
Answer: (C)
: , then
: , then
: , then
: , so it returns
So .
Answer: (C)
Keep practising