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: 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.

8.

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.

9.

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.

10.

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.

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