Top 50 Core Java & Spring Boot Interview Questions for Freshers (With Code Examples)
Master the 50 most frequently asked Java and Spring Boot technical interview questions for freshers, complete with code snippets, OOP concepts, and memory explanations.
Top 50 Core Java & Spring Boot Interview Questions for Freshers
Java remains the most heavily tested programming language across Indian campus placements and off-campus drives (TCS, Infosys, Cognizant, Wipro, Accenture, Deloitte). Here are the foundational questions, explanations, and code examples you must master.
---
Part 1: Core Java Fundamentals
1. What is the difference between JDK, JRE, and JVM?
- JVM (Java Virtual Machine): The execution engine that executes Java bytecode (
.classfiles). JVM provides platform independence by converting bytecode into native machine code. - JRE (Java Runtime Environment): JVM + Core Libraries required to run Java applications. It does NOT include compilers or debuggers.
- JDK (Java Development Kit): JRE + Development Tools (such as
javaccompiler,javadoc, andjdbdebugger). Developers require the JDK to write and compile code.
2. Why is String immutable in Java?
Strings in Java are immutable (cannot be changed once created) for three vital reasons:- String Constant Pool (SCP): Stored in the heap memory to save RAM. If strings were mutable, changing one reference would silently alter all other variables pointing to the same literal.
- Security: Strings are widely used for database URLs, usernames, passwords, and network sockets. Immutability prevents tampering.
- Thread Safety: Immutable objects are inherently thread-safe and can be shared freely across multiple threads without synchronization.
String s1 = "Hello";
s1.concat(" World");
System.out.println(s1); // Prints "Hello" (Original string unmodified)
String s2 = s1.concat(" World");
System.out.println(s2); // Prints "Hello World"
3. Difference between == and .equals()?
==is a reference comparison operator. It checks whether both references point to the exact same memory address..equals()is a method in theObjectclass. By default, it checks references, but classes likeString,Integer, andDateoverride it to perform value/content comparison.
String a = new String("Java");
String b = new String("Java");
System.out.println(a == b); // false (different heap objects)
System.out.println(a.equals(b)); // true (same textual content)
4. What is the difference between ArrayList and LinkedList?
| Feature | ArrayList | LinkedList |
|---|---|---|
| Internal Data Structure | Resizable Dynamic Array | Doubly Linked List |
| Get / Lookup by Index | O(1) Constant Time | O(n) Linear Time |
| Insert / Delete at beginning | O(n) (Shifting required) | O(1) Pointer update |
| Memory Consumption | Low (Stores only elements) | Higher (Stores data + 2 pointers per node) |
5. How does HashMap work internally in Java?
HashMap works on the principle of Hashing.- When
map.put(key, value)is called, Java calculateskey.hashCode(). - The hash code is converted to an index in the internal bucket array:
index = hash & (n-1). - If no collision occurs, the entry is stored in a Node.
- If a collision occurs (two keys yield the same index), entries are stored in a linked list.
- In Java 8+, if the number of colliding nodes in a bucket exceeds 8 (TREEIFY_THRESHOLD), the linked list is automatically converted into a balanced Red-Black Tree, reducing worst-case lookup from O(n) to O(log n).
Part 2: Object-Oriented Programming (OOP)
6. What is the difference between Method Overloading and Method Overriding?
- Overloading: Same method name, different parameters within the same class. Resolved at compile-time (Static polymorphism).
- Overriding: Same method name, identical parameters in a child class. Resolved at runtime (Dynamic method dispatch).
class Calculator {
// Overloading
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}
class Animal {
void speak() { System.out.println("Animal speaks"); }
}
class Dog extends Animal {
// Overriding
@Override
void speak() { System.out.println("Dog barks"); }
}
---
Part 3: Spring Boot Essentials
7. What are the key advantages of Spring Boot over standard Spring Framework?
- Auto-Configuration: Automatically configures beans based on dependencies present on the classpath.
- Embedded Servers: Bundles Tomcat, Jetty, or Undertow inside the JAR, eliminating manual WAR deployments.
- Starter Dependencies: Simplifies Maven/Gradle dependency management (e.g.,
spring-boot-starter-web). - Production-Ready Actuators: Out-of-the-box health checks, metrics, and application monitoring.
8. Explain the Spring Bean Lifecycle.
- Bean Definition loaded from annotations or XML.
- Bean Instantiation (Constructor invoked).
- Dependency Injection (Properties populated via
@Autowired). - Custom Init methods called (
@PostConstructorInitializingBean.afterPropertiesSet()). - Bean is ready for use.
- Container shutdown -> Destroy methods called (
@PreDestroyorDisposableBean.destroy()).
Prepare Your Resume for Top Tech Companies
Before applying, verify that your resume passes Automated Tracking Systems (ATS) used by TCS, Infosys, Accenture, and Wipro. Use our free suite of applicant tools: