Java_9_to_17_Interview_Problems
Java_9_to_17_Interview_Problems
What are the key features introduced in Java 9, and how do they impact
modular development?
Java 9 introduced the Java Platform Module System (JPMS).
Key features:
Modules: explicit dependencies, encapsulated packages.
module-info.java defines exposed and required modules.
Improves maintainability, encapsulation, and startup performance.
Impact:
Large applications can be broken into well-defined modules.
Avoids classpath hell — better control over visibility.
Challenges:
Migration of legacy monoliths may require deep refactoring.
Third-party library compatibility issues.
What are text blocks in Java 13/15 and how do they improve string handling?
Text blocks simplify multi-line string literals using triple quotes (""" ... """).
Introduced in Java 13 as preview; finalized in Java 15.
Benefits:
Avoids escaping quotes and newlines.
Improves readability of SQL, JSON, XML snippets.
Example:
String sql = """
SELECT *
FROM users
WHERE active = true
""";
What is the difference between sealed classes (Java 17) and abstract
classes/interfaces?
Sealed classes restrict which classes can extend or implement them.
Must explicitly permit subclasses using 'permits' clause.
Motivation:
Improves control over inheritance and helps maintain invariants.
Enhances exhaustiveness checking in switch expressions.
Difference from abstract classes:
Sealed classes control extension, not behavior enforcement.
Works with records, interfaces, and final classes.
Alternatives: non-sealed, final, or standard class hierarchies.
How does the new 'switch' enhancement improve code in Java 14+?
Java 14 introduced switch expressions (preview in Java 12, finalized in Java 14).
New features:
Switch as an expression that returns a value.
Use of arrow syntax and yield for multi-line blocks.
Example:
String result = switch(day) {
case MONDAY -> "Start";
case FRIDAY -> "End";
default -> "Mid";
};
Benefits:
Reduces boilerplate and fall-through errors.
Enables functional-style programming.