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

Functions Questions and Answers

81.

What is the output of this C code?

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

 

Answer: A

Correct answer: 9

x = 9; assigns to the file-scope static variable, and the inner block’s int x = 4; declares a different variable that vanishes at the closing brace. So the printf, back outside, shows 9.

Please log in or register to report this question.

83.

Automatic variables are allocated space in the form of a:

Answer: A

Correct answer: stack

Automatic variables are allocated on the stack: space is reserved when the block is entered and released when it is left, which is what makes their lifetime match the block and what makes recursion possible - each call gets its own frame.

Please log in or register to report this question.

84.

What is the output of this C code?

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

 

Answer: A

Correct answer: 8 3

main declares a local x = 3, which shadows the file-scope x = 5 inside main only. m is outside that scope, so its x = 8 assigns to the global, and n prints that 8. Back in main, the local is still 3.

Please log in or register to report this question.

85.

The output of the code below is

int *m();
    void main()
    {
        int *k = m();
        printf("hello ");
        printf("%d", k[0]);
    }
    int *m()
    {
        int a[2] = {5, 8};
        return a;
    }

 

Answer: C

Correct answer: hello followed by garbage value

a is a local array, and it ceases to exist when m returns - so the pointer returned is dangling. Reading k[0] reads memory that has been released and may already have been reused, giving whatever happens to be there. To return an array safely it must be static, or allocated with malloc, or supplied by the caller.

Please log in or register to report this question.

86.

What is the output of this C code?

int main()
{
void foo();
void f()
{
foo();
}
f();
}
void foo()
{
printf("2 ");
}

 

Answer: D

Correct answer: Depends on the compiler

The program tries to define one function inside another, which standard C does not allow - so strictly it should not compile. GCC, however, offers nested functions as an extension, and there it compiles and prints 2. The result therefore depends on which compiler is used.

Please log in or register to report this question.

87.

What will be the data type returned for the following function?

int func()
    {
        return (double)(char)5.0;
    }

 

Answer: B

Correct answer: int

The declared return type wins. Whatever casting happens inside, the value is converted to the function’s return type on the way out - so func returns an int. Here 5.0 becomes a char, then a double, and finally an int at the return.

Please log in or register to report this question.

89.

What is the output of this C code?

void main()
    {
        static int x;
        printf("x is %d", x);
    }

 

Answer: D

Correct answer: 0

A static variable, whether at file scope or inside a function, is zero-initialised if given no initialiser. So it prints 0 - unlike an ordinary automatic variable, which would hold junk.

Please log in or register to report this question.

90.

The output of the code below is

int *m()
    {
        int *p = 5;
        return p;
    }
    void main()
    {
        int *k = m();
        printf("%d", k);
    }

 

Answer: A

Correct answer: 5

int *p = 5; puts the value 5 into a pointer - meaningless as an address, but the bit pattern is simply carried back and printed with %d as 5. The code compiles only with warnings, and would crash the moment anything dereferenced p.

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