Java Interview Question Answers Part 2

Core Java Interview Questions & Answers – Part 2 (Advanced Basics with Clear Use Cases)


21. What is the final keyword and where is it used?

Answer: final is used to restrict modification.

Where applied:

  • final variable: Value cannot change
  • final method: Cannot be overridden
  • final class: Cannot be inherited

Why used:

  • Security
  • Prevent misuse
  • Maintain consistent behavior

Use case: Constants like tax rate, application version.

final double TAX_RATE = 0.15;

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

Answer:

  • == compares memory reference
  • equals() compares actual value

Why important: Avoid logical bugs when comparing objects.

Use case: User input comparison, database record checks.

String a = new String("Java");
String b = new String("Java");
a.equals(b); // true
a == b;      // false

23. Why is String immutable in Java?

Answer: Once created, a String cannot be changed.

Why Java designed it this way:

  • Security (passwords, URLs)
  • Thread safety
  • Performance optimization (String pool)

Use case: Login credentials, configuration keys.


24. Difference between String, StringBuilder, and StringBuffer

Answer:

  • String: Immutable
  • StringBuilder: Mutable, fast, not thread-safe
  • StringBuffer: Mutable, thread-safe, slower

Use case:

  • String → Fixed text
  • StringBuilder → Loop concatenation
  • StringBuffer → Multithreaded environment

25. What is the Java Memory Model?

Answer: Java memory is divided into areas to manage data efficiently.

Main parts:

  • Heap → Objects
  • Stack → Method calls, local variables
  • Method Area → Class metadata

Why important: Helps understand performance and memory leaks.


26. What is a Wrapper Class?

Answer: Wrapper classes convert primitive data types into objects.

Why needed: Collections work only with objects.

Use case: Storing numbers in ArrayList.

Integer num = 10;

27. What is Autoboxing and Unboxing?

Answer:

  • Autoboxing: Primitive → Object
  • Unboxing: Object → Primitive

Why useful: Simplifies code and improves readability.

Integer a = 5;  // Autoboxing
int b = a;      // Unboxing

28. What is this keyword and why is it needed?

Answer: this refers to the current object.

Why used:

  • Avoid variable confusion
  • Call current class methods or constructors

Use case: Constructor parameter naming.


29. What is super keyword?

Answer: super refers to the parent class object.

Why used:

  • Access parent fields/methods
  • Call parent constructor

Use case: Extending framework classes.


30. What is a Package in Java?

Answer: Package groups related classes.

Why used:

  • Avoid naming conflicts
  • Better project organization
  • Access control

Use case: java.util, java.lang


31. What is Access Modifier?

Answer: Access modifiers control visibility.

Modifier Scope
private Same class
default Same package
protected Package + subclass
public Everywhere

Why important: Security and clean design.


32. What is a Java Bean?

Answer: A Java Bean is a reusable class with:

  • Private variables
  • Public getters/setters
  • No-arg constructor

Use case: Data transfer between layers (DTOs).


33. What is Serialization?

Answer: Serialization converts object into byte stream.

Why used:

  • Save object to file
  • Send object over network

Use case: Session management, caching.


34. What is transient keyword?

Answer: transient prevents a variable from being serialized.

Use case: Passwords, sensitive data.

transient String password;

35. What is HashMap vs Hashtable?

Answer:

  • HashMap → Not synchronized, faster
  • Hashtable → Synchronized, slower

Use case:

  • HashMap → Single-threaded apps
  • Hashtable → Legacy systems

36. What is ConcurrentHashMap?

Answer: Thread-safe map with better performance than Hashtable.

Use case: High-concurrency server applications.


37. What is Synchronization?

Answer: Synchronization ensures one thread accesses shared resource at a time.

Why needed: Avoid data inconsistency.

Use case: Bank transactions.


38. What is volatile keyword?

Answer: Ensures visibility of variable changes across threads.

Why important: Prevents thread caching issues.


39. What is Deadlock?

Answer: Deadlock occurs when two threads wait for each other forever.

Why dangerous: Application freezes.

Use case: Improper lock ordering.


40. Why Java does not support Multiple Inheritance?

Answer: To avoid diamond problem ambiguity.

Solution provided by Java: Interfaces allow multiple inheritance safely.