Machine-translated by Google. Questions are authored in English.
Top

Functions Questions and Answers

11.

What will happen if a value is returned from a function declared with void return type in C ?

Exam Year(s)

Tripura Public Service Commission - 2026,

Answer: C

The keyed result is a compilation error.

A function declared void promises to return no value. The C standard makes a return statement with an expression in a void function a constraint violation, so the compiler must issue a diagnostic, and C++ compilers and strict C compilers reject it outright.

The value is not printed automatically, since returning a value never prints anything. The problem is caught when the program is compiled, not when it runs, so 'runtime error' is wrong; and the compiler does not silently ignore it.

Please log in or register to report this question.

12.

What is the value returned by the recursive function ackermann(2,3) ?

int ackermann(int i, int j){
if(i= =0) return 2*j;
else if (j= =1) return 2;
else return ackermann(i-1, ackermann(i, j-1));
}

Exam Year(s)

Tripura Public Service Commission - 2026,

Answer: A

ackermann(2,3) returns 16.

With i = 1 the function doubles repeatedly: ackermann(1, j) = \(2^{j}\), since ackermann(1,1) = 2 and each step applies \(2 \times\) to the previous value. Now unwind ackermann(2,3).

\[ \begin{aligned} a(2,1) &= 2 \\ a(2,2) &= a(1, a(2,1)) = a(1,2) = 4 \\ a(2,3) &= a(1, a(2,2)) = a(1,4) \\ &= 2^{4} = 16 \end{aligned} \]

Check a(1,4): it calls a(0, a(1,3)), and a(1,3) = 8, so the result is \(2 \times 8 = 16\). Options 20, 25 and 30 do not come from any correct reading of the recursion.

Please log in or register to report this question.

13.

What is the total number of recursive calls/invocations to compute fib(8) using the function ?

int fib(int n){
if (n = =1) return 1;
else if(n= =2) return 1;
else return(fib(n-1)+fib(n-2));
}

Exam Year(s)

Tripura Public Service Commission - 2026,

Answer: D

Computing fib(8) takes 41 calls in all.

Let C(n) be the number of invocations for fib(n), counting the call itself. fib(1) and fib(2) make one call each; any larger n makes one call plus the calls for n-1 and n-2.

\[ \begin{aligned} C(n) &= C(n-1) + C(n-2) + 1 \\ C(3) &= 3,\ C(4) = 5,\ C(5) = 9 \\ C(6) &= 15,\ C(7) = 25 \\ C(8) &= 25 + 15 + 1 = 41 \end{aligned} \]

A shortcut: \(C(n) = 2F(n) - 1\), and fib(8) = 21 gives \(2 \times 21 - 1 = 41\). 21 is the value fib(8) returns, not the number of calls, and 13 is fib(7).

Please log in or register to report this question.

Answer: A

Correct answer: Declared within the scope of a block, usually a function

An automatic variable is one declared inside a block. It comes into existence when the block is entered and disappears when it is left. Declaring it outside all functions would make it static; writing auto is optional, since it is the default.

Please log in or register to report this question.

Answer: D

Correct answer: Both (a) and (b)

An automatic variable is visible from its declaration to the end of the block that contains it, and that includes any blocks nested inside - unless one of them declares another variable of the same name, which shadows it.

Please log in or register to report this question.

16.

Functions can be called either by value or reference

Answer: A

Correct answer: True

C passes everything by value, but passing a pointer by value gives the function the caller’s address and so the effect of call by reference. Both styles are therefore available, which is why arrays - whose names decay to pointers - behave as though passed by reference.

Please log in or register to report this question.

17.

What is the output of the below code snippet?

#include <stdio.h>

main() 
{
   unsigned x = 5, y=&x, *p = y+0; 

   printf("%u",*p);
}

 

Answer: D

Correct answer: 5

y holds the address of x, and p is set to that same address, so *p is x itself - 5. (The code stores an address in an unsigned int, which is poor practice and provokes warnings; it happens to work only where a pointer and an unsigned int are the same width.)

Please log in or register to report this question.

18.

What is the output of this C code?
 

void m();
void n()
{
m();
}
void main()
{
void m()
{
printf("hi");
}
}

 

Answer: B

Correct answer: Compile time error

Two faults. Standard C does not allow a function to be defined inside another, and m is defined inside main here. And n, which calls m, is never called at all - so even if it compiled, nothing would be printed.

Please log in or register to report this question.

19.

Is an assignment statement that gives a value to a local static variable executed only once?

Answer: B

Correct answer: false

The initialisation of a static variable happens once, before the program starts. But an ordinary assignment to it inside the function is an executable statement like any other, and runs every time the function is called - which would defeat the whole point of making the variable static.

Please log in or register to report this question.

Answer: A

Correct answer: return-type function-name(argument type);

A declaration (prototype) states the return type, the name and the parameter types, and ends with a semicolon - no body. Adding a body makes it a definition, which is a different thing.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Functions Questions and Answers || Functions MCQ Questions and Answers || Functions GK Questions and Answers || Functions GK MCQ Questions || Functions Multiple Choice Questions and Answers || Functions GK || GK on Functions || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming