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

Functions Questions and Answers

41.

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.

42.

Maximum number of arguments that a function can take is 12

Answer: B

Correct answer: No

The standard sets no limit of twelve. It requires an implementation to support at least 127 parameters, and most allow far more - so the practical limit is the compiler’s, not the language’s. (A function needing many parameters is usually better given a structure.)

Please log in or register to report this question.

43.

What is the output of this C code?

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

 

Answer: A

Correct answer: Junk value

The local int x; shadows the file-scope static, and being an automatic variable with no initialiser it holds whatever was on the stack - a junk value. Only variables with static storage duration are zeroed for you.

Please log in or register to report this question.

44.

Functions cannot return more than one value at a time

Answer: A

Correct answer: True

A C function returns at most one value. Where more is needed the usual devices are to pass pointers for the extra results, or to return a structure containing them all.

Please log in or register to report this question.

45.

What linkage does automatic variables have?

Answer: C

Correct answer: No linkage

An automatic variable has no linkage: its name refers to a fresh object on each entry to the block and cannot be referred to from anywhere else - not from another file, not even from another function. Only file-scope names have linkage, internal or external.

Please log in or register to report this question.

46.

What is the output of this C code?

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

 

Answer: A

Correct answer: 4

The inner block contains an assignment, not a declaration, so it changes the x already in scope. The value printed is 4.

Please log in or register to report this question.

48.

What is the output of this C code?

void foo();
int main()
{
void foo(int);
foo(1);
return 0;
}
void foo(int i)
{
printf("2 ");
}

 

Answer: A

Correct answer: 2

The declaration inside main - void foo(int) - is the one in scope at the call, so foo(1) matches it, and it agrees with the definition. The outer void foo() declares a function with an unspecified parameter list rather than none, so it does not conflict. The program prints 2.

Please log in or register to report this question.

49.

What is the output of code given below?

enum m{JAN, FEB, MAR};
    enum m foo();
    int main()
    {
        enum m i = foo();
        printf("%d\n", i);
    }
    int  foo()
    {
        return JAN;
    }

 

Answer: A

Correct answer: Compile time error

The prototype declares enum m foo(); but the definition is written int foo(). The two conflict, and the compiler rejects the program - even though an enumeration is represented as an integer, the declared types must match.

Please log in or register to report this question.

50.

Which variable has the longest scope?

int b;
    int main()
    {
        int c;
        return 0;
    }
    int a;

 

Answer: B

Correct answer: b

Scope runs from the point of declaration onwards, so b, declared at the very top, is visible through the whole file - the longest scope of the three. a is declared at the bottom and so is visible almost nowhere, and c is local to main.

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