Top 10+ Interview Questions and Answers on Java
Prepare for your interview with these Top 10+ Interview Questions and Answers on Java above version 9, covering key features, enhancements, and best practices.
Q.1 What is the Java Platform Module System (JPMS) in Java 9? Why was it introduced?
Answer:
Java 9 introduced the Java Platform Module System (JPMS), also known as Project Jigsaw, to improve modularity in Java applications. Before Java 9, Java applications relied on JAR files, which led to dependency issues, security risks, and large monolithic applications.
Example of a Simple Module:
module com.example.myapp
{
exports com.example.myapp; // Makes this package accessible to other modules
}
2. How is a module defined in Java 9? What is module-info.java?
Answer:
A module in Java 9 is defined using a module-info.java file. This file specifies the module name, exported packages, and required dependencies.
Example of a Module Definition:
module com.example.mymodule
{
exports com.example.service; // Makes this package available to other modules
requires java.sql; // Declares dependency on java.sql module
}
- The exports keyword allows other modules to use the specified package.
- The requires keyword declares dependencies on other modules.
3. What are the improvements in the Stream API in Java 9?
Answer:
Java 9 introduced four new methods to enhance the Stream API:
- takeWhile(Predicate): Takes elements while the condition is true.
- dropWhile(Predicate): Drops elements while the condition is true.
- ofNullable(T): Creates a stream from a single element (or an empty stream if null).
- New iterate() method with a predicate.
Example: Using takeWhile() and dropWhile()
List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7);
List<Integer> result1 = numbers.stream()
.takeWhile(n -> n < 5)
.collect(Collectors.toList());
System.out.println(result1); // Output: [1, 2, 3, 4]
List<Integer> result2 = numbers.stream()
.dropWhile(n -> n < 5)
.collect(Collectors.toList());
System.out.println(result2); // Output: [5, 6, 7]
Feature | takeWhile() | filter() |
Stops Processing? | Yes, stops at the first failure | No, checks all elements |
Works on Sorted Lists? | Yes, efficient for sorted lists | Works on all lists |
Example | stream.takeWhile(n -> n < 5) | stream.filter(n -> n < 5) |
4. What are private methods in interfaces, and why were they introduced in Java 9?
Answer:
Java 9 allows private methods inside interfaces, making it easier to reuse common logic between default and static methods.
- Example: Using Private Methods in Interfaces
interface Calculator {
default int add(int a, int b) {
return operation(a, b);
}
default int multiply(int a, int b) {
return operation(a, b) * 2;
}
private int operation(int a, int b) {
return a + b;
}
}
Helps avoid code duplication
Improves encapsulation within interfaces
Q.5 What are the improvements in the Garbage Collector (GC) in Java 9?
Answer:
Java 9 made G1 (Garbage First) GC the default garbage collector, replacing the old Parallel GC.
Benefits of G1 GC:
✔ Reduces pause times by splitting heap into regions
✔ Improves responsiveness for large applications
✔ Better multi-threaded garbage collection
Q.6 What are Compact Strings in Java 9, and how does it improve performance?
Answer:
Java 9 introduced Compact Strings, which reduces memory consumption for String objects.
- Before Java 9, each String used a char[] array, consuming 2 bytes per character (UTF-16).
- In Java 9, Strings use byte arrays (byte[]), using 1 byte per character for ASCII text.
Advantages:
✔ Reduces heap memory usage
✔ Improves string processing speed
✔ Works automatically, no code changes needed
Q 7. What are the key features introduced in Java 10?
Answer:
Java 10 introduced several important features, including:
- Local Variable Type Inference (var) – Enables type inference for local variables.
- G1 Garbage Collector Improvements – Enhances performance by reducing full GC pauses.
- Application Class-Data Sharing (AppCDS) – Optimizes startup time and memory usage.
- Time-Based Release Model – Java moved to a six-month release cycle from Java 10 onwards.
- Thread-Local Handshakes – Allows executing callbacks on threads without performing global safepoint stops.
- Container Awareness – Java 10 detects memory and CPU limits when running in Docker containers.
Q. 8 What is Local Variable Type Inference (var) in Java 10? How does it work?
Answer:
Java 10 introduced local variable type inference using the var keyword, which allows the compiler to automatically infer the type of a variable based on its assigned value.
Example:
var message = “Hello, Java 10”; // Compiler infers String
var number = 10; // Compiler infers int
var list = List.of(“Apple”, “Banana”, “Cherry”); // Compiler infers List<String>
Rules & Limitations:
✔ var can only be used for local variables inside methods, loops, and blocks.
✔ var cannot be used for instance or class variables.
✔ var must be initialized at the time of declaration.
Incorrect Usage:
var x; // ❌ Compilation error: cannot use ‘var’ without initialization
var y = null; // ❌ Error: Cannot infer type from null
Q 9. Can we use var for method return types and parameters?
Answer:
No, var cannot be used for method parameters or return types because the compiler cannot infer types outside the method’s body.
Example (Incorrect Usage):
public var getName() { // ❌ Compilation error
return “Java 10”;
}
public void display(var name) { // ❌ Compilation error
System.out.println(name);
}
Reason: Java 10’s var is only for local variables, not for method signatures.
Q 13. What is Thread-Local Handshakes in Java 10?
Answer:
Thread-Local Handshakes allow JVM to pause individual threads instead of stopping all threads at a safepoint.
Benefits:
✔ Better performance in multi-threaded applications
✔ Reduces unnecessary thread pauses, improving application responsiveness
Note: Do watch our latest video: Click Here
Author:-
Pooja Nandode-Bhavsar
Call the Trainer and Book your free demo class Python for now!!!
© Copyright 2020 | SevenMentor Pvt Ltd.