Java Support Interview Questions and Answers
Intermediate / 1 to 5 years experienced level questions & answers
Ques 1. What is the difference between an interface and an abstract class?
An abstract class can have both abstract and concrete methods, while an interface can only have abstract methods. In Java, a class can implement multiple interfaces but can extend only one abstract class.
Example:
Abstract class example:
abstract class Shape {
abstract void draw();
}
Interface example:
interface Shape {
void draw();
}
Ques 2. Explain the concept of multithreading in Java.
Multithreading in Java allows multiple threads to execute concurrently. It improves the performance and responsiveness of a program. The 'Thread' class and 'Runnable' interface are commonly used to create and manage threads in Java.
Example:
Example:
class MyThread extends Thread {
public void run() {
// thread execution logic
}
}
MyThread t1 = new MyThread();
t1.start();
Ques 3. Explain the 'try', 'catch', 'finally' blocks in Java Exception Handling.
'try' block contains the code that might throw an exception. 'catch' block handles the exception if it occurs. 'finally' block always executes, whether an exception is thrown or not. It is used for cleanup activities.
Example:
Example:
try {
// code that may throw an exception
}
catch (Exception e) {
// handle the exception
}
finally {
// cleanup code
}
Ques 4. What is the purpose of the 'static' keyword in Java?
'static' is used to create class-level variables and methods. It means the variable or method belongs to the class rather than a specific instance. 'static' members can be accessed using the class name.
Example:
Example:
class MyClass {
static int count;
static void incrementCount() {
count++;
}
}
Ques 5. What is the 'final' keyword in Java?
'final' is used to restrict the modification of classes, methods, and variables. A 'final' class cannot be inherited, a 'final' method cannot be overridden, and a 'final' variable cannot be reassigned after initialization.
Example:
Example:
public final class ImmutableClass {
// class definition
}
Ques 6. What is the difference between 'throw' and 'throws' in Java?
'throw' is used to explicitly throw an exception, while 'throws' is used in method signatures to declare the exceptions that the method might throw. Multiple exceptions can be declared using a comma-separated list in 'throws'.
Example:
Example:
void myMethod() throws CustomException {
// method implementation
}
Ques 7. Explain the concept of method overriding in Java.
Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass. The overridden method in the subclass should have the same signature (name, return type, and parameters).
Example:
Example:
class Animal {
void makeSound() {
System.out.println('Generic Animal Sound');
}
}
class Dog extends Animal {
void makeSound() {
System.out.println('Bark');
}
}
Ques 8. Explain the concept of the 'equals()' and 'hashCode()' contract.
According to the contract, if two objects are equal (according to the 'equals()' method), their hash codes must be equal as well. However, the reverse is not necessarily true: two objects with equal hash codes may not be equal.
Example:
It's important to ensure that the 'equals()' and 'hashCode()' methods are consistently implemented to maintain this contract.
Ques 9. What is the purpose of the 'default' method in Java interfaces?
The 'default' method in Java interfaces provides a default implementation for a method. It allows adding new methods to interfaces without breaking existing implementations.
Example:
Example:
interface MyInterface {
default void myMethod() {
// default implementation
}
}
Ques 10. Explain the 'Comparator' interface in Java.
The 'Comparator' interface is used to define custom ordering for objects. It provides two methods: 'compare()' to compare two objects and 'equals()' to check if two objects are equal. 'Comparator' is often used with sorting algorithms or data structures that require custom ordering.
Example:
Example:
class MyComparator implements Comparator{
public int compare(MyClass obj1, MyClass obj2) {
// custom comparison logic
}
}
Ques 11. Explain the 'super()' constructor in Java.
'super()' is used to invoke the constructor of the immediate parent class. It should be the first statement in the constructor of the subclass. If not explicitly called, the compiler inserts a 'super()' call by default.
Example:
Example:
class Subclass extends Superclass {
Subclass() {
super(); // invokes the constructor of the parent class
}
}
Ques 12. Explain the 'try-with-resources' statement in Java.
The 'try-with-resources' statement is used to automatically close resources (like files or sockets) at the end of the try block. Resources must implement the 'AutoCloseable' interface. It simplifies resource management and reduces the chances of resource leaks.
Example:
Example:
try (BufferedReader br = new BufferedReader(new FileReader('file.txt'))) {
// code that uses 'br'
} catch (IOException e) {
// handle the exception
}
Ques 13. Explain the 'java.util.Collections' class in Java.
'java.util.Collections' is a utility class that provides static methods for operating on collections (e.g., lists, sets, and maps). It includes methods for sorting, shuffling, searching, and synchronizing collections.
Example:
Example:
ListmyList = new ArrayList<>();
Collections.addAll(myList, 'Java', 'Python', 'C++');
Collections.sort(myList); // sorts the list
Ques 14. Explain the concept of 'immutable objects' in Java.
Immutable objects are objects whose state cannot be modified after they are created. Once an immutable object is created, its state remains constant throughout its lifetime. String, Integer, and BigDecimal are examples of immutable classes in Java.
Example:
Example:
final class ImmutableClass {
private final int value;
public ImmutableClass(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
Most helpful rated by users: