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

Functions Questions and Answers

101.

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.

102.

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.

103.

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.

105.

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.

107.

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.

108.

What will be the output?

int main()
    {
        printf("%d", d++);
    }
    int d = 10;

 

Answer: D

Correct answer: Compile time error

d is declared after main, so at the point of use it is not in scope. Under C99 and later an undeclared identifier is an error; older compilers would have assumed int and then found the later definition consistent, but the modern answer is a compile error.

Please log in or register to report this question.

109.

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

Answer: D

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

Example:

#include
int add(int, int); /* Function prototype */

int main()
{
    int a = 4, b = 3, c;
    c = add(a, b);
    printf("c = %d\n", c);
    return 0;
}
int add(int a, int b)
{
/* returns the value and control back to main() function */
   return (a+b);
}

Output: c = 7

Please log in or register to report this question.

110.

What is the output of this C code?

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

 

Answer: A

Correct answer: 3 3

Here the inner int x = 4; is a declaration, so it creates a new variable that shadows the outer one for the length of the block and then disappears. main’s own x is untouched, and both printf calls show 3.

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