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

Functions Questions and Answers

101.

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.

102.

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.

104.

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.

105.

Property of external variable to be accessed by any source file is called by C90 standard as:

Answer: A

Correct answer: external linkage

Linkage is the term for whether a name refers to the same object across translation units; scope is where a name is visible within one. A file-scope variable has external linkage - the same object however many files declare it.

Please log in or register to report this question.

106.

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.

107.

Which part of the program address space is p stored in the code given below?

int *p = NULL;
    int main()
    {
        int i = 0;
        p = &i;
        return 0;
    }

 

Answer: B

Correct answer: Data segment

p is a file-scope variable with an explicit initialiser, so it goes in the data segment, which holds initialised statics and whose contents come from the executable file. (Strictly, a pointer initialised to NULL is all-zero and many toolchains put it in BSS after all; the distinction the question is drawing is between explicitly initialised and not.)

Please log in or register to report this question.

108.

What is the output of this C code?

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

 

Answer: D

Correct answer: 0

A variable at file scope has static storage duration, and every such object is zero-initialised before the program starts - it is not left holding junk, as an uninitialised automatic variable would be. So the value printed is 0.

Please log in or register to report this question.

109.

Which of the following is an external variable?

int func (int a)
    {
        int b;
        return b;
    }
    int main()
    {
        int c;
        func (c);
    }
    int d;

 

Answer: D

Correct answer: d

d is declared at file scope, so it is the external variable - even though it appears at the end of the file, which merely limits how much of the file can see it. a is a parameter, and b and c are locals; all three are automatic.

Please log in or register to report this question.

110.

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.

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