Java 21 Interview Questions
Enhance your Java 21 proficiency by practicing for the Java 21 Developer Certification practice tests.
Java Sealed Classes Interview Questions
1. What are Java sealed classes?
Java sealed classes are a new language feature introduced in Java 15. They are classes that have restricted inheritance, meaning that only specific classes can extend them.
2. Why are sealed classes useful?
Sealed classes are useful for restricting the hierarchy of a class to specific subclasses. They can help prevent unwanted extensions and simplify the design of a program.
3. How are sealed classes declared?
To declare a sealed class, use the "sealed" keyword before the class name, and then add a "permits" clause after any "extends" or "implements" clauses. The permits clause lists the specific classes that are allowed to extend the sealed class.
4. Can a sealed class be extended by any class?
No, a sealed class cannot be extended by any arbitrary class. Only the classes listed in the permits clause can extend the sealed class.
5. Can a sealed class be extended by an unknown subclass?
No, a sealed class cannot be extended by unknown subclasses. However, using the non-sealed modifier allows the sealed class to be extended by unknown subclasses.
6. What are the constraints for permitted subclasses of a sealed class?
Permitted subclasses of a sealed class must be accessible by the sealed class at compile time, directly extend the sealed class, and have one of the following modifiers: final, sealed, or non-sealed. They must also be in the same module or package as the sealed class.
7. Can interfaces be sealed?
Yes, interfaces can also be sealed. To seal an interface, use the "sealed" keyword before the interface name, and then add a "permits" clause after any "extends" clause. The permits clause lists the specific classes and interfaces that are allowed to implement or extend the sealed interface.
8. What is narrowing reference conversion in relation to sealed classes?
Narrowing reference conversion is a conversion used in type checking cast expressions, which allows an expression of one reference type to be treated as a different reference type, even if the first type is not a subtype of the second type. Sealed classes use this conversion to restrict the types that can be cast to a sealed class.
9. What is the isSealed() method in the java.lang.Class class?
The isSealed() method in the java.lang.Class class is a method that returns true if the given class or interface is sealed, and false otherwise.
10. What is the purpose of the permittedSubclasses() method in the java.lang.Class class?
The permittedSubclasses() method in the java.lang.Class class is a method that returns an array containing java.lang.constant.ClassDesc objects representing all the permitted subclasses of a sealed class, if it is sealed. If the class is not sealed, an empty array is returned.
Java Pattern Matching Interview Questions
11. What is pattern matching in Java?
Pattern matching is a feature introduced in Java 16 that allows developers to express more sophisticated logical conditions in a concise and typesafe manner. It involves comparing a value against a pattern and binding the value to a variable if the pattern matches.
12. How is pattern matching different from traditional switch statements in Java?
Pattern matching allows for more complex conditions and patterns to be used for comparison, whereas switch statements are limited to comparing against specific values. Pattern matching also supports binding of variables, whereas switch statements do not.
13. What are the benefits of using pattern matching in Java?
Pattern matching helps to reduce code duplication, improve code readability, and make it easier to handle complex conditional logic. It also allows for more efficient coding by reducing the need for nested if-else statements.
14. Can you explain how pattern matching works in Java?
Pattern matching first identifies the type of the value being compared and then checks if it matches any of the specified patterns. If a match is found, the value is bound to a variable and the corresponding code block is executed. If no match is found, an exception is thrown.
15. What are the different types of patterns supported in Java pattern matching?
Java pattern matching supports constant patterns, type patterns, deconstruction patterns, and variable patterns.
16. How do you use deconstruction patterns in Java pattern matching?
Deconstruction patterns allow for more complex matching by breaking down objects into their constituent parts. This is done using the deconstruct() method, which can be implemented on custom objects.
17. Can you use pattern matching with collections or arrays in Java?
Yes, pattern matching can be used with collections and arrays in Java by using the instanceof operator to check if the value being compared is of the correct type.
18. Are there any limitations to using pattern matching in Java?
It is important to note that pattern matching is not a replacement for switch statements, and there are certain scenarios where switch statements may still be a better choice. Additionally, pattern matching is only available in Java 16 and higher versions.
19. How can you handle cases where multiple patterns may match in Java pattern matching?
In such cases, the first pattern that matches will be used and any subsequent patterns will be ignored. This can be controlled using the keyword "or" to indicate that multiple patterns may match.
20. Can you provide an example of using Java pattern matching in a real-world scenario?
Sure, for example, pattern matching can be used to handle different types of exceptions in a more concise way. Instead of multiple if-else blocks, a single pattern matching statement can be used to handle different types of exceptions and perform the necessary actions.
Java Records Interview Questions
21. What are records in Java?
Records are a new kind of type declaration introduced in Java 14. Similar to enums, records are a restricted form of a class, suitable for storing "plain data" with minimal methods such as constructors and accessors.
22. What are the benefits of using records?
Records simplify the process of creating simple data carrier classes. They reduce boilerplate code, improve code readability, and make it easier to modify and maintain. Records also provide built-in implementations of equals(), hashCode(), and toString() methods, reducing the amount of code that needs to be written.
23. Can records extend a class or declare instance fields?
No, records cannot extend a class or declare instance fields. They are implicitly final and can only have private final fields that correspond to the record components.
24. Can records be abstract?
No, records cannot be abstract as they are implicitly final.
25. How are records created and instantiated?
Records are created using the record keyword and instantiated using the new keyword.
26. Is it possible to create nested records in Java?
Yes, nested records are possible in Java and they are implicitly static.
27. Can records have static methods?
Yes, records can have static methods, fields, initializers, constructors, instance methods, and nested types.
28. What methods are automatically provided by records?
Records automatically provide implementations of equals(), hashCode(), and toString() methods based on their record components.
29. Can records implement interfaces?
Yes, records can implement interfaces just like regular classes.
30. Can custom constructors be defined for records?
Yes, custom constructors can be defined for records, but they follow a compact constructor syntax and cannot have a formal parameter list.
31. What restrictions are there for using records?
A: Records cannot extend any class, declare instance fields (other than the private final fields corresponding to record components), or be abstract. They also cannot be non-static nested types.
32. What new methods related to records are available in the java.lang.Class class?
A: The java.lang.Class class has two new methods related to records - getRecordComponents(), which returns an array of RecordComponent objects representing the record's components, and isRecord(), which returns true if the class was declared as a record.
Java Virtual Threads Interview Questions
33. What are virtual threads in Java?
Virtual threads in Java are lightweight threads that are designed to reduce the effort of writing, maintaining, and debugging high-throughput concurrent applications. They are instances of the java.lang.Thread class and are not tied to a specific operating system (OS) thread.
34. How do virtual threads differ from platform threads?
Platform threads are thin wrappers around OS threads, while virtual threads are not tied to a specific OS thread. Platform threads have a large thread stack and other resources maintained by the OS, while virtual threads have a shallow call stack and typically perform a single, short-lived task.
35. What is the purpose of using virtual threads?
Virtual threads are designed to improve the throughput of high-concurrency applications by allowing a single OS thread to handle multiple virtual threads. This allows for more efficient use of system resources and can improve the overall performance of the application.
36. How are virtual threads created in Java?
Virtual threads can be created using the Thread and Thread.Builder APIs. The java.util.concurrent.Executors class also provides methods for creating an ExecutorService that starts a new virtual thread for each task.
37. Can virtual threads perform long-running CPU-intensive operations?
No, virtual threads are not suitable for long-running CPU-intensive operations. They are designed for tasks that spend most of their time waiting for I/O operations to complete.
38. What is the difference between virtual threads and thread pools?
Virtual threads are created and managed by the Java runtime, while thread pools are managed by the application code. Virtual threads are designed for short-lived, I/O-intensive tasks, while thread pools are used for longer-running, CPU-intensive tasks.
39. Can virtual threads access thread-local variables?
A: Yes, virtual threads can access thread-local variables and inheritable thread-local variables. However, it is recommended to use them carefully as a single JVM can support millions of virtual threads.
40. How can virtual threads be used to improve the performance of a server application?
Virtual threads are particularly useful for server applications that handle a large number of client requests. By using virtual threads, the application can handle more concurrent tasks with fewer OS threads, thus improving the overall throughput of the application.
Author | JEE Ganesh | |
Published | 2 months ago | |
Category: | Java Programming | |
HashTags | #Java #Programming #JavaCertification |