Java interview questions notes

Simon Law
4 min readJun 6, 2020

Java, SpringBoot, SQL

Concept

Q: What is functional programming?
A: Pure function (same return value for same arguments, no mutation of arguments)
First-class and higher-order function (take other functions as arguments or return them as results)

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 Array?
A: ArrayList has a dynamic size.

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.

Keywords

Q: What is abstract keyword?
A: It is a non-access modifier
abstract class (cannot be instantiated)
abstract method (declared without implementation)

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.

Method

Q: hashcode() vs equal()?
A: hashcode() return the integer hash code value of the object. If two objects are equal according to equals() method, their hash code must be the same.

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.

SpringBoot

Q: What are the annotation?
A: @Value: get value from config file
@Autowired: used on constructor/varaible/setter method, inject bean
@Qualifier: inject particular bean by id name
@Bean: declare a bean, will execute the method and register return value as a bean within a BeanFactory
@Configuration: contains @Bean
@Query: used in Spring Data JPA, it uses JPQL, e.g. “SELECT u FROM User u WHERE u.status = :status”
@Param: give a method parameter a name, and bind the name in the query, e.g. @Param(“status”) String stat
@RequestMapping: map web request to Spring Controller methods
@Entity: map to a database table
@Id: specify primary key
@GeneratedValue: specify generation strategy for primary key value

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.

SQL

Q: What is SQL injection?
A: It is the injection of malicious code in SQL statements via web page input.

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

Q: UNION vs UNION ALL?
A: UNION selects only distinct values. UNION ALL select duplicate values.

--

--