Java Interview Questions

1. What is the difference between Parallel programming and Asynchronous programming

Asynchronous programming involves some calculations time-intensive tasks, which on the one hand are engaging a thread in the background but do not affect the normal flow of the program. Parallel programming incorporates several threads to perform a task faster.

2. What is the difference between == and equals() in Java

'==' compares the reference equality of two objects, while equals() compares the content equality of two objects. In other words, '==' checks whether two objects are the same instance, while equals() checks whether two objects have the same content.

3. What is the difference between HashMap and HashTable in Java

HashMap is not thread-safe and allows null values for both keys and values, while HashTable is thread-safe and does not allow null values for both keys and values. HashMap is generally faster than HashTable, but is not suitable for concurrent access without external synchronization.

4. What is a lambda expression in Java

A lambda expression is a concise way to represent a functional interface, which is an interface that contains only one abstract method. Lambda expressions are used to create anonymous functions that can be passed as arguments to methods or stored in variables.

5. What is the difference between an abstract class and an interface in Java

An abstract class can contain both abstract and non-abstract methods, while an interface can only contain abstract methods. An abstract class can also contain instance variables and constructors, while an interface can only contain constants and static methods.

6. What is the difference between 'final', 'finally', and 'finalize' in Java

final is a keyword that indicates that a variable or method cannot be modified or overridden. finally is a block that is executed after a try-catch block, regardless of whether an exception is thrown or not. finalize is a method that is called by the garbage collector before an object is destroyed, allowing it to perform any necessary cleanup operations.

7. What is the difference between a StringBuilder and a StringBuffer in Java

StringBuilder is not thread-safe and has better performance, while StringBuffer is thread-safe but has slightly worse performance. Both classes are used to create and modify strings.

8. What is the difference between a Stack and a Queue in Java

A Stack is a Last-In-First-Out (LIFO) data structure, while a Queue is a First-In-First-Out (FIFO) data structure. In other words, a Stack processes the most recent element added first, while a Queue processes the oldest element added first.

9. What is the difference between 'super()' and 'this()' in Java

super() is used to call the constructor of the superclass, while this() is used to call the constructor of the current class. Both are used to initialize the object's state, but super() is used to initialize the superclass's state, while this() is used to initialize the current class's state.

10. What is the difference between a HashSet and a TreeSet in Java

HashSet is an unordered collection that uses hashing to store elements, while TreeSet is an ordered collection that uses a red-black tree to store elements. HashSet has faster insertion and retrieval performance, while TreeSet has faster searching and sorting performance.

11. What is the difference between a Comparator and a Comparable in Java

Both Comparator and Comparable are used to compare objects, but Comparable is implemented by the object being compared, while Comparator is implemented separately. Comparable defines a natural ordering for a class, while Comparator allows for custom ordering.

12. What is the difference between a LinkedList and an ArrayList in Java

LinkedList is a doubly linked list data structure, while ArrayList is an array-based data structure. LinkedList has better performance for insertion and deletion operations, while ArrayList has better performance for random access operations.

13. What is the difference between static and final in Java

static is a keyword that indicates that a member variable or method belongs to the class itself, while final is a keyword that indicates that a variable cannot be changed after it has been initialized. static final is used to create constants in Java.

14. What is the difference between a try-catch block and a throws clause in Java

A try-catch block is used to catch and handle exceptions that may be thrown by a method, while a throws clause is used to declare that a method may throw a specific exception, but does not handle it itself. try-catch blocks provide a way to recover from exceptions, while throws clauses are used to propagate exceptions up the call stack.