nerdexam
Oracle

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

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 class Car { int value = 0; public void speed () { value = distance /timeTravel; System.out.println ("Velocity with new speed"+value+"kmph"); } } new Car().speed(); } and this code fragment: Vehicle v = new Vehicle (100); v.increSpeed(60); What is the result?

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)
  • A
    73% (37)
  • B
    4% (2)
  • C
    16% (8)
  • D
    8% (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 distance is 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). Since timeTravel is 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.

Full 1Z0-809 Practice