Question 1
What does this program print? ```java public class Main { static sealed class Shape permits Circle { String kind() { return "shape"; } } static final class Circle extends Shape { @Override String kind() { return "circle"; } } public static void main(String[] args) { Shape s = new Shape(); System.out.println(s.kind()); } } ```
A. Compilation fails — Circle must be declared non-sealed, not final, in order to extend a sealed class
Inverts the rule; a permitted subclass must be exactly one of final, sealed, or non-sealed, and final is a valid choice, so Circle being final is fine.
B. Compilation fails — a sealed class must declare at least two permitted subclasses
Invents a cardinality rule; one permitted subclass is allowed, the only requirement being at least one subtype somewhere.
C. Compilation fails — a sealed class is implicitly abstract and cannot be instantiated
Confuses sealed with abstract; sealed restricts who may extend but says nothing about instantiation, so a concrete sealed Shape can be instantiated.
D. shapeCorrect answer
sealed only restricts subclasses, not instantiation, so new Shape() is legal on this concrete class and s.kind() dispatches to Shape.kind(), printing shape.
Explanation
Trace: `sealed` restricts *who may extend* a class; it says nothing about whether the class itself may be instantiated. `Shape` is a concrete class with a body and an implicit no-arg constructor, so `new Shape()` is legal and `s.kind()` dispatches to `Shape.kind()`, printing `shape`. `Circle` is declared `final`, which satisfies the rule that every permitted subclass be `final`, `sealed`, or `non-sealed` — but nothing here ever creates a `Circle`. Why the others are wrong: `Compilation fails — a sealed class is implicitly abstract...` confuses two orthogonal modifiers. `abstract` controls instantiation; `sealed` controls the subclass set. A sealed class may be abstract, but it is not abstract by default — this one compiles and runs. `Compilation fails — Circle must be declared non-sealed, not final...` inverts the rule. A permitted subclass must be exactly one of `final`, `sealed`, or `non-sealed`. `final` (close the hierarchy here) is the most common choice; `non-sealed` (re-open it to anyone) is only one of the three. `Compilation fails — a sealed class must declare at least two permitted subclasses` invents a cardinality rule. One permitted subclass is fine; the only cardinality rule is that a sealed type must have *at least one* subtype somewhere, or javac reports `sealed class must have subclasses`. Exam tip: read `sealed` as "the permits list is the complete set of direct subtypes" — nothing more. Instantiability is still governed solely by `abstract`. The reverse trap: an `abstract sealed class` cannot be instantiated, and that is because of `abstract`, not `sealed`.