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

Functions Questions and Answers

61.

 

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.

62.

The output of the code below is

void m(int k)
    {
        printf("hi");
    }
    void m(double k)
    {
        printf("hello");
    }
    void main()
    {
        m(3);
    }

 

Answer: C

Correct answer: Compile time error

C has no function overloading: two functions cannot share a name, whatever their parameters. The compiler reports a redefinition. In C++ this would compile, and m(3) would select the int version.

Please log in or register to report this question.

Answer: B

Correct answer: int sum(int a, int b) {return (a + b);}

A function definition must have its body in braces. The forms without braces are not definitions at all. (The third also uses the old K&R parameter style but omits the type declarations that style requires.)

Please log in or register to report this question.

65.

A function may have any number of return statements each returning different values.

Answer: A

Correct answer: True

A function may contain as many return statements as its logic needs - typically one per branch - and each may return a different value. The first one reached ends the function.

Please log in or register to report this question.

66.

Which of the following is a storage specifier?

Answer: C

Correct answer: auto

The storage class specifiers are auto, register, static, extern and typedef. enum and union introduce types, and volatile is a type qualifier, not a storage class.

Please log in or register to report this question.

67.

In C, a global variable that is not explicitly initialised is set to ________.

Answer: D

Correct answer: zero

Variables with static storage duration, which includes all globals, are zero-initialised before the program starts. It is local variables of automatic storage duration that hold indeterminate values until assigned.

Please log in or register to report this question.

68.

The output of the code below is

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

    void m()
    {

        printf("hello");
    }

 

Answer: A

Correct answer: Error

m is defined after main and not declared before it, so at the call the compiler either rejects it outright or, under the old rules, assumes int m() - and the later definition as void m() then conflicts with that assumption. Either way the program does not compile. Assigning the result of a void function to an int would be an error in its own right.

Please log in or register to report this question.

69.

Usually recursion works slower than loops.

Answer: A

When a recursive call is made, the function/process clones itself and then process that funtion. This leads to time and space constrains.

In a loop, there is no recursive call involved that saves a lot of time and space too.

Please log in or register to report this question.

70.

What is the output of this C code?

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

 

Answer: B

Correct answer: Compile time error

x is declared after main, so it is not in scope where main uses it, and m is neither declared nor defined before its call. The compiler rejects the program on both counts.

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