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

Functions Questions and Answers

61.

What is the output of this code having void return-type function?

void foo()
    {
        return 1;
    }

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

 

Answer: C

Correct answer: Compile time error

A void function must not return a value, so return 1; is an error - and assigning the result of a void function to an int is a second one. Either alone would stop the program compiling.

Please log in or register to report this question.

62.

What will be the data type returned for the following function?

int func()
    {
        return (double)(char)5.0;
    }

 

Answer: B

Correct answer: int

The declared return type wins. Whatever casting happens inside, the value is converted to the function’s return type on the way out - so func returns an int. Here 5.0 becomes a char, then a double, and finally an int at the return.

Please log in or register to report this question.

Answer: D

Correct answer: All of the mentioned

C has no function overloading: one name may denote only one function in a translation unit. So none of the three variations is allowed - not a different signature, not a different return type, not a different number of parameters. (C++ permits overloading on the parameters, though still not on the return type alone.)

Please log in or register to report this question.

64.

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.

65.

The output of the code below is

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

 

Answer: A

Correct answer: 5

int *p = 5; puts the value 5 into a pointer - meaningless as an address, but the bit pattern is simply carried back and printed with %d as 5. The code compiles only with warnings, and would crash the moment anything dereferenced p.

Please log in or register to report this question.

66.

The output of the code below is

int *m();
    void main()
    {
        int *k = m();
        printf("hello ");
        printf("%d", k[0]);
    }
    int *m()
    {
        int a[2] = {5, 8};
        return a;
    }

 

Answer: C

Correct answer: hello followed by garbage value

a is a local array, and it ceases to exist when m returns - so the pointer returned is dangling. Reading k[0] reads memory that has been released and may already have been reused, giving whatever happens to be there. To return an array safely it must be static, or allocated with malloc, or supplied by the caller.

Please log in or register to report this question.

67.

The output of the code below is

int *m();
    void main()
    {
        int k = m();
        printf("%d", k);
    }
    int *m()
    {
        int a[2] = {5, 8};
        return a;
    }

 

Answer: D

Correct answer: Varies

Two faults compound each other. The function returns a pointer to a local array that no longer exists, and the caller assigns that pointer to an int. What is printed is an address reinterpreted as an integer - different on every run and every machine.

Please log in or register to report this question.

68.

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.

69.

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