top of page
Best Software Training Institute In Coimbatore -Idm Techpark Coimbatore

Java Interview Questions and Answers

Top 100 Java Interview Questions for Freshers

1. What is Java?

Java is a high-level, object-oriented programming language known for its portability, security, and robustness. It follows the "Write Once, Run Anywhere" (WORA) principle, making it platform-independent via the Java Virtual Machine (JVM).

2. What are Java's key features?

  • Platform-independent (via JVM)

  • Object-oriented (supports OOP principles)

  • Robust and secure

  • Multi-threaded (supports concurrent execution)

  • Garbage collection (automatic memory management)

  • Rich standard library (Java API)

3. What is Java coding convention?

Java has an official Java Code Conventions guide that defines best practices for writing clean, readable, and maintainable code, including naming conventions, indentation, and class structure.

4. What is the difference between an ArrayList and a LinkedList?

  • ArrayList: Uses a dynamic array, faster for random access (O(1)) but slower for insertions and deletions (O(n)).

  • LinkedList: Uses doubly linked nodes, better for insertions/deletions (O(1) at ends) but slower for random access (O(n)).

5. What is the difference between Java 8 and Java 11?

  • Java 8 introduced Lambda expressions, Streams API, and the Date/Time API.

  • Java 11 introduced local variable inference (var), new methods in String, Optional, and Files API, and removed JavaFX from the JDK.

6. How is memory managed in Java?

Java uses automatic memory management, including:

  • Heap Memory: Stores objects and class instances.

  • Stack Memory: Stores method execution details and local variables.

  • Garbage Collection (GC): Automatically deallocates unreferenced objects.

7. What are Java’s data types?

  • Primitive types: int, double, char, boolean, byte, short, long, float

  • Non-primitive types: String, Arrays, Classes, Interfaces

8. What are Java’s built-in data structures?

  • ArrayList (dynamic array)

  • LinkedList (doubly linked list)

  • HashMap (key-value pair)

  • HashSet (unique elements, unordered)

  • Stack (LIFO)

  • Queue (FIFO)

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

  • == compares object references (memory addresses).

  • .equals() compares object values (overridden for meaningful equality).

10. What are Java methods?

Methods are reusable blocks of code that perform specific tasks, defined using void or return types like int, String, etc.

public int add(int a, int b) { return a + b; }

11. What is the difference between clone() and copy()?

  • Shallow Copy (clone()): Creates a new object but references the same nested objects.

  • Deep Copy: Duplicates all objects recursively.

12. What is a lambda function in Java?

Lambda expressions are used to create anonymous functions:

interface MathOperation { int operation(int a, int b); } MathOperation add = (a, b) -> a + b; System.out.println(add.operation(3, 4)); // Output: 7

13. What are Java annotations?

Annotations provide metadata about code. Example:

@interface MyAnnotation {} class Example { @Override public String toString() { return "Example"; } }

14. What are varargs in Java?

varargs (...) allows passing a variable number of arguments:

public void printNumbers(int... numbers) { for (int num : numbers) { System.out.print(num + " "); } } printNumbers(1, 2, 3, 4, 5);

15. What is the difference between a class and a package?

  • Class: A blueprint for objects.

  • Package: A collection of related classes (java.util, java.io).

16. What is the difference between static, instance, and abstract methods?

  • Instance method: Requires an object (obj.method()).

  • Static method: Defined with static, belongs to the class (Class.method()).

  • Abstract method: Declared in an abstract class/interface, must be implemented in subclasses.

17. How does exception handling work in Java?

Using try-catch-finally:

try { int x = 1 / 0; } catch (ArithmeticException e) { System.out.println("Error: " + e.getMessage()); } finally { System.out.println("Execution completed."); }

18. What are Java’s file handling modes?

  • "r" – Read

  • "w" – Write (overwrite)

  • "a" – Append

  • "rb"/"wb" – Binary read/write

Example:

FileWriter writer = new FileWriter("file.txt", true); writer.write("Hello, Java!"); writer.close();

19. What is list comprehension equivalent in Java?

Using Streams and Lambda:

List<Integer> numbers = IntStream.range(0, 10) .filter(x -> x % 2 == 0) .map(x -> x * x) .boxed() .collect(Collectors.toList()); System.out.println(numbers); // [0, 4, 16, 36, 64]

20. How do you implement multithreading in Java?

Using Thread or Runnable:

class MyThread extends Thread { public void run() { for (int i = 0; i < 5; i++) { System.out.println(i); } } } MyThread thread = new MyThread(); thread.start();

OR using Runnable:

class MyRunnable implements Runnable { public void run() { for (int i = 0; i < 5; i++) { System.out.println(i); } } } Thread thread = new Thread(new MyRunnable()); thread.start();​

21. What is the difference between String, StringBuilder, and StringBuffer?

  • String: Immutable (creates a new object on modification).

  • StringBuilder: Mutable, faster but not thread-safe.

  • StringBuffer: Mutable and thread-safe (synchronized methods).

StringBuilder sb = new StringBuilder("Hello"); sb.append(" World"); System.out.println(sb); // Output: Hello World

22. What is method overloading and method overriding?

  • Method Overloading: Same method name, different parameters (compile-time polymorphism).

  • Method Overriding: Redefining a method in a subclass (runtime polymorphism).

Example of Overloading:

class MathUtil { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } }

Example of Overriding:

class Parent { void show() { System.out.println("Parent class"); } } class Child extends Parent { @Override void show() { System.out.println("Child class"); } }

23. What are Java access modifiers?

  • public – Accessible everywhere.

  • private – Accessible only within the class.

  • protected – Accessible within the package and subclasses.

  • Default (no modifier) – Accessible only within the package.

24. What is a constructor in Java?

A constructor is a special method that initializes objects.

class Example { int x; Example(int val) { x = val; } } Example obj = new Example(10);

Types:

  • Default Constructor (if none is defined).

  • Parameterized Constructor (takes arguments).

  • Copy Constructor (copies an object).

25. What is the difference between an interface and an abstract class?

FeatureInterfaceAbstract Class

MethodsAll methods are abstract (default in Java 7)Can have abstract & concrete methods

FieldsOnly constants (public static final)Can have variables and constants

Multiple InheritanceAllowed (a class can implement multiple interfaces)Not allowed (can only extend one class)

Example of Interface:

interface Animal { void sound(); } class Dog implements Animal { public void sound() { System.out.println("Bark!"); } }

Example of Abstract Class:

abstract class Vehicle { abstract void start(); } class Car extends Vehicle { void start() { System.out.println("Car starts with key."); } }

Best Java Training Institute In Coimbatore - Idmtechpark Coimbatore

 "Deep Concepts to Elevate Your Career"

This guide provides 100+ Java interview questions along with in-depth concepts to strengthen your expertise.
bottom of page