Top Java 11 frequently asked interview questions.
You can also have a look at Java 11 Quiz - MCQ - Multiple Choice Questions
Q: What are the methods added in String API in Java 11?
A :- isBlank - Returns true if the String is empty or contains only spaces
- lines - Returns a stream of lines extracted from the string, separated by line terminators such as \n, \r
- strip(), stripLeading(), stripTrailing() - strip whitespaces from the string
- repeat(int n) - Returns a new string which is the concatenation of this string repeated 'n' times
Q: What is the Z Garbage Collector introduced in Java 11?
A : The Z Garbage Collector, also known as ZGC, is a scalable low latency garbage collector designed to meet the following goals:- Pause times do not exceed 10ms
- Pause times do not increase with the heap or live-set size
- Handle heaps ranging from a few hundred megabytes to multiterabytes in size
Q: What is the Java Flight Recorder introduced in Java 11?
A : Java Flight Recorder (JFR) is a Java profiling tool that monitors and diagnoses a running Java application. It is responsible for collecting data about the running environment, JVM and Java application and dumps the recorded data into a .jfr file, and we can use Java Mission Control (JMC) to analyze and visualize the .jfr file.How to launch Single-File Source-Code Programs in JDK 11?
A : Before Java 11 -javac HelloWorld.java java HelloWorldJava 11 -
java HelloWorld.java
How to get Java 11 run-time environment working since there is no more JRE 11 for download?
A :- Download and use an OpenJDK Java 11 JDK from the OpenJDK site.
- Download and use an Oracle Java 11 JDK from the Oracle site and be sure that you fully understand the restrictions on "commercial use" that now apply to the Oracle Java 11+ releases.
- Roll own Windows JRE for Windows from the OpenJDK sources; see Create jre from OpenJDK Windows.
- Look into using the new jlink tool to create a custom image (basically a cut-down JRE) for your application. This seems to be the option that Oracle want 3rd-party application developers to use.
- Talk to Oracle sales about a Java support contract, and specifically ask about how to get a JRE build.
- Use a 3rd-party Java JRE distribution such as AdoptOpenJDK.
How to install OpenJDK 11 on Windows?
A : Set a PATH: Go to Advanced and then Environment Variables - Give path of your JDK binSet JAVA_HOME: Create a variable named JAVA_HOME and give JDK path till JDK folder(above bin)
What are the key features of Java 11?
Java 11, released in September 2018, introduced several new features and enhancements. Some of the key features of Java 11 include: - Local-Variable Syntax for Lambda Parameters: Java 11 allows the use of the var keyword in Lambda expressions, making the code more concise and readable. For example:(var x, var y) -gt; x + y- HTTP Client API: Java 11 comes with a new built-in HTTP client API in the java.net.http package. This API provides a more efficient and modern way to perform HTTP requests and handle responses.
HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://example.com")) .build(); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); String responseBody = response.body();- Dynamic Class-File Constants: Java 11 adds support for loading constant values dynamically from class files, enabling future-proofing of code that relies on external constants.
class MyClass { static final String MESSAGE = "Hello, World!"; }- Enhanced Security with Transport Layer Security (TLS) 1.3: Java 11 includes TLS 1.3, the latest version of the TLS protocol, which provides better security and improved performance for secure network communication. - Easier Migration to Java Modules: Java 11 introduces additional support for migrating from the classpath to using Java modules (introduced in Java 9). It allows developers to use unnamed modules to ease the migration process. - Launch Single-File Source-Code Programs: Java 11 enables developers to directly run a single-file Java source code program without first compiling it. This feature is particularly useful for small utility programs or one-off scripts.
$ java MyClass.java- HTTP/2 Support in the Servlet API: Java 11 enhances the Servlet API with native support for the HTTP/2 protocol, enabling faster and more efficient communication between servers and clients. - Flight Recorder: Java 11 includes the Java Flight Recorder (JFR) feature as a commercial feature, allowing developers to collect detailed runtime information about their applications with low overhead. These are some of the key features introduced in Java 11. However, it is important to note that the Java platform continuously evolves, and new features and enhancements may be introduced in future versions.
What is the purpose of the var keyword introduced in Java 11?
The var keyword, introduced in Java 11, is used to declare and initialize local variables with implicit type inference. The purpose of introducing the var keyword is to make the code more concise and readable by reducing unnecessary repetitions of the variable type. It helps to avoid code redundancy when the variable type is already clear from the right-hand side expression or initialization value. Using the var keyword, the Java compiler infers the appropriate type based on the context and the assigned value. The type inference is done at compile-time and does not introduce dynamic typing. Here's an example to illustrate the usage of the var keyword:var age = 25; // implicitly inferred as int var name = "John Doe"; // implicitly inferred as String var list = new ArrayList<>(); // implicitly inferred as ArrayList<Object>In the above code snippet, the appropriate types of variables age, name, and list are inferred automatically, making the code more concise. The inferred types are int, String, and ArrayList<Object> respectively. It's important to note that the var keyword should be used only for local variables with an initializer. It cannot be used for method parameters, return types, or class fields. The var keyword does not affect the runtime behavior or performance of the code. The compiled bytecode still includes the actual type information for the variables. However, it's recommended to use the var keyword judiciously, ensuring that the variable names and the assigned values provide enough context to understand the inferred type without explicitly mentioning it. By using the var keyword effectively, developers can write more concise and readable code while maintaining the type safety provided by the Java language.
How does the new HTTP Client API in Java 11 differ from the traditional HttpURLConnection?
Java 11 introduced a new built-in HTTP Client API in the java.net.http package, providing a more efficient and modern way to perform HTTP requests and handle responses compared to the traditional HttpURLConnection. The new HTTP Client API offers several advantages: Asynchronous and Reactive: The new API is natively asynchronous and supports reactive programming. It provides CompletableFuture-based methods for sending requests and receiving responses asynchronously. This allows developers to make non-blocking HTTP calls, improving the efficiency and responsiveness of applications. Fluent API: The HTTP Client API provides a fluent and easy-to-use API for constructing HTTP requests. It allows developers to chain multiple method calls together to build the request, making the code more concise and readable. Here's an example of constructing an HTTP GET request using the new HTTP Client API:HttpRequest request = HttpRequest.newBuilder() .GET() .uri(URI.create("https://example.com/api/users")) .header("Authorization", "Bearer token") .build();In comparison, the traditional HttpURLConnection API requires multiple method calls and explicit handling of input and output streams, making the code more verbose and error-prone. Response Handling: The new HTTP Client API provides a streamlined way to handle responses. It returns an HttpResponse object encapsulating the response status, headers, and body. The body can be processed using HttpResponse.BodyHandlers, which provide built-in support for handling different response types, like String, InputStream, or JSON. Here's an example of handling an HTTP response using the new API:
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); String responseBody = response.body();The traditional HttpURLConnection API, on the other hand, requires manual parsing and handling of the response stream, making the code more error-prone and harder to maintain. Transparent Compression and Decompression: The new HTTP Client API can automatically handle content compression and decompression using gzip or deflate algorithms based on the Accept-Encoding header. This helps in reducing network bandwidth and improving performance. Overall, the new HTTP Client API in Java 11 provides a more modern and efficient way to perform HTTP requests and handle responses compared to the traditional HttpURLConnection API. It simplifies the code, improves readability, and enhances the performance of HTTP communication within Java applications.