java

Here are some of the most commonly asked Java interview questions and answers, suitable for freshers and candidates with 1–3 years of experience.

1. What is Java?

Answer:
Java is a high-level, object-oriented, platform-independent programming language developed by Sun Microsystems (now Oracle). It follows the principle “Write Once, Run Anywhere (WORA)” because Java code runs on the Java Virtual Machine (JVM).


2. What are the features of Java?

Answer:

  • Object-Oriented

  • Platform Independent

  • Secure

  • Robust

  • Multithreaded

  • High Performance (using JIT Compiler)

  • Distributed

  • Portable

  • Dynamic


3. What is JVM?

Answer:
JVM (Java Virtual Machine) is responsible for executing Java bytecode. It provides platform independence by allowing the same bytecode to run on different operating systems.


4. What is JDK?

Answer:
JDK (Java Development Kit) contains:

  • JVM

  • JRE

  • Compiler (javac)

  • Debugger

  • Development tools

It is used to develop Java applications.


5. What is JRE?

Answer:
JRE (Java Runtime Environment) contains:

  • JVM

  • Core Java libraries

It is used only to run Java applications.


6. Difference between JDK, JRE, and JVM?

JDKJREJVM
Development KitRuntime EnvironmentVirtual Machine
Contains JREContains JVMExecutes bytecode
Used for developmentUsed for running applicationsPlatform dependent

7. What is Object-Oriented Programming (OOP)?

Answer:
OOP is a programming paradigm based on objects that contain data and methods.

The four pillars are:

  • Encapsulation

  • Inheritance

  • Polymorphism

  • Abstraction


8. What is Encapsulation?

Answer:
Encapsulation means wrapping data and methods into a single class and restricting direct access using private fields with public getters and setters.


9. What is Inheritance?

Answer:
Inheritance allows one class to acquire properties and methods of another class using the extends keyword.

Example:

class Animal {
    void eat() {
        System.out.println("Eating");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Barking");
    }
}

10. What is Polymorphism?

Answer:
Polymorphism means one interface with multiple implementations.

Types:

  • Compile-time (Method Overloading)

  • Runtime (Method Overriding)


11. What is Method Overloading?

Answer:
Multiple methods having the same name but different parameters.

void add(int a, int b) { }

void add(double a, double b) { }

12. What is Method Overriding?

Answer:
A subclass provides its own implementation of a method already defined in the parent class.


13. What is Abstraction?

Answer:
Abstraction hides implementation details and exposes only essential functionality.

Achieved using:

  • Abstract classes

  • Interfaces


14. Difference between Abstract Class and Interface?

Abstract ClassInterface
Can have constructorsCannot have constructors
Can have instance variablesOnly constants
Supports partial abstractionSupports full abstraction (traditionally)
Uses extendsUses implements

15. What is a Constructor?

Answer:
A constructor initializes an object and has the same name as the class with no return type.

Types:

  • Default Constructor

  • Parameterized Constructor


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

Answer:

==

  • Compares object references.

  • For primitives, compares values.

.equals()

  • Compares object contents (if implemented).


17. What is String?

Answer:
A String is an immutable object representing a sequence of characters.


18. Why is String immutable?

Answer:

  • Security

  • Thread safety

  • Efficient string pooling

  • Better performance


19. Difference between String, StringBuilder, and StringBuffer?

StringStringBuilderStringBuffer
ImmutableMutableMutable
Slow for modificationsFastThread-safe
Thread-safeNoYes

20. What is Exception Handling?

Answer:
Exception handling prevents abnormal program termination.

Keywords:

  • try

  • catch

  • finally

  • throw

  • throws


21. Difference between Checked and Unchecked Exceptions?

Checked

  • Checked at compile time.

  • Example: IOException

Unchecked

  • Occur at runtime.

  • Example: NullPointerException


22. What is Multithreading?

Answer:
Multithreading allows multiple threads to execute simultaneously for better performance.


23. What is Collection Framework?

Answer:
The Java Collections Framework provides classes and interfaces to store and manipulate groups of objects.

Examples:

  • List

  • Set

  • Queue

  • Map


24. Difference between ArrayList and LinkedList?

ArrayListLinkedList
Dynamic arrayDoubly linked list
Fast random accessFast insert/delete
Slower insertionSlower access

25. Difference between HashMap and HashSet?

HashMapHashSet
Stores key-value pairsStores unique values
Keys are uniqueElements are unique
Allows one null keyAllows one null element

26. What is the difference between final, finally, and finalize()?

  • final: Prevents modification (variables, methods, classes).

  • finally: Block that executes after try/catch.

  • finalize(): Legacy method called before garbage collection (deprecated and not recommended).


27. What is Garbage Collection?

Answer:
Garbage Collection automatically frees memory by removing objects that are no longer referenced.


28. What is the difference between this and super?

this

  • Refers to the current object.

super

  • Refers to the parent class object.


29. What are Access Modifiers?

ModifierSame ClassSame PackageSubclassOther Package
public
protected✘*
default
private

*Accessible in subclasses across packages.


30. What is the difference between break and continue?

break

  • Exits the loop completely.

continue

  • Skips the current iteration and continues with the next one.


Frequently Asked Coding Questions

  • Reverse a String

  • Check if a number is prime

  • Fibonacci series

  • Factorial of a number

  • Palindrome String

  • Remove duplicates from an array

  • Find the largest number in an array

  • Count vowels in a String

  • Sort an array

  • Swap two numbers without a third variable

These questions cover the core Java concepts that are commonly asked in technical interviews. If you’re preparing for a Java developer role, it’s also helpful to practice coding problems and be familiar with Java 8 features (Streams, Lambda expressions), JDBC, Collections, Multithreading, and basic SQL.