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

C Constants Questions and Answers

11.

enum types are processed by

Answer: A

Correct answer: Compiler

Enumerations are processed by the compiler. enum is part of the C language proper: the compiler creates a type, assigns integer values to the enumerators and makes them available as integer constants with scope and type.

This distinguishes them from #define constants, which the preprocessor substitutes textually before the compiler sees the source, with no type and no scope. That difference is the main argument for preferring enumerations to macros for related integer constants - they are visible to the debugger and obey the ordinary scoping rules.

Please log in or register to report this question.

12.

enum types are processed by?

Answer: A

Correct answer: Compiler

Enumerations are processed by the compiler. enum is part of the C language proper: the compiler creates a type, assigns integer values to the enumerators and makes them available as integer constants with scope and type.

This distinguishes them from #define constants, which the preprocessor substitutes textually before the compiler sees the source, with no type and no scope. That difference is the main argument for preferring enumerations to macros for related integer constants - they are visible to the debugger and obey the ordinary scoping rules.

Please log in or register to report this question.

13.

If the declaration unsigned c:5; is replaced by unsigned : 6; then

Answer: C

Correct answer: compiler will give a new name for the field

A bit-field declared without a name - unsigned : 6; - is an unnamed bit-field, which is legal C. It reserves the stated number of bits as padding but provides no name by which they can be accessed.

Its purpose is layout control: unnamed fields are used to align the following members at a particular bit position when a structure is mapped onto a hardware register or a defined external format. A width of zero has a further special meaning, forcing the next field to begin at the next storage unit.

Please log in or register to report this question.

Answer: C

Correct answer: both const keyword and #define preprocessor

Both mechanisms are used, and they work quite differently. #define is a preprocessor directive performing textual substitution before compilation: the name has no type, no scope and no address, and the compiler never sees it.

const qualifies an actual object, which has a type, obeys the ordinary scoping rules, occupies storage and is visible to the debugger. const is generally preferred for that reason, though #define is still needed where a value must be a constant expression usable in contexts such as an array bound in older C.

Please log in or register to report this question.

15.

If a variable can take only integral values from 0 to n. where n is a constant integer, then the variable can be represented as a bit-field whose width is the integral part of (the log in the answers arc to the base 2)

Answer: A

Correct answer: log (n) + 1

To represent every value from 0 to \( n \) requires enough bits to express \( n \) itself, which takes \( \lfloor \log_2 n \rfloor + 1 \) bits.

Checking with \( n = 7 \): \( \lfloor \log_2 7 \rfloor + 1 = 2 + 1 = 3 \) bits, which gives the eight values 0 to 7 - correct. With \( n = 8 \) it gives \( 3 + 1 = 4 \) bits, since three would only reach 7. The other expressions fail one or other of these checks.

Please log in or register to report this question.

16.

If a is an unsigned integer variable whose value is 0x6db7, what is the value of -a?

Answer: D

Correct answer: 0x9249

For an unsigned type, unary minus does not produce a negative number - unsigned arithmetic is defined modulo \(2^{n}\), so the result is whatever value added to a gives zero: \[ -a \equiv 2^{16} - a \pmod{2^{16}}. \] With \( a = \mathtt{0x6db7} = 28087 \) and a 16-bit unsigned int, \[ 65536 - 28087 = 37449 = \mathtt{0x9249}. \] The same result comes out of the bitwise route, which is quicker by hand: negation is complement and add one. Complementing 0x6db7 gives 0x9248, and adding one gives 0x9249. That intermediate value is exactly the trap. This record was keyed to 0x9248 - the one’s complement, with the final increment forgotten - and the key has been corrected. Note also that on a 32-bit int the answer would be 0xffff9249; the two-byte width is what makes the short form right here. The hexadecimal prefixes throughout this item had been corrupted from 0x to hx and are restored.

Please log in or register to report this question.

17.

What is the output of this C code?

int main()
{
printf("AllIndiaExams\r\nclass\n");
return 0;
}

 

Answer: B

Correct answer: AllIndiaExams class

The string contains \r\n between the two words. The carriage return returns the cursor to the start of the line and the newline moves it down one, so on a terminal the effect is simply to end the line - AllIndiaExams appears on one line and class on the next.

The carriage return alone would have returned to the start of the same line, so subsequent text would overwrite what was already there. Combined with the newline, its effect is invisible, which is why the two characters together are the conventional line ending on some systems.

Please log in or register to report this question.

18.

For the following code snippet:
char *str = "AllIndiaExams.in\0" "training classes";
The character pointer str holds reference to string:

Answer: C

Correct answer: AllIndiaExams.in\0training classes

Adjacent string literals are concatenated at translation time, so "AllIndiaExams.in\0" and "training classes" become a single literal. The embedded \0 is retained as a byte within it, and the compiler still appends its own terminating null at the very end.

The pointer therefore refers to the whole of that storage. Printing it with %s would show only AllIndiaExams.in, because printf stops at the first null - but what str holds a reference to is the complete concatenated sequence, which is what the question asks.

Please log in or register to report this question.

Answer: A

Correct answer: int < unsigned < float < double

The usual arithmetic conversions promote towards the type of greater rank, and the order is int, unsigned, float, double.

The int to unsigned step is the one that causes real trouble: comparing a negative int with an unsigned converts the negative value to a large positive one, so -1 < 1u evaluates false. The floating point steps are safer, though converting a large integer to float can lose precision silently.

Please log in or register to report this question.

20.

The value of an automatic variable that is declared but not initialized will be

Answer: B

Correct answer: unpredictable

An automatic variable that is declared without an initialiser holds whatever happened to be in its stack storage, so its value is unpredictable - indeterminate, in the standard's term.

Reading it before assignment is undefined behaviour rather than merely a matter of luck: the compiler may assume it does not happen and optimise on that basis, so the observed effect need not even be a consistent garbage value. Static and external variables are the contrast, being zero-initialised before the program begins.

Please log in or register to report this question.

Loading…

Other Subjects

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