Bird
0
0

Find the error in this Java code:

medium📝 Debug Q7 of 15
Java - Abstraction
Find the error in this Java code:
abstract class Vehicle {
  abstract void move();
}
class Bike extends Vehicle {
  void move() {
    System.out.println("Bike is moving");
  }
}
class Test {
  public static void main(String[] args) {
    Vehicle v;
    v.move();
  }
}
AAbstract method move() cannot be overridden
BNo error, code runs fine
CClass Bike should be abstract
DVariable 'v' is not initialized before use
Step-by-Step Solution
Solution:
  1. Step 1: Check variable initialization

    Variable 'v' is declared but not assigned an object before calling move().
  2. Step 2: Understand consequences

    Calling method on uninitialized reference causes compile-time error.
  3. Final Answer:

    Variable 'v' is not initialized before use -> Option D
  4. Quick Check:

    Uninitialized variable usage = compile error [OK]
Quick Trick: Always initialize object before method call [OK]
Common Mistakes:
  • Assuming abstract methods cannot be overridden
  • Thinking subclass must be abstract
  • Ignoring variable initialization

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes