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

Functions Questions and Answers

1.

Consider the following C program.

void f(int, short);
void main()
{
     int i = 100;
     short s = 12;
     short *p = &s;
     __________ ;    // call to f()
}

Which one of the following expressions, when placed in the blank above, will NOT result in a type checking error?

Exam Year(s)

GATE CS - 2016,

Answer: D

The call that type-checks is f(i,*p).

f is declared as taking an int and a short and returning void. \(i\) is an int, and p is a pointer to short, so *p is a short: the arguments match exactly.

The other choices fail. f(s,*s) and f(i,*s) dereference s, which is a short, not a pointer, so *s is a type error. i = f(i,s) has valid arguments but assigns the result of a void function to an int, which is also a type error.

Please log in or register to report this question.

2.

What will be the output of the following C program?

void count(int n){
    static int d=1;

    printf("%d ", n);
    printf("%d ", d);
    d++;
    if(n\(\gt\)1) count(n-1);
    printf("%d ", d);
}

void main(){
    count(3);
}

Exam Year(s)

GATE CS - 2016,

Answer: A

The output is 3 1 2 2 1 3 4 4 4.

The key is that d is static: there is one copy shared by every call, initialized once and never reset.

  1. count(3) prints 3 and d = 1, then d becomes 2, and calls count(2).
  2. count(2) prints 2 and d = 2, then d becomes 3, and calls count(1).
  3. count(1) prints 1 and d = 3, then d becomes 4; since n is not greater than 1 it does not recurse, and prints d = 4.
  4. Back in count(2), it prints d, now 4; back in count(3), it prints d, 4.

So the sequence is 3 1, 2 2, 1 3, then 4 4 4. Treating d as an ordinary local variable, reset to 1 in each call, gives 3 1 2 1 1 1 2 2 2; the seven-number options forget that every call prints d once more as it returns.

Please log in or register to report this question.

3.

Consider the following C program:

#include \(\lt\)stdio.h\(\gt\)
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?

Exam Year(s)

GATE CS - 2024,

Answer: C

The program terminates with 4321 as output.

Each call to fX reads one character into its own local variable a. If it is not a newline, it calls fX again before printing. So the calls nest: the first reads '1', the second '2', the third '3', the fourth '4', and the fifth reads the newline and does not recurse.

Now the calls return in reverse order. The fifth call holds the newline and prints nothing. The fourth prints '4', the third '3', the second '2' and the first '1'. Output: 4321. The recursion depth is bounded by the input length, so the program terminates, and it does produce output; printing happens after the recursive call, which is why the order is reversed rather than 1234.

Please log in or register to report this question.

4.

Consider the following C program. Assume parameters to a function are evaluated from right to left.

#include \(\lt\)stdio.h\(\gt\)

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?

Exam Year(s)

GATE CS - 2024,

Answer: A

The output is 20101020.

In f(g(10),h(20)) the arguments are evaluated before f runs, and the question fixes their order as right to left. So h(20) runs first and prints 20, then g(10) prints 10; they return 20 and 10, which become y and x.

Inside f, g(x) prints 10 and then h(y) prints 20. No separators are printed, so the whole output reads 20, 10, 10, 20 run together: 20101020.

10202010 evaluates the arguments left to right and then swaps the body. 20102010 gets the arguments right but reverses the calls inside f, and 10201020 is the answer for left-to-right evaluation.

Please log in or register to report this question.

5.

Consider the following program:

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?

Exam Year(s)

GATE CS - 2023,

Answer: A

The activation tree is (A).

An activation tree has one node per call, with each call's children in the order it makes them. Trace the program:

  • main calls f1(), then f2(2), then f3(): three children, in that order.
  • f2(2) first calls f3(); since X = 2 it then calls f2(1).
  • f2(1) first calls f3(); since X = 1 it then calls f1() and returns.

So main has children f1, f2, f3; that f2 has children f3 and f2; and the inner f2 has children f3 and f1. This is tree (A).

(B) stops after one level of f2 and misses the recursive call f2(1). (C) makes f2 a child of f1, but f1 calls nothing. (D) puts f3, f2 and f1 all under the first f2, when f1 is called by the inner f2(1).

Please log in or register to report this question.

Answer: C

The false statement is Generally recursive version of programs take less space over iterative counter part.

Each recursive call pushes a new activation record, with its parameters, local variables and return address, onto the stack. A recursion of depth n therefore uses stack space proportional to n, while an equivalent loop reuses the same variables. Recursive versions generally take more space, not less.

The other statements are true: any recursive program can be rewritten iteratively, if necessary with an explicit stack, and recursion often simplifies the logic, as in tree traversal or the Tower of Hanoi. Option D wrongly includes the true statement A.

Please log in or register to report this question.

7.

All subtasks in a program can be implemented as functions, either as functions that return a value or as ....................

Exam Year(s)

MH SET - 2018, SET Exam - 2018,

Answer: B

The answer is void-functions.

In C and C++, a function either returns a value of some type, or is declared with return type void and returns nothing, performing an action such as printing output or modifying data through pointers or references. Together these two kinds cover every subtask a program can be split into.

'Return-function' and 'call functions' are not categories of function: every function is called and every value-returning function returns. 'All of the above' therefore cannot be right.

Please log in or register to report this question.

8.

A ........................ is a program that does nothing but test a function.

Exam Year(s)

MH SET - 2018, SET Exam - 2018,

Answer: D

Such a program is called a driver program.

A driver is a small test program whose only job is to call the function under test with chosen arguments and display the results, so that the function can be checked on its own before it is placed in a larger program. Its counterpart is a stub, a simplified stand-in for a function that is not yet written.

A 'hello' program is the classic first program that prints a greeting, a print program only produces output, and an interrupt program (an interrupt service routine) responds to hardware events; none of them exists to test a function.

Please log in or register to report this question.

9.

Function declaration gives the name of the function. Which of the following statements are relevant for function :

(i) The semantics of argument passing are identical to semantics of initialisation.
(ii) The argument types are checked and implicit argument type conversion takes place when necessary.
(iii) The argument types are unchecked and explicit argument type conversion takes place when necessary.
(iv) A function declaration may contain argument names

Exam Year(s)

MH SET - 2020, SET Exam - 2020,

Answer: B

The answer is (i), (ii) and (iv).

These follow Stroustrup's description of C++ functions. Argument passing has the same semantics as initialisation (i); the types of arguments are checked against the declaration and implicit conversions are applied where needed (ii); and a declaration may include argument names, though the compiler ignores them (iv).

Statement (iii) is the opposite of (ii): arguments are not unchecked, and conversions are implicit, not explicit. So any option including (iii) is wrong, and (i) and (ii) or (ii) and (iv) alone leave out a true statement.

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.

Loading…
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