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

Functions Questions and Answers

51.

What is the output of code given below?

enum m{JAN, FEB, MAR};
    enum m foo();
    int main()
    {
        enum m i = foo();
        printf("%d\n", i);
    }
    int  foo()
    {
        return JAN;
    }

 

Answer: A

Correct answer: Compile time error

The prototype declares enum m foo(); but the definition is written int foo(). The two conflict, and the compiler rejects the program - even though an enumeration is represented as an integer, the declared types must match.

Please log in or register to report this question.

52.

Which variable has the longest scope?

int b;
    int main()
    {
        int c;
        return 0;
    }
    int a;

 

Answer: B

Correct answer: b

Scope runs from the point of declaration onwards, so b, declared at the very top, is visible through the whole file - the longest scope of the three. a is declared at the bottom and so is visible almost nowhere, and c is local to main.

Please log in or register to report this question.

Answer: D

Correct answer: From the point of declaration to the end of the file being compiled

Scope runs from the point of declaration to the end of the file - a variable declared at the bottom of a file is not visible to functions above it. Being reachable from other files is a matter of linkage, which is a different property; an extern declaration in the other file is what brings it into scope there.

Please log in or register to report this question.

Answer: D

Correct answer: From the point of declaration to the end of the file being compiled

A function’s name is in scope from its declaration to the end of the file - which is exactly why prototypes are put at the top or in a header. Its linkage is external, so other files can call it once they have declared it.

Please log in or register to report this question.

56.

What is the output of the following program?

#include <stdio.h>

void swap(int m, int n)
{
   int x = m;
   
   m = n;
   n = x;
}
main()
{
   int x=5, y=3;

   swap(x,y);
   printf("%d %d", x, y);
}

 

Answer: B

Correct answer: 5 3

C passes arguments by value, so swap receives copies and exchanges the copies. The originals in main are untouched, and the output is 5 3. To swap the caller’s variables the function must take pointers and be called as swap(&x, &y).

Please log in or register to report this question.

57.

The variable declaration with no storage class specified is by default:

Answer: A

Correct answer: auto

With no storage class specified, a variable declared inside a block is auto. (At file scope the default is different: external linkage and static storage duration.)

Please log in or register to report this question.

58.

Can a function return a structure in C?

Answer: A

Correct answer: true

A function may return a structure, and the whole structure is copied back to the caller. It is the standard way of returning several values at once - and it is why the language does not need multiple return values. Returning a large structure copies a good deal of data, so a pointer is often preferred for big ones.

Please log in or register to report this question.

59.

Is it true that too many recursive calls may result into stack overflow?

Answer: A

Yes, too many recursive calls may result into stack overflow. because when a function is called its return address is stored in stack.

After sometime the stack memory will be filled completely. Hence stack overflow error will occur.

Please log in or register to report this question.

60.

What is the output of this C code?

int x = 5;
    void main()
    {
        int x = 3;
        printf("%d", x);
        {
            x = 4;
        }
        printf("%d", x);
    }

 

Answer: D

Correct answer: 3 4

The inner x = 4; is an assignment, not a declaration, so it changes the x already in scope - main's local. The first printf shows 3 and the second, after the block, shows 4. The file-scope x = 5 is shadowed throughout and never seen.

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