---Advertisement---

Java Interview Questions and Answers (Part-III)

By Manisha

Published On:

---Advertisement---

71. What is the difference between notify() and notifyAll() in Java?

  • notify() wakes a single thread that is waiting on the object’s monitor.
  • notifyAll() wakes all threads that are waiting on the monitor.

🔹 Use notify() when only one thread needs to resume, and notifyAll() when multiple waiting threads should proceed.


Q72. What are the two types of exceptions in Java?

Java categorizes exceptions into:

  1. Checked Exceptions – Handled at compile time (e.g., IOException, SQLException).
  2. Unchecked Exceptions – Occur at runtime (e.g., NullPointerException, ArrayIndexOutOfBoundsException) and don’t require explicit handling.

Q73. What does OutOfMemoryError indicate in Java?

It’s a runtime error that occurs when the Java Virtual Machine (JVM) exhausts available memory.

  • Common causes include memory leaks, huge object allocation, or infinite object creation loops.

Q74. Difference between == and .equals() in Java?

  • == compares memory references, checking if two variables point to the same object.
  • .equals() compares the actual content or value inside the objects.

Q75. How can multiple strings be concatenated in Java?

Popular methods for joining strings include:

  1. + Operator – Easy but inefficient for loops.
  2. concat() – Used with String objects.
  3. StringBuilder – Best for performance.
  4. String.join() – Clean option introduced in Java 8.

Q76. Array vs Collection – What’s the distinction?

  • Array: Fixed-size data structure holding similar data types.
  • Collection: Part of Java Collection Framework (e.g., ArrayList, HashSet), supports dynamic resizing and various object types.

Q77. What is a BlockingQueue in Java?

A thread-safe queue that:

  • Blocks when retrieving from an empty queue.
  • Blocks when inserting into a full queue (in bounded queues).

🔹 Ideal for producer-consumer problems. Examples: LinkedBlockingQueue, ArrayBlockingQueue.


Q78. Process vs Thread – What’s the difference?

  • Process: Independent program with its own memory space.
  • Thread: A lightweight sub-task of a process, sharing memory with other threads within the same process.

Q79. Key benefits of multithreading in Java?

  • Increases CPU efficiency.
  • Reduces execution time.
  • Enhances application responsiveness.
  • Helps handle concurrent I/O operations smoothly.

Q80. What is context switching in Java multithreading?

The CPU switches between threads, saving the state of one and loading another.

  • It enables multitasking but incurs a performance cost due to overhead.

Q81. Differences between Array and ArrayList?

  • Array:
    • Fixed size.
    • Supports both primitives and objects.
  • ArrayList:
    • Dynamically resizable.
    • Stores only objects.
    • Offers built-in utility methods (like add(), remove()).

Q82. Why is volatile used in Java?

  • Guarantees visibility of changes to variables across threads.
  • Prevents caching and instruction reordering.
  • Used to maintain thread safety for simple variables.

Q83. What is Java NIO (New I/O)?

Java NIO provides a faster, non-blocking I/O mechanism:

  • Uses Channels and Buffers instead of streams.
  • Selectors allow one thread to manage multiple channels.

🔹 Excellent for high-performance I/O operations.


Q84. Compare String, StringBuilder, and StringBuffer.

TypeMutableThread-SafePerformance
StringNoYesSlow
StringBuilderYesNoFast
StringBufferYesYesModerate

Q85. Difference between Runnable and Callable?

  • Runnable:
    • No return value.
    • Cannot throw checked exceptions.
  • Callable:
    • Returns a result via Future<T>.
    • Can throw checked exceptions.

🔹 Callable is used in ExecutorService for asynchronous programming.


Q86. What is a checked exception in Java?

Checked exceptions are:

  • Checked at compile time.
  • Must be declared with throws or handled using try-catch.
  • Examples: IOException, ClassNotFoundException.

Q87. How do static and final keywords differ?

  • static:
    • Belongs to the class, not instances.
    • Common across all objects.
  • final:
    • Used to declare constants.
    • Prevents method overriding and class inheritance.

Q88. What are unchecked exceptions?

  • Not checked at compile time.
  • Occur due to logical errors or bad programming.
  • Examples: ArithmeticException, NullPointerException.

Q89. What are Java Lambda Expressions?

Lambdas offer a functional style of programming (Java 8+):

  • Allow defining anonymous functions in a concise way.
  • Commonly used with functional interfaces.

java

(int a, int b) -> a + b;


Q90. What is the use of try-catch block in Java?

  • Handles runtime exceptions gracefully.
  • Prevents abnormal program termination.

java

try {

   int result = 10 / 0;

} catch (ArithmeticException e) {

   System.out.println(“Cannot divide by zero.”);

}

👉The Next 10 Questions-IV: JAVA

---Advertisement---

Leave a Comment