🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Abstract Classes and Interfaces: all 10 questions

Quiz: Abstract Classes and Interfaces · Question 6 of 10

What does this print? (code)

What does this print?

abstract class Shape {
    Shape() { System.out.print("S"); }
    abstract int sides();
}
class Triangle extends Shape {
    int sides() { return 3; }
}
public class Main {
    public static void main(String[] args) {
        Shape s = new Triangle();
        System.out.println(s.sides());
    }
}
  1. A.3
  2. B.S3
  3. C.Compile error: abstract classes cannot have constructors
  4. D.3S
Answer & explanation

Correct answer: S3

An abstract class can have a constructor. It runs when a subclass object is created, before the subclass's own code.

Can you get the rest right without peeking?

Take the timed Quiz: Abstract Classes and Interfaces quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →