Core Java Interview Questions and Answers

Master Core Java with production-ready interview questions, practical examples, JVM concepts, OOP principles, memory management, and real-world scenarios for Java developers.

Core Java Interview Questions & Answers

Introduction

Core Java is the foundation of every Java application, whether you're developing Spring Boot microservices, enterprise banking systems, cloud-native applications, or Android apps.

Nearly every Java interview begins with Core Java questions because interviewers want to assess your understanding of the language before moving on to advanced topics like Collections, Multithreading, JVM, or Spring Framework.

In this article, we'll cover the most frequently asked Core Java interview questions with detailed explanations, practical examples, and production best practices.


1. Why is Java called Platform Independent?

Answer

Java is called Platform Independent because Java source code is compiled into Bytecode, which can run on any operating system that has a compatible Java Virtual Machine (JVM).

Compilation Process

Java Source (.java)
        │
        ▼
Java Compiler (javac)
        │
        ▼
Bytecode (.class)
        │
        ▼
JVM
        │
        ▼
Machine Code

Example

The same .class file can run on:

  • Windows
  • Linux
  • macOS

without recompilation.

Interview Tip

Java follows the principle:

Write Once, Run Anywhere (WORA).


2. What is the difference between JDK, JRE, and JVM?

Answer

JVM (Java Virtual Machine)

The JVM is responsible for executing Java bytecode.

Responsibilities:

  • Loads classes
  • Verifies bytecode
  • Executes code
  • Performs Garbage Collection
  • Manages memory

JRE (Java Runtime Environment)

JRE contains:

  • JVM
  • Java Libraries
  • Runtime Components

It is used only to run Java applications.


JDK (Java Development Kit)

JDK contains:

  • JRE
  • Java Compiler (javac)
  • Debugging tools
  • Development utilities

It is required for developing Java applications.

Comparison Table

Component Purpose
JVM Executes Bytecode
JRE Runs Java Applications
JDK Develops Java Applications

3. Explain the Java Compilation Process.

Answer

The Java compilation process consists of several stages.

.java File
      │
      ▼
javac Compiler
      │
      ▼
.class Bytecode
      │
      ▼
Class Loader
      │
      ▼
Bytecode Verifier
      │
      ▼
JIT Compiler
      │
      ▼
Native Machine Code

Explanation

  • The compiler converts Java source code into bytecode.
  • The JVM loads the bytecode.
  • The Bytecode Verifier ensures security.
  • The JIT Compiler converts frequently used bytecode into native machine code for better performance.

4. Explain the Four OOP Principles.

Answer

Encapsulation

Binding data and methods together while restricting direct access.

Example:

public class Employee {

    private double salary;

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}

Benefits:

  • Data hiding
  • Better security
  • Loose coupling

Inheritance

Allows one class to acquire properties of another.

class Animal {

    void eat() {
    }
}

class Dog extends Animal {
}

Benefits:

  • Code reuse
  • Extensibility

Polymorphism

One interface, many implementations.

Example:

List<String> list = new ArrayList<>();

Benefits:

  • Flexible code
  • Runtime behavior changes

Abstraction

Shows only necessary details while hiding implementation.

Example:

interface PaymentService {

    void pay();
}

Benefits:

  • Loose coupling
  • Better architecture

5. What is the difference between Stack Memory and Heap Memory?

Answer

Stack Memory

Stores:

  • Local variables
  • Method calls
  • Primitive values
  • Method references

Characteristics:

  • Faster
  • Thread-specific
  • Automatically released

Heap Memory

Stores:

  • Objects
  • Arrays
  • Instance variables

Characteristics:

  • Shared among threads
  • Managed by Garbage Collector
  • Larger than Stack

Comparison

Stack Heap
Stores local variables Stores objects
Thread specific Shared memory
Fast Comparatively slower
Auto cleanup Garbage Collection

6. Why is String Immutable?

Answer

Once a String object is created, it cannot be modified.

Example:

String s = "Java";

s.concat("17");

System.out.println(s);

Output

Java

A new object is created instead of modifying the existing one.

Advantages

  • Thread-safe
  • Secure
  • Supports String Pool
  • Efficient HashMap keys
  • Better caching

Production frameworks like Spring and Hibernate rely heavily on String immutability.


7. What is the difference between == and equals()?

Answer

==

Compares object references.

String s1 = new String("Java");
String s2 = new String("Java");

System.out.println(s1 == s2);

Output

false

equals()

Compares object content.

System.out.println(s1.equals(s2));

Output

true

Interview Tip

  • == → Memory reference
  • equals() → Object content

8. Explain final, finally, and finalize().

Answer

final

Used with:

  • Variable
  • Method
  • Class
final int age = 25;

Cannot be changed.


finally

Executes regardless of whether an exception occurs.

try {

}
finally {

}

Typically used for resource cleanup.


finalize()

Was called before object destruction.

It is deprecated and should not be used in modern Java.


9. What is Method Overloading vs Method Overriding?

Answer

Method Overloading

Same method name.

Different parameters.

add(int a, int b)

add(double a, double b)

Occurs at compile time.


Method Overriding

Subclass provides a new implementation.

class Animal {

    void sound() {}
}

class Dog extends Animal {

    @Override
    void sound() {}
}

Occurs at runtime.

Comparison

Overloading Overriding
Compile Time Runtime
Same Class Parent & Child
Different Parameters Same Signature

10. Interface vs Abstract Class

Answer

Interface

Used for defining behavior.

Supports multiple inheritance.

interface Payment {

    void pay();
}

Abstract Class

Can contain:

  • Abstract methods
  • Concrete methods
  • Constructors
  • Instance variables
abstract class Vehicle {

    abstract void start();
}

Interview Tip

Use Interface when different implementations share behavior.

Use Abstract Class when implementations share common state or logic.


11. What is the difference between Exception and Error?

Answer

Exception

Recoverable problems.

Examples:

  • IOException
  • SQLException
  • FileNotFoundException

Error

Serious JVM problems.

Examples:

  • OutOfMemoryError
  • StackOverflowError

Applications generally should not attempt to recover from Errors.


12. What is Serialization?

Answer

Serialization converts an object into a byte stream.

implements Serializable

Uses:

  • File storage
  • Distributed systems
  • Caching
  • Messaging
  • Session replication

Java automatically serializes many objects in enterprise applications.


13. What are Wrapper Classes?

Answer

Wrapper classes convert primitive values into objects.

Primitive Wrapper
int Integer
long Long
double Double
boolean Boolean

Example:

Integer number = 100;

Wrapper classes are heavily used in Collections because collections store objects, not primitives.


14. What are Java Best Practices?

Answer

Some important production practices include:

  • Follow SOLID principles.
  • Prefer composition over inheritance.
  • Use meaningful class and method names.
  • Handle exceptions properly.
  • Close resources using try-with-resources.
  • Avoid unnecessary object creation.
  • Write immutable classes when possible.
  • Use interfaces for loose coupling.
  • Keep methods short and focused.
  • Write unit tests for business logic.

These practices improve maintainability, readability, and scalability.


15. Explain a Production Scenario Where Core Java Knowledge Helped Solve an Issue.

Answer

Scenario

A Spring Boot application was experiencing increasing memory usage and slow API responses.

Investigation

Heap analysis revealed:

  • Large mutable objects were shared across multiple threads.
  • Incorrect use of == for String comparison caused cache misses.
  • Resources such as file streams were not closed properly.

Solution

  • Replaced mutable shared objects with immutable ones where appropriate.
  • Used equals() for String comparison.
  • Introduced try-with-resources for automatic resource cleanup.
  • Refactored code to use interfaces for better maintainability.

Result

  • Memory usage stabilized.
  • API response time improved significantly.
  • Resource leaks were eliminated.
  • The application became easier to maintain and test.

This example demonstrates how a solid understanding of Core Java concepts directly impacts the quality and performance of enterprise applications.


Summary

Core Java forms the foundation of every Java developer's journey. A strong grasp of these concepts is essential before moving to advanced topics such as Collections, Multithreading, JVM Internals, and Spring Framework.

Key Takeaways

  • Understand the Java compilation process.
  • Know the differences between JDK, JRE, and JVM.
  • Master Object-Oriented Programming principles.
  • Understand Stack vs Heap memory.
  • Learn why String is immutable.
  • Use equals() instead of == for object comparison.
  • Understand final, finally, and finalize().
  • Differentiate between interfaces and abstract classes.
  • Follow Java best practices for writing clean, maintainable code.
  • Relate Core Java concepts to real-world production scenarios.

Next Article

➡️ Collections Interview Questions & Answers