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

Functions Questions and Answers

92.

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.

94.

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.

95.

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.

96.

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.

97.

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.

98.

What will happen after compiling and running following code?

main()
{
  printf("%p", main);
}

 

Answer: C

Function names are just addresses (just like array names are addresses).
main() is also a function. So the address of function main will be printed. %p in printf specifies that the argument is an address. They will be printed as hexadecimal numbers.

Please log in or register to report this question.

Answer: B

Correct answer: External

A function in C has external linkage by default: its name is visible to every other file linked with it. That is why two files cannot define functions of the same name, and why static is used to confine a function to its own file.

Please log in or register to report this question.

100.

What is the default return type if it is not specified in function definition?

Answer: B

Correct answer: int

Under the older rules an omitted return type defaults to int. C99 removed the implicit int, so a modern compiler warns or refuses - but the default, where it is honoured, is int.

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