Java interview questions notes

Java, SpringBoot, SQL


Q: What is the use of interface?
A: Support multiple inheritance like C++

Q: What are the OOP features?
A: inheritance (subclass reuse superclass method/attribute)
polymorphism (variable of a superclass type may refer to a subclass object)
encapsulation (hide data)
abstraction (interface, abstract class)

Q: Autoboxing vs Unboxing?
A: Autoboxing means the automatic conversion of primitive data types into their wrapper class, e.g. int to Integer. Unboxing is the opposite operation.

Q: Interface vs Abstract class?
A: Interface method is implicitly abstract and cannot have implementations. Abstract method can have default implementation. A class can implement multiple interfaces but it can extend only one abstract class.

Q: Overloading vs Overriding?
A: Overloading means same method name with different parameters. Overriding means a subclass method manipulates its superclass method implementation.

Data Structure

Q: ArrayList vs LinkedList?
A: ArrayList is better for storing and accessing data. LinkedList is better and faster for manipulating data. LinkedList can act as a queue as well as it implements Deque interface.

Q: ArrayList vs Vector?
A: Vector is synchronized.

Q: HashMap vs Hashtable?
A: HashMap is not synchronized. Hashtable does not allow null key or value.

Q: HashMap vs ConcurrentHashMap?
A: ConcurrentHashMap is thread-safe but the performance is relatively low.

Q: List vs Map?
A: List is an index-based array and may contain duplicate values. Map stores key-value pairs where keys are unique.

Q: String vs StringBuilder?
A: String is immutable whereas StringBuilder is mutable.

Q: StringBuilder vs StringBuffer?
A: StringBuffer is thread-safe and synchronized.

Q: Thread vs Runnable?
A: Implementing Runnable interface is recommended than extending Thread class, as we can save a space for our class to extend other class.


Q: What is final keyword?
A: It is a non-access modifier.
final variable (create constant)
final method (prevent method overriding)
final class (prevent inheritance)

Q: public, private vs protected?
A: They are access level modifiers.
public (same class, same package, subclass, diff. package)
protected (same class, same package, subclass)
default (same class, same package)
private (same class)

Q: static vs non-static?
A: A static method can be called without creating any instance of the class while a non-static method needed to be called by an object.

Q: What is volatile keyword?
A: It guarantees visibility of changes to variable across threads.


Q: Stream stream() vs parallelStream()?
A: parallelStream() can be used to quickly iterate over the large-sized collections, but the output should not be dependent on the element order.

Q: Thread join() vs wait()?
A: join() is used to wait until one thread finishes its execution. wait() is mainly used for shared resources, a thread notifies other waiting thread when a resource becomes free.

Q: Thread notify() vs notifyAll()?
A: notifyAll() sends notification to all waiting threads instead of single one thread.


Q: Inversion of Control vs Dependency Injection?
A: IoC means objects get other objects that they need from an outside source (e.g. xml config file), instead of creating themselves. DI means this is done without object intervention, by a framework component that passes constructor parameters and set properties.

Q: Is RESTful API stateful or stateless?
A: Stateless. The server does not store any client state. Client needs to pass its context to the server.


Q: JOIN vs Subquery performance?
A: JOIN clause is better. IN is a slow operator.

A: UNION selects only distinct values. UNION ALL select duplicate values.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store