Top java interview + coding question and Answers 2025

INTERVIEW QUESSTIONS

1. Describe the Java Memory Model (JMM).
Defines how threads interact through memory, ensuring visibility and ordering of variable
accesses.

2. What is garbage collection in Java? How does it work?
Garbage collection automatically deallocates memory for objects no longer in use, reclaiming
memory in the heap.

3. Explain the concept of Java annotations.
Annotations provide metadata about code, such as @Override, @Deprecated, and
custom annotations.

4. What are lambda expressions? Provide a use case.

Lambda expressions provide a concise way to implement functional interfaces.

Example: java CopyEdit List list = Arrays.asList(1, 2, 3);

list.forEach(n -> System.out.println(n));

5.Discuss the Stream API in Java.
The Stream API processes collections of objects in a functional style, supporting operations
like filter, map, and reduce.

6. What is the purpose of the Optional class?
Optional prevents NullPointerException by representing optional values.

7. Explain the try-with-resources statement.
Manages resources (like files) automatically, ensuring they are closed after use.
Example:
java
CopyEdit
try (BufferedReader br = new BufferedReader(new FileReader(“file.txt”))) {
// Read file
}

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

  • final: Prevents modification of variables, methods, or classes.
  • finally: Ensures execution of code after a try-catch.
  • finalize(): Called by the garbage collector before destroying an object.

9. How does the volatile keyword affect thread behavior?
Ensures visibility of changes to a variable across threads, preventing caching.

10. What are design patterns?
Design patterns are reusable solutions to common software design problems. Examples:
Singleton, Factory, Observer.

11. Explain the Singleton design pattern.
Restricts a class to one instance and provides a global access point to it.
class Singleton {
private static Singleton instance;
private Singleton() { }
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}

12. What is JDBC? How is it used?
JDBC (Java Database Connectivity) is an API for connecting to databases.
Steps:

  1. Load driver.
  2. Establish connection.
  3. Execute SQL queries.
  4. Close connection.

13. Discuss the differences between Statement and PreparedStatement.

  • Statement: Used for static queries.
  • PreparedStatement: Precompiled and supports dynamic queries.

14. What is the purpose of the transient keyword?
Excludes fields from serialization.

15. Explain serialization and deserialization.

  • Serialization: Converts an object to a byte stream.
  • Deserialization: Converts a byte stream back to an object.

16. What are inner classes?
Classes defined within another class. Types: static, non-static, local, and anonymous

17. Describe the use of the synchronized keyword.
Locks a block/method to allow only one thread access at a time.

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

  • String: Immutable.
  • StringBuilder: Mutable, non-thread-safe.
  • StringBuffer: Mutable, thread-safe.

19. Explain the concept of immutability in Java.
Immutable objects cannot be modified after creation, e.g., String.

20. How does Java handle memory leaks?
Java uses garbage collection but memory leaks can occur if references to unused objects are
maintained.

21. What are functional interfaces?
Interfaces with a single abstract method, e.g., Runnable

22. Discuss the role of the default keyword in interfaces.
Allows adding methods to interfaces without breaking existing implementations.

23. What is the enum type in Java?
Used to define a set of named constants.
Example:
enum Day { MONDAY, TUESDAY }

24. Explain the concept of reflection in Java.
Allows inspection and modification of classes, methods, and fields at runtime.

25. What are modules in Java?
Introduced in Java 9, modules allow better packaging, encapsulation, and dependency
management.

CODING QUESTIONS

  1. Two Sum: Given an array of integers, find two numbers that add up to a specific
    target.
  2. Reverse a String: Write a function to reverse a string without using built-in functions.
  3. Palindrome Check: Determine if a given string is a palindrome.
  4. Merge Two Sorted Lists: Merge two sorted linked lists and return it as a new sorted
    list.
  5. Longest Substring Without Repeating Characters: Find the length of the longest
    substring without repeating characters.
  6. Valid Parentheses: Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’,
    determine if the input string is valid.
  7. Search in Rotated Sorted Array: Search for a target value in a rotated sorted array.
  8. Container With Most Water: Given n non-negative integers, find two lines that
    together with the x-axis form a container, such that the container contains the most
    water.
  9. 3Sum: Find all unique triplets in the array which gives the sum of zero.
  10. Remove Nth Node From End of List: Remove the n-th node from the end of a
    linked list and return its head.
  11. Maximum Subarray: Find the contiguous subarray with the largest sum.
  12. Climbing Stairs: You are climbing a staircase. It takes n steps to reach the top. Each
    time you can either climb 1 or 2 steps. In how many distinct ways can you climb to
    the top?
  13. Set Matrix Zeroes: Given a m x n matrix, if an element is 0, set its entire row and
    column to 0.
  14. Group Anagrams: Given an array of strings, group anagrams together.
  15. Merge Intervals: Given a collection of intervals, merge all overlapping intervals.
  16. Linked List Cycle: Given a linked list, determine if it has a cycle in it.
  17. Implement Stack using Queues: Implement a last-in-first-out (LIFO) stack using
    only two queues.
  18. Minimum Window Substring: Given two strings s and t, find the minimum window
    in s which will contain all the characters in t.
  19. Word Search: Given a 2D board and a word, find if the word exists in the grid.
  20. Longest Increasing Subsequence: Find the length of the longest increasing
    subsequence in an array.
  21. Decode Ways: A message containing letters from A-Z is encoded to numbers using
    ‘A’ -> 1, ‘B’ -> 2, …, ‘Z’ -> 26. Given an encoded message, determine the total number
    of ways to decode it.
  22. Coin Change: Given coins of different denominations and a total amount of money,
    find the fewest number of coins needed to make up that amount.
  23. House Robber: Given a list of non-negative integers representing the amount of
    money of each house, determine the maximum amount of money you can rob tonight
    without alerting the police.

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Index