1Z0-809 · Question #80
Given the definition of the Vehicle class: class Vehicle { int distance;//line n1 Vehicle (int x) { this.distance = x; } } public void increSpeed(int time) {//line n2 int timeTravel = time;//line n3…
The correct answer is A. Velocity with new speed 1 kmph. Option A is correct because value = distance / timeTravel performs integer division: 100 / 60 = 1 (the remainder is discarded), so the program compiles and runs successfully, printing the velocity as 1 kmph. Why the distractors are wrong: B (line n1): int distance is a…
Question
Options
- AVelocity with new speed 1 kmph
- BA compilation error occurs at line n1.
- CA compilation error occurs at line n2.
- DA compilation error occurs at line n3.
How the community answered
(51 responses)- A73% (37)
- B4% (2)
- C16% (8)
- D8% (4)
Explanation
Option A is correct because value = distance / timeTravel performs integer division: 100 / 60 = 1 (the remainder is discarded), so the program compiles and runs successfully, printing the velocity as 1 kmph.
Why the distractors are wrong:
- B (line n1):
int distanceis a perfectly valid instance variable declaration - no error. - C (line n2):
public void increSpeed(int time)is a valid method signature - no error. - D (line n3): This is the trickiest distractor. Pre-Java 8 required local variables accessed by inner classes to be explicitly declared
final. Since Java 8, variables only need to be effectively final (assigned once, never reassigned). SincetimeTravelis assigned once and never changed, no compilation error occurs.
Memory tip: Remember the phrase "effectively final since Java 8" - if a local variable is only assigned once, a local inner class can use it freely without the final keyword. Whenever you see a local class accessing an outer method's variable, ask yourself: "Is it effectively final?" - if yes, no error.
Community Discussion
No community discussion yet for this question.