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

Functions Questions and Answers

91.

Is initialization mandatory for local static variables?

Answer: B

Correct answer: NO

Initialisation is optional. A static variable with no initialiser is zero-initialised automatically, since it has static storage duration - unlike an automatic variable, which would hold junk. (If an initialiser is given, it must be a constant expression.)

Please log in or register to report this question.

Answer: E

Correct answer: All of the above

Functions do all four. They let a block of code be written once and used many times; they make a program clearer by giving each task a name; they can be collected into libraries and reused across programs; and because each can be tested on its own, they make debugging far easier.

Please log in or register to report this question.

93.

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.

94.

Functions cannot return a floating point number

Answer: B

Correct answer: No

A function may return any type it likes, floating point included - double sqrt(double) is the obvious example. The statement is false.

Please log in or register to report this question.

96.

Names of functions in two different files linked together must be unique

Answer: A

Correct answer: True

Once two files are linked together they share a single global namespace, so two external functions with the same name collide and the linker reports a duplicate symbol. The way to avoid it is to declare a function static, which confines it to its own file.

Please log in or register to report this question.

97.

What is the output of this C code?

double i;
    int main()
    {
       printf("%g\n",i);
       return 0;
    }

 

Answer: A

Correct answer: 0.000000

A file-scope double is zero-initialised, so it holds 0.0. %g chooses the shorter of the fixed and exponential forms, and for zero the answer is 0 either way.

Please log in or register to report this question.

98.

What is the output of this C code?

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

 

Answer: A

Correct answer: 6 7

A static local is initialised once, before the program starts, and keeps its value between calls - its storage lasts as long as the program even though its name is local. So the first call prints 6 and the second 7. An ordinary local would print 6 both times.

Please log in or register to report this question.

100.

What is the output of this C code?

void main()
    {
        static int x = 3;
        x++;
        if (x <= 5)
        {
            printf("hi");
            main();
        }
    }

 

Answer: D

Correct answer: hi hi

x is static, so it is initialised to 3 once and keeps its value across the recursive calls. It becomes 4 on the first pass (prints hi), 5 on the second (prints hi), and 6 on the third, where the test fails and the recursion stops. So hi appears twice. Had x been ordinary, it would have been reset to 3 on every call and the recursion would never have ended.

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