Core Java – Interview Q&A
1. Difference between JVM, JRE, and JDK?
JVM (Java Virtual Machine) runs Java bytecode on any platform. JRE (Java Runtime Environment)
includes JVM and libraries needed to run Java programs. JDK (Java Development Kit) includes JRE
plus compiler and tools for development.
2. How is the Java platform independent?
Java source code is compiled into platform-independent bytecode. The JVM interprets this
bytecode on any OS, ensuring "write once, run anywhere".
3. Difference between == and equals()?
== compares references to check if two objects point to the same memory location. equals()
compares the actual content, and can be overridden in custom classes.
4. String vs StringBuilder vs StringBuffer?
String is immutable, and changes create new objects. StringBuilder is mutable and not
synchronized (faster), while StringBuffer is mutable and synchronized (thread-safe).
5. Use of the final keyword?
Declaring a variable as final makes it a constant. A final method cannot be overridden, and a final
class cannot be extended.
6. Use of the static keyword?
static members belong to the class, not an instance. Static methods and variables can be
accessed without creating an object.
7. Checked vs Unchecked Exceptions?
Checked exceptions are checked at compile-time (e.g., IOException). Unchecked exceptions
occur at runtime and usually indicate programming errors (e.g., NullPointerException).
8. What is Garbage Collection?
Garbage Collection automatically frees memory by removing unused objects. It helps prevent
memory leaks and optimizes performance.
Collections in Java – Interview Q&A
9. Difference between List, Set, and Map?
A list is ordered and allows duplicates, a Set is unordered and disallows duplicates, and a Map
stores key-value pairs without duplicate keys.
10.Difference between ArrayList and LinkedList?
ArrayList is backed by a dynamic array and provides fast random access. LinkedList uses a doubly
linked list and is faster for insertions and deletions.
11.Difference between HashSet and TreeSet?
HashSet is unordered and backed by a hash table. TreeSet maintains elements in sorted order
using a Red-Black tree.
12.Difference between HashMap and Hashtable?
HashMap is not synchronized and allows one null key. Hashtable is synchronized and does not
allow null keys.
13.What is ConcurrentHashMap?
A thread-safe Map that allows concurrent read and write operations without locking the entire
map. It uses segment-level locking.
14.Difference between fail-fast and fail-safe iterators?
Fail-fast iterators throw ConcurrentModificationException if the collection is modified during
iteration. Fail-safe iterators work on a copy of the collection.
15.Comparable vs Comparator?
Comparable defines natural ordering via compareTo() inside the class. Comparator defines
custom ordering via compare() in a separate class.
OOP Concepts – Interview Q&A
1. What is Encapsulation?
Encapsulation is the practice of hiding internal state and requiring all interaction to be
performed through methods. It is implemented via private fields and public getters/setters.
2. What is Inheritance?
Inheritance allows a class to acquire the properties and behavior of another class using extends
or implements. It promotes reusability and hierarchical classification.
3. What is Polymorphism?
Polymorphism means one interface can have multiple implementations. It exists in two forms:
compile-time (method overloading) and run-time (method overriding).
4. What is Abstraction?
Abstraction hides complex implementation details and shows only the necessary features. It is
achieved through abstract classes and interfaces.
5. Difference between abstract class and interface?
Abstract classes can have both abstract and concrete methods and support single inheritance.
Interfaces can have only abstract, default, or static methods and support multiple inheritance.
6. What is Method Overloading?
Overloading is having multiple methods with the same name but different parameter lists. It is
resolved at compile time and improves code readability.
7. What is Method Overriding?
Overriding allows a subclass to provide its own implementation of a method already defined in
its parent class. It supports run-time polymorphism.
JAVA8 Concepts:
1. Lambda Expressions
Lambda expressions allow you to write functional-style code by passing behavior as an argument. They
are essentially anonymous functions with a concise syntax.
Example:
(x, y) -> x + y
Benefits:
● Reduces boilerplate code for anonymous classes.
● Makes code more readable for functional operations (sorting, filtering, mapping).
2. Functional Interfaces
A functional interface is an interface with exactly one abstract method.
● Annotated with @FunctionalInterface (optional but recommended).
● Can have default and static methods.
Examples:
● Predicate<T> – takes T, returns boolean.
● Function<T, R> – takes T, returns R.
● Consumer<T> – takes T, returns nothing.
3. Streams API
Streams allow processing of collections in a declarative, functional way.
● Intermediate operations: return a Stream (lazy). Examples: map, filter, sorted.
● Terminal operations: trigger execution. Examples: collect, count, forEach.
4. map() vs flatMap()
● map() – transforms each element to another value (1→1 mapping).
● flatMap() – transforms each element to a stream/collection and then flattens (1→many
mapping).
Used when dealing with nested collections or multiple results per element.
5. Optional
● A container that may or may not hold a value.
● Avoids NullPointerException by forcing explicit handling of missing values.
● Methods: isPresent(), ifPresent(), orElse(), orElseGet(), orElseThrow(), map().
6. Intermediate vs Terminal Operations
● Intermediate: Lazy, return Stream, can be chained. No data processed until terminal operation is
invoked.
● Terminal: Triggers stream processing and returns a non-Stream result.
7. [Link]()
● Groups elements by a classifier function into a Map<K, List<T>>.
● Can be combined with downstream collectors for counting, mapping, averaging, etc.
Example:
[Link](Person::getCity, [Link]())
8. New Date/Time API ([Link])
● Introduced in Java 8 to replace the old [Link] and Calendar.
● Immutable and thread-safe.
● Main classes:
● LocalDate – date without time.
● LocalTime – time without date.
● LocalDateTime – date and time without timezone.
● ZonedDateTime – date, time, and timezone.
● Supports better formatting, parsing, and time zone handling.
Java 8 – Interview Q&A
1. What are Lambda Expressions?
Lambda expressions provide a concise way to write anonymous functions. They make code more
readable and are commonly used in functional interfaces and Stream API.
2. What are Functional Interfaces?
A functional interface has exactly one abstract method, making it eligible for use with lambda
expressions. Examples include Predicate, Function, and Consumer.
3. Difference between map() and flatMap()?
map() transforms each element to another object, maintaining the structure (1→1). flatMap()
transforms each element to multiple elements and flattens the result (1→many).
4. What is Optional and why use it?
Optional is a container that may or may not hold a value, used to avoid null checks. It provides
methods like orElse() and ifPresent() to handle absence safely.
5. Difference between intermediate and terminal operations in Streams?
Intermediate operations (like filter, map) are lazy and return a Stream. Terminal operations (like
collect, count) trigger execution and return a non-Stream result.
6. How does [Link]() work?
It groups elements of a stream by a classifier function into a Map. It can be combined with
downstream collectors for counting, mapping, or averaging.
7. Explain the new Date/Time API.
Java 8 introduced the immutable, thread-safe Java. timepackage. It includes classes like
LocalDate, LocalTime, and ZonedDateTime for better date-time handling.
Spring Boot – Interview Q&A
1. What is Spring Boot, and why is it used?
Spring Boot is a framework that simplifies Spring application development by providing
auto-configuration, embedded servers, and minimal XML configuration. It accelerates
development with ready-to-use defaults.
2. How does Spring Boot differ from Spring Framework?
Spring Boot is built on top of Spring and eliminates boilerplate configuration using
[Link], starter dependencies, and embedded Tomcat/Jetty servers.
3. What are Spring Boot Starters?
Starters are pre-configured Maven/Gradle dependency descriptors for specific functionalities,
e.g., spring-boot-starter-web includes all libraries for building REST APIs.
4. What is auto-configuration in Spring Boot?
Auto-configuration automatically sets up beans and configurations based on the dependencies
present on the classpath, using @EnableAutoConfiguration internally.
5. How to disable a specific auto-configuration?
Use @EnableAutoConfiguration(exclude = [Link]) or [Link] the
property in [Link].
6. What is the role of [Link] or [Link]?
These files store application-level configurations such as server port, database credentials, and
logging settings in a centralized manner.
7. How does Spring Boot embed servers like Tomcat?
Spring Boot packages the server inside the application JAR/WAR, enabling java -jar execution
without external server deployment.
8. Difference between @Component, @Service, and @Repository?
All are stereotypes for Spring-managed beans. @Service marks service-layer classes,
@Repository marks DAO classes with exception translation, and @Component is a generic bean
marker.
9. What is @SpringBootApplication?
A convenience annotation combining @Configuration, @EnableAutoConfiguration, and
@ComponentScan to bootstrap a Spring Boot app.
10.How do you create a REST API in Spring Boot?
Use @RestController for the controller, @GetMapping/@PostMapping for endpoints, and Spring
Boot auto-configures JSON serialization with Jackson.
11.How does Spring Boot handle database connections?
Through [Link].* properties and DataSource auto-configuration. Can use Spring Data
JPA with repositories for CRUD operations.
12.How to change the default server port in Spring Boot?
Set [Link]=8081 in [Link] or pass --[Link]=8081 as a command-line
argument.
13.What is an Actuator in Spring Boot?
Actuator provides production-ready features like health checks, metrics, and environment details
via HTTP endpoints.
14.How does Spring Boot support profiles?
Profiles allow grouping of configurations for different environments (dev, test, prod). Activated
via [Link] property.
15.How to secure a Spring Boot application?
Use spring-boot-starter-security for authentication and authorization. Configure using
WebSecurityConfigurerAdapter or SecurityFilterChain in Spring Security 5+.