ProgrammingHardProduct-company placement · Java
What happens when this is compiled?
What happens when this is compiled?
// Java 17
sealed class Shape permits Circle, Square { }
final class Circle extends Shape { }
class Square extends Shape { }- A. Compile-time error: Square must be declared final, sealed or non-sealed
- B. It compiles; permitted subclasses need no modifier
- C. Compile-time error: a sealed class cannot have a final subclass
- D. It compiles, but Square cannot be instantiated
Correct answer: A. Compile-time error: Square must be declared final, sealed or non-sealed
Every permitted direct subclass of a sealed class must state how it continues the hierarchy: final (closed), sealed (with its own permits) or non-sealed (open again).
More OOP with C++ and Java questions
- What is printed? java class AppEx extends Exception { } class DbEx extends AppEx { } // static method of the main class: static void run()…
- What is printed? java // in main, which is declared throws Exception: try (AutoCloseable r = null) { System.out.print("body"); }…
- How many of these statements are correct in Java? (i) A subclass object contains storage for its superclass's private fields. (ii) A…
- What is printed? java int[][] a = {{1}, {2}}; int[][] b = a.clone(); b[0][0] = 9; b[1] = new int[]{5}; System.out.print(a[0][0] + " " +…
- What is printed? java StringBuilder[] sbs = new StringBuilder[3]; Arrays.fill(sbs, new StringBuilder()); sbs[0].append("x")…
- What is printed? java List<Integer> l = new ArrayList<>(10); System.out.print(l.size() + " "); l.set(0, 1); System.out.print(l);