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

C Constants Questions and Answers

1.

The declaration
enum cities{ bethlehem, jericho, nazareth = 1, jerusalem }
assigns the value 1 to

Answer: D

Correct answer: jericho and nazareth

Enumerators count upward from zero unless given an explicit value, and an explicit value resets the count. So bethlehem is 0, jericho is 1, nazareth is explicitly 1, and jerusalem follows at 2.

Both jericho and nazareth therefore have the value 1. C permits duplicate enumerator values, which is occasionally useful for aliases but is more often an accident - as here, where an explicit assignment collides with the automatic sequence.

Please log in or register to report this question.

2.

Comment on the output of this C code?

int const print()
{
printf("AllIndiaExams.in");
return 0;
}
void main()
{
print();
}

 

Answer: B

Correct answer: AllIndiaExams.in

The program prints the string once and stops. Applying const to a function's return type simply qualifies the value returned; for a return type of int it has no practical effect at all, since the returned value is a copy that the caller here discards.

Nothing in the code causes repetition - print() is called once from main and returns 0. The trap is reading int const print() as something exotic; it is an ordinary function with a redundant qualifier.

Please log in or register to report this question.

Answer: A

Correct answer: Constant variables need not be defined as they are declared and can be defined later

This is the false statement. A const object must be initialised in its declaration, because it cannot be assigned to afterwards - deferring the definition would leave no way to give it a value.

The other three are true: const is the keyword used, a const-qualified object cannot be reassigned, and a const object at file scope is zero-initialised in the absence of an initialiser, like any other object with static storage duration.

Please log in or register to report this question.

4.

The C declaration
int b [100];
reserves ____________ successive memory locations,each large enough to contain single integer.

Answer: C

Correct answer: 100

int b[100]; declares an array of 100 elements, so it reserves 100 successive locations each large enough for one int.

The bytes occupied would be \( 100 \times \mathtt{sizeof(int)} \) - 400 on a typical implementation - but the question asks for locations each holding a single integer, which is the element count. Note also that the valid indices run from 0 to 99; using b[100] is the classic off-by-one error, and C performs no bounds checking to catch it.

Please log in or register to report this question.

5.

What is the output of this C code?

#include <stdio.h>
    int main()
    {
        enum {ORANGE = 5, MANGO, BANANA = 4, PEACH};
        printf("PEACH = %d\n", PEACH);
    }

 

Answer: C

Correct answer: PEACH = 5

Enumerators continue from the previous value, and an explicit value restarts the sequence from that point.

So ORANGE = 5, MANGO becomes 6, BANANA = 4 resets the count, and PEACH follows at 5. ORANGE and PEACH therefore share the value 5, which C permits. The trap is expecting PEACH to continue from MANGO rather than from the explicitly reset BANANA.

Please log in or register to report this question.

Answer: C

Correct answer: repeated execution may result in different addresses for i

The declaration int i = 5; inside a function creates an object with automatic storage duration: it is allocated when the block is entered and released when it is left.

Nothing fixes where. A recursive call, a different call path, or simply a different run of the program can place it at a different address, and modern systems randomise the stack deliberately. Within a single lifetime the address is stable and the value may change; across lifetimes neither the address nor the value carries over.

Please log in or register to report this question.

Answer: B

Correct answer: outputs tim3

The inner printf is evaluated first as the argument to the outer one. It writes tim and returns the number of characters written, which is 3. The outer printf then prints that return value.

So the output is tim followed by 3. The point of the exercise is that printf returns a value at all - the character count - which is easy to forget because it is almost always discarded.

Please log in or register to report this question.

8.

What is the output of this C code?

int main()
{
const int a;
a = 32;
printf("a is %d", a);
return 0;
}

 

Answer: B

Correct answer: Compile time error

A const object must be given its value in its declaration, because there is no later opportunity: any assignment to it afterwards is an attempt to modify a const-qualified object, which the compiler rejects.

So both lines are problematic - const int a; declares a const object with no initialiser, and a = 32; then tries to assign to it. Either alone would be diagnosed. The correct form is const int a = 32;, initialising at the point of declaration.

Please log in or register to report this question.

Answer: A

Correct answer: leftmost bit is reserved for sign

A plain int is signed, and in the two's complement representation used by essentially every current implementation the most significant - leftmost - bit carries the sign: 0 for non-negative, 1 for negative.

That bit is not available for magnitude, which is why a 32-bit int spans roughly \( -2^{31} \) to \( 2^{31}-1 \) rather than 0 to \( 2^{32}-1 \). Declaring the variable unsigned releases the bit for magnitude and doubles the positive range.

Please log in or register to report this question.

10.

Consider the following statement
#define hypotenuse (a, b) sqrt (a*a + b*b);
The macro-call hypotenuse (a + 2, b + 3);

Answer: D

Correct answer: finds square root of 3*a + 4*b + 5

The macro's parameters are not parenthesised in the replacement text, so the arguments are substituted textually and the surrounding operators then bind more tightly than intended:

sqrt(a+2*a+2 + b+3*b+3)

which is \( \sqrt{a + 2a + 2 + b + 3b + 3} = \sqrt{3a + 4b + 5} \). The intended \( (a+2)^2 + (b+3)^2 \) is not what results. This is the standard argument for parenthesising every parameter and the whole replacement text in a function-like macro.

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