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

Functions Questions and Answers

91.

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.

92.

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.

94.

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.

95.

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.

96.

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.

98.

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.

Answer: D

Correct answer: None of the mentioned

All three are legal. Declaring a function before main and defining it afterwards is the normal pattern; a stray semicolon after a definition is an empty declaration, which is harmless; and a declaration inside a function is allowed, its scope being that block.

Please log in or register to report this question.

100.

The keyword used to transfer control from a function back to the calling function is

Answer: D

Correct answer: return

return ends the function and hands control - and optionally a value - back to the caller. goto can only jump within the same function, and there is no go back keyword.

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