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

Functions Questions and Answers

31.

Can variable i be accessed by functions in another source file?

int i;
    int main()
    {
        printf("%d\n", i);
    }

 

Answer: D

Correct answer: true

A variable at file scope has external linkage by default, so another source file can reach it by declaring extern int i;. Adding static to the definition is what would prevent that.

Please log in or register to report this question.

32.

If return type for a function is not specified, it defaults to int

Answer: A

Correct answer: True

Under the older rules an omitted return type defaulted to int - which is why so much old code writes main() rather than int main(). C99 removed the default, so a modern compiler will at least warn.

Please log in or register to report this question.

Answer: C

Correct answer: All of the mentioned

All three are true of a static local variable. It keeps its value between calls, because it is initialised once and its storage lasts as long as the program; it therefore still exists after the function returns; and although its name is local, its address can be passed to another function, which may then modify it through the pointer.

Please log in or register to report this question.

34.

What is the notation for the following functions?

1.  int f(int a, float b)
    {
        /* Some code */
    }

2.  int f(a, b)
    int a; float b;
    {
        /* Some code */
    }

 

Answer: C

Correct answer: 1. ANSI Notation 2. KR Notation

The first form declares each parameter’s type inside the parentheses - the ANSI (prototype) style, introduced by the 1989 standard. The second lists only the names in the parentheses and declares their types between the parentheses and the body - the old Kernighan and Ritchie style. The ANSI form matters because it lets the compiler check the arguments at every call; the K&R form does not, and is now obsolete.

Please log in or register to report this question.

36.

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.

37.

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.

38.

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.

39.

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.

40.

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.

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