1Z0-808 · Question #92
Change line 5 to:
The correct answer is B. Change line 5 to: price price = 4. Option B is correct because it follows proper Java variable declaration syntax: type variableName = value;. The original line 5 is missing the type declaration before the variable name, meaning the variable price was never formally declared with its data type - without a type…
Question
Options
- AChange line 5 to: price = 4f;
- BChange line 5 to: price price = 4;
- CChange line 5 to: price = (float) 4;
- DChange line 5 to: price = (Simple) 4;
- EThe code compiles and runs properly; no changes are necessary.
How the community answered
(31 responses)- A3% (1)
- B94% (29)
- C3% (1)
Explanation
Option B is correct because it follows proper Java variable declaration syntax: type variableName = value;. The original line 5 is missing the type declaration before the variable name, meaning the variable price was never formally declared with its data type - without a type, the compiler cannot allocate the correct memory or perform type checking.
Why the distractors are wrong:
- A (
price = 4f;) uses the float literal suffixf, but this only works ifpricewas already declared with a type on a previous line; it doesn't fix a missing declaration. - C (
price = (float) 4;) explicitly casts the integer4to a float, but again assumespriceis already declared - it does not introduce the required type declaration. - D (
price = (Simple) 4;) attempts to cast an integer primitive to a reference type (Simple), which is illegal in Java and would itself cause a compile error. - E is wrong because missing a type in a variable declaration is a compile-time error in Java; the code would not run as-is.
Memory tip: Think of Java variable declarations as needing three parts - Type Name Value (e.g., int count = 0;). If any of those three parts is missing from the declaration line, the code won't compile. When you see assignment-only lines like price = 4;, ask yourself: "Was the type declared somewhere above?"
Topics
Community Discussion
No community discussion yet for this question.