How many of the following declarations are correct ? int z = 7.0; double void…
2010
How many of the following declarations are correct ?
int z = 7.0;
double void = 0.000;
short array [2] = {0, 1, 2};
char c = "\n";Answer: B. One is correct — ConceptA declaration is well-formed only when three independent rules of the C language all hold at once. First, the name being declared must be an…
- A.
None
- B.
One is correct
- C.
Two are correct
- D.
All four are correct
Attempted by 172 students.
Show answer & explanation
Correct answer: B
Concept
A declaration is well-formed only when three independent rules of the C language all hold at once. First, the name being declared must be an identifier, and a reserved keyword may never serve as one. Second, an initialiser must be assignment-compatible with the declared type: any arithmetic type converts implicitly to any other arithmetic type, but a pointer never converts to an arithmetic type. Third, a brace-enclosed initialiser list may not supply a value for storage that lies outside the object being initialised.
Application
Test each line of the block against those three rules in turn.
int z = 7.0;— This is the second rule at work. Both int and double are arithmetic types, and initialising a scalar applies the same conversions as a simple assignment, so the double value converts implicitly and z is initialised to 7. The conversion discards any fractional part; here that part is zero, so 7.0 converts exactly and no value is lost. Permitted either way, the declaration is well-formed.double void = 0.000;— This breaks the first rule. The name being declared is void, one of the language's reserved keywords, so it cannot occupy the identifier position at all. The line is a syntax error regardless of what the initialiser 0.000 would otherwise have produced.short array [2] = {0, 1, 2};— This breaks the third rule. The object is an array of exactly two shorts, so it has room for the values 0 and 1; the brace list supplies a third value, 2, for storage that is not part of the object. That is a constraint violation and must be diagnosed.char c = "\n";— This breaks the second rule. Double quotes make "\n" a string literal: an array of two chars holding the newline character and the terminating null, which decays here to a char * pointer. The declared type char is an arithmetic type, and a pointer does not convert to an arithmetic type, so the initialiser is incompatible. Written with single quotes, '\n' is a character constant and the line would have been well-formed.
Cross-check
Counting the diagnostics from the other direction gives the same result: three lines each break a different rule — a keyword used as an identifier, an excess initialiser, and a pointer used to initialise an arithmetic object — while one line breaks none. The tally of well-formed declarations is therefore one, so “One is correct” is the answer.
A tally of zero would additionally require int z = 7.0; to be an error, but converting a double to an int inside an initialiser is exactly what the assignment conversions permit.
A tally of two would require a second line to survive, yet each of the other three breaks a distinct rule of its own.
A tally of four would require void to be usable as a variable name, which the language reserves outright.
Several previous-year compilations circulate a tally of two for this item. That reading treats the two-element array given three initialisers as acceptable because a compiler commonly reports it as a warning and still produces an executable; under the language standard it is a constraint violation, so the standard-conforming tally remains one.