Bird
0
0

Given these classes:

hard📝 Application Q15 of 15
Java - Constructors
Given these classes:
class Vehicle {
    Vehicle() {
        System.out.print("V");
    }
}
class Car extends Vehicle {
    Car() {
        super();
        System.out.print("C");
    }
}
class SportsCar extends Car {
    SportsCar() {
        System.out.print("S");
    }
}
public class Demo {
    public static void main(String[] args) {
        new SportsCar();
    }
}
What is the output, and why does it appear in that order?
ACSV
BVSC
CSVC
DVCS
Step-by-Step Solution
Solution:
  1. Step 1: Analyze constructor calls

    Creating new SportsCar() calls SportsCar(), which implicitly calls Car(), which calls Vehicle() via super().
  2. Step 2: Trace output sequence

    Vehicle constructor prints "V", then Car prints "C", then SportsCar prints "S".
  3. Final Answer:

    VCS -> Option D
  4. Quick Check:

    Parent to child constructor output order is V-C-S [OK]
Quick Trick: Parent constructors run before child, print in that order [OK]
Common Mistakes:
  • Assuming SportsCar calls super() explicitly
  • Mixing output order
  • Forgetting implicit super() call in SportsCar

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes