Java Support Interview Questions and Answers
Experienced / Expert level questions & answers
Ques 1. What is the difference between '==', 'equals()', and 'hashCode()' methods in Java?
'==' is used to compare object references, 'equals()' is used to compare object content, and 'hashCode()' returns the hash code value of an object. It is recommended to override 'equals()' and 'hashCode()' when dealing with custom classes.
Example:
Example:
@Override
public boolean equals(Object obj) {
// custom implementation
}
@Override
public int hashCode() {
// custom implementation
}
Ques 2. Explain the concept of garbage collection in Java.
Garbage collection in Java automatically reclaims the memory occupied by objects that are no longer reachable. The 'java.lang.System.gc()' method and the 'finalize()' method are related to garbage collection.
Example:
No specific example, as garbage collection is a background process handled by the JVM.
Ques 3. What is the purpose of the 'transient' keyword in Java?
'transient' is used to indicate that a variable should not be serialized during object serialization. The value of a transient variable is not persisted when the object is stored.
Example:
Example:
class MyClass implements Serializable {
transient int sensitiveData;
}
Ques 4. What is the 'volatile' keyword in Java?
'volatile' is used to indicate that a variable's value may be changed by multiple threads simultaneously. It ensures that the variable is always read from and written to the main memory, avoiding thread-local caching.
Example:
Example:
volatile boolean flag = true;
// variable shared among multiple threads
Ques 5. What is the purpose of the 'assert' statement in Java?
The 'assert' statement is used for debugging purposes to check if a given boolean expression is true. If it's false, an 'AssertionError' is thrown. 'assert' statements can be enabled or disabled during runtime using the '-ea' or '-da' JVM options.
Example:
Example:
int x = -1;
assert x >= 0 : 'x should be non-negative';
Ques 6. What is the 'classpath' in Java, and how is it set?
The 'classpath' is a parameter in the Java Virtual Machine (JVM) that specifies the location of user-defined classes and packages. It can be set using the '-classpath' or '-cp' option when running Java applications. It includes directories, JAR files, and ZIP archives containing Java classes.
Example:
Example:
java -cp myapp.jar com.example.MyClass
Ques 7. What is the 'NaN' value in Java, and how is it represented?
'NaN' (Not a Number) is a special floating-point value used to represent undefined or unrepresentable results of mathematical operations. It is typically the result of operations like 0.0/0.0 or Math.sqrt(-1). 'NaN' is represented using the 'Double.NaN' constant.
Example:
Example:
double result = 0.0 / 0.0; // results in NaN
Ques 8. What is the purpose of the 'java.util.concurrent' package in Java?
The 'java.util.concurrent' package in Java provides a framework for concurrent programming. It includes classes and interfaces for thread management, concurrent collections, synchronization utilities, and high-level concurrency abstractions.
Example:
Example:
ExecutorService executor = Executors.newFixedThreadPool(5);
executor.submit(() -> System.out.println('Hello, concurrent world!'));
executor.shutdown();
Most helpful rated by users: