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

C Constants Questions and Answers

31.

What is the output of this C code?

#include <stdio.h>
    int main()
    {
        const int p;
        p = 4;
        printf("p is %d", p);
        return 0;
    }

 

Answer: B

Since the constant variable has to be declared and defined at the same time, not doing it results in an error.

Output:
$ cc pgm10.c
pgm10.c: In function ‘main’:
pgm10.c:5: error: assignment of read-only variable ‘p’

Please log in or register to report this question.

32.

In C programming language, if the first and the second operands of operator + are of types int and float, respectively, the result will be of type

Answer: B

Correct answer: float

The usual arithmetic conversions apply: when the operands of a binary operator have different types, the one of lower rank is converted to the type of the other before the operation, and the result has that common type.

Here int is converted to float, so the addition is performed in floating point and the result is float. The rule is why integer division is such a common source of surprise - 1/2 is 0 because both operands are int, whereas 1/2.0 is 0.5.

Please log in or register to report this question.

33.

What is the output of the following 'C' program?
main()
{
extern int a;
printf("\n%d",a);
}
int a=20;

Answer: B

Correct answer: 20

The extern int a; declaration inside main announces that a exists with external linkage and is defined elsewhere. The definition int a = 20; appears later in the same file, which is perfectly acceptable - the linker, not the compiler, resolves the reference.

So the program prints 20. Declaration and definition are distinct in C: a declaration introduces a name and its type, a definition also reserves storage, and a name may be declared many times but defined once.

Please log in or register to report this question.

Answer: D

Correct answer: All of the above

All three are true. An array holds one type. An array may be a member of a structure, as a fixed-length name field commonly is. And an array of structures is entirely ordinary - a table of records is exactly that.

Please log in or register to report this question.

Answer: D

Correct answer: All of the above

All three are true. An array holds one type. An array may be a member of a structure, as a fixed-length name field commonly is. And an array of structures is entirely ordinary - a table of records is exactly that.

Please log in or register to report this question.

36.

What is the output of this C code?

int main()
{
enum {ORANGE = 12, MANGO, BANANA = 11, APPLE};
printf("APPLE = %d\n", APPLE);
}

 

Answer: B

Correct answer: APPLE= 12

An enumerator without an explicit value takes the previous enumerator's value plus one, and an explicit value resets the count from that point.

So ORANGE = 12, then MANGO becomes 13, then BANANA = 11 resets the sequence, and APPLE follows at 12. Two enumerators therefore share the value 12, which C permits - enumerator values need not be distinct. The trap is expecting APPLE to continue from MANGO rather than from the explicitly reset BANANA.

Please log in or register to report this question.

Answer: C

Correct answer: Declares a constant a whose value will be 100

const int a = 100; declares an object named a of type int, qualified as constant and initialised to 100. Because it is const-qualified it may not be assigned to afterwards, which is why the initialiser is required here rather than optional.

Calling it simply a variable understates the qualification, which is the whole point of the declaration. The object is real - it has a type, storage and an address - which distinguishes it from a #define constant.

Please log in or register to report this question.

38.

Comment on the output of this C code?

#include <stdio.h>
    int const print()
    {
        printf("Sanfoundry.com");
        return 0;
    }
    void main()
    {
        print();
    }

 

Answer: B

Correct answer: Sanfoundry.com

The string is printed once and the program ends. Applying const to a function's return type qualifies the returned value; for a return type of int it has essentially no effect, since the value is a copy that the caller here discards.

It is certainly not an error - a return type may be qualified - and nothing in the code causes repetition, print() being called once from main. The trap is reading int const print() as something unusual; it is an ordinary function with a redundant qualifier.

Please log in or register to report this question.

Answer: D

Correct answer: They end with a semicolon

This is the false statement. A preprocessor directive is terminated by the end of the line, not by a semicolon - and adding one has consequences, since a semicolon written at the end of a #define becomes part of the replacement text and is substituted along with the value.

The other three are true: directives are read and processed by the preprocessor, they generate no code themselves but control what the compiler sees, and each must occupy its own line.

Please log in or register to report this question.

Answer: D

Correct answer: All of the above

All three are true. An array holds one type. An array may be a member of a structure, as a fixed-length name field commonly is. And an array of structures is entirely ordinary - a table of records is exactly that.

Please log in or register to report this question.

Loading…
Tags: C Constants Questions and Answers || C Constants MCQ Questions and Answers || C Constants GK Questions and Answers || C Constants GK MCQ Questions || C Constants Multiple Choice Questions and Answers || C Constants GK || GK on C Constants || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming