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

C Constants Questions and Answers

Answer: B

Correct answer: a string constant

In C a literal is a constant written directly in the source, and "string literal" means a string constant - a sequence of characters in double quotes, stored as an array of char with a terminating null appended by the compiler.

The term is worth distinguishing from a string in general, which may be held in a modifiable array. A string literal is not modifiable: attempting to write through a pointer to one is undefined behaviour, and identical literals may share storage.

Please log in or register to report this question.

52.

What is the output of this C code?

#include <stdio.h>
    enum birds {SPARROW, PEACOCK, PARROT};
    enum animals {TIGER = 8, LION, RABBIT, ZEBRA};
    int main()
    {
        enum birds m = TIGER;
        int k;
        k = m;
        printf("%d\n", k);
        return 0;
    }

 

Answer: C

Correct answer: 8

Enumerators in C are simply integer constants, and an enumeration type is compatible with an integer type. Nothing prevents assigning TIGER, an enumerator of animals with value 8, to a variable of type enum birds.

The compiler may warn but is not required to reject it, and the value stored is 8, so that is what is printed. This looseness distinguishes C from C++ and from languages with genuine enumerated types - a C enumeration provides names and documentation, not type safety.

Please log in or register to report this question.

53.

What will be the output of the following C++ code?

#include <iostream>
    using namespace std;
    int main()
    {
        int  const  p = 5;
        cout << ++p;
        return 0;
    }

 

Answer: C

Correct answer: Error

p is declared const, so ++p attempts to modify a const-qualified object and the compiler rejects it.

The order of int and const is immaterial - int const p and const int p are the same declaration - so nothing is wrong with the declaration itself. It is the increment that fails, and it fails at compile time: const-ness is enforced entirely by the compiler and has no run-time representation.

Please log in or register to report this question.

54.

What is the output of the following 'C' program?
main
{
extern int i;
i = 20;
print("%d",size of (i));
}

Answer: D

Correct answer: Error

The fragment will not compile, and for several reasons at once. main is written without parentheses, so it is not a function definition; print is not a standard function, the output function being printf; and size of is written with a space, so the operator sizeof is not recognised.

Any one of these is fatal. The extern int i; declaration is itself legal, but with no definition anywhere the link would fail even if the syntax were repaired.

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 where it is declared, precisely because it cannot be assigned to afterwards - deferring the definition leaves no way to give it a value at all.

The other three are true: const is the keyword used to qualify a value as constant, a const-qualified object cannot be reassigned, and a const object at file scope with no initialiser would be zero-initialised like any other static-storage object, though it would still be useless.

Please log in or register to report this question.

56.

How to declare a wide character in the string literal?

Answer: A

Correct answer: L prefix

A wide string literal is written with an L prefix - L"text" - which gives an array of wchar_t rather than of char. The same prefix on a character constant, L'x', gives a wide character.

Later standards added further prefixes for specific encodings: u8 for UTF-8, u for char16_t and U for char32_t. The lower-case l is not a valid prefix, prefixes in C being case-sensitive.

Please log in or register to report this question.

57.

When a variable of data type double is converted into float, then

Answer: A

Correct answer: Rounding takes place

Narrowing a double to a float is a value conversion, not a reinterpretation of bits. If the value cannot be represented exactly in single precision, the result is one of the two nearest representable values, chosen according to the current rounding mode - normally to nearest.

So it is rounding rather than truncation, and describing it as dropping the low-order bits is wrong in both senses: the exponent field differs too, and mantissa bits are rounded rather than discarded. If the magnitude is too large for float, the result is infinity.

Please log in or register to report this question.

58.

What is the output of this C code?

#include <stdio.h>
    int main()
    {
        printf("sanfoundry\r\nclass\n");
        return 0;
    }

 

Answer: B

Correct answer: sanfoundry class

Here the carriage return is followed by a newline. The carriage return moves the cursor to the start of the line and the newline moves it down one, so nothing is overwritten and the two words appear on separate lines.

Compare this with the same code using \r alone, which prints classundry because the second word overwrites the first. The pair \r\n is the conventional line ending on some systems, and its combined effect is simply to end the line.

Please log in or register to report this question.

Answer: C

Correct answer: anywhere, but starting on a new line

The question concerns a #define constant, which is a preprocessor directive. A directive may appear anywhere in the file - before main, after it, or between functions - but it must begin on its own line, since the preprocessor works line by line and a directive is recognised by the # being the first non-whitespace token on the line.

Its effect runs from the point of definition to the end of the file, or until an #undef. By convention such definitions are placed near the top, where a reader will find them.

Please log in or register to report this question.

Answer: D

Correct answer: all of the mentioned

All the kinds named are literal constants: integer numerals, floating-point numerals, strings, character constants and, in C99 and later through stdbool.h, boolean values.

Each has its own syntax and its own type. Integer literals may carry u and l suffixes and a radix prefix, floating-point literals default to double unless suffixed, character literals have type int in C, and string literals are arrays of char with an appended null.

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