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

Functions Questions and Answers

51.

What is the output of this C code?

void main()
    {
        m();
        void m()
        {
            printf("hi");
        }
    }

 

Answer: B

Correct answer: Compile time error

m is defined inside main, which standard C forbids - and it is also called before its definition is reached. The compiler rejects the program.

Please log in or register to report this question.

52.

What is the output of this C code?

void main()
    {
        m();
    }
    void m()
    {
        printf("hi");
        m();
    }

 

Answer: C

Correct answer: Infinite hi

m calls itself as its last action, with no condition to stop it - unbounded recursion. It prints hi each time, so the output repeats until the stack is exhausted and the program crashes.

Please log in or register to report this question.

53.

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: A

Correct answer: return-type function-name(argument type);

A declaration (prototype) states the return type, the name and the parameter types, and ends with a semicolon - no body. Adding a body makes it a definition, which is a different thing.

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.

Answer: C

Correct answer: Yes, but we call the function again to get the value, not as convenient as in using variable

C does allow it: a parameter written as a function is adjusted to a pointer to that function, so void wow(int func()) means void wow(int (*func)()). But what is passed is the function itself, not a value - so inside wow you must call it to obtain anything, which is less convenient than passing the value. This is the mechanism behind callbacks, such as the comparison function given to qsort.

Please log in or register to report this question.

58.

The value computed in a function is given back to the caller using the ________ keyword.

Answer: A

Correct answer: return

return both ends the function and passes the value back. static, new and volatile have nothing to do with it - and new is not even a C keyword.

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.

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