Ultimate Guide to Java Interview Questions
If you're preparing for a Java interview, mastering these questions will give you a strong foundation. This post covers all critical Java topics, from Core Java concepts to Advanced topics, including coding challenges. Let's dive in!
Here’s a detailed explanation of the requested topics:
Core Java Basics
Key Features of Java:
- Platform Independent: Java code runs on any platform using the JVM.
- Object-Oriented: Encourages encapsulation, inheritance, and polymorphism.
- Simple and Easy to Learn: Syntax similar to C++ but without complexity like pointers.
- Secure: Provides runtime checking, bytecode verification, and a security manager.
- Robust: Exception handling and garbage collection help avoid errors.
- Multithreaded: Supports concurrent programming with built-in thread management.
- High Performance: Uses Just-In-Time (JIT) compiler.
- Distributed and Networked: Simplifies distributed system development using APIs.
- Dynamic and Extensible: Adapts to evolving requirements through dynamic linking.
Difference Between JDK, JRE, and JVM:
- JDK (Java Development Kit): Tools for developing and debugging Java programs, including JRE and compilers.
- JRE (Java Runtime Environment): Executes Java applications; includes JVM and libraries.
- JVM (Java Virtual Machine): Runs Java bytecode; ensures platform independence.
Difference Between ==
and .equals()
:
-
==
: Compares references (memory locations) for objects. -
.equals()
: Compares content or state of objects. Can be overridden.
Class and Object in Java:
- Class: Blueprint defining the properties (fields) and behaviors (methods).
- Object: Instance of a class, representing a real-world entity.
public static void main(String[] args)
Explained:
-
public
: Accessible from anywhere. -
static
: No need to instantiate the class. -
void
: No return value. -
main
: Entry point of the program. -
String[] args
: Command-line arguments.
Difference Between String
, StringBuilder
, and StringBuffer
:
-
String
: Immutable; thread-safe. -
StringBuilder
: Mutable; not thread-safe but faster. -
StringBuffer
: Mutable; thread-safe with synchronized methods.
Access Modifiers:
- Private: Visible within the class.
- Default: Visible within the package.
- Protected: Visible within the package and subclasses.
- Public: Visible everywhere.
Difference Between final
, finally
, and finalize
:
-
final
: Used to declare constants, prevent inheritance, or override. -
finally
: Block to ensure cleanup after atry
block. -
finalize()
: Method invoked by the garbage collector before destroying an object.
Object-Oriented Programming (OOP)
Four Pillars of OOP:
- Encapsulation: Restricts access to internal details. Example: Private fields with public getters/setters.
-
Abstraction: Hides complexity using interfaces or abstract classes.
Example:
List
interface. -
Inheritance: Reuse code by extending parent classes.
Example:
class Dog extends Animal
. - Polymorphism: Behavior changes based on the object. Example: Method overriding.
Difference Between Abstraction and Encapsulation:
- Abstraction: Focuses on "what" an object does.
- Encapsulation: Focuses on "how" the data is protected.
Method Overloading vs. Overriding:
- Overloading: Same method name, different parameters.
- Overriding: Subclass modifies a parent class method.
Interface vs. Abstract Class:
- Interface: 100% abstraction; methods are implicitly abstract.
- Abstract Class: Can have abstract and concrete methods.
Multiple Interfaces: A class can implement multiple interfaces using the implements
keyword.
Constructor Overloading Example:
class Example {
Example() { }
Example(int a) { }
Example(String s) { }
}
super
and this
:
-
this
: Refers to the current object. -
super
: Refers to the parent class object.
Collections and Data Structures
ArrayList vs. LinkedList:
- ArrayList: Faster for random access.
- LinkedList: Faster for insertions/deletions.
HashMap, LinkedHashMap, TreeMap:
- HashMap: Unordered, fastest.
- LinkedHashMap: Maintains insertion order.
- TreeMap: Sorted order.
Significance of hashCode()
and equals()
in HashMap:
- Ensures proper key-value mapping by preventing duplicates.
Iterator vs. ListIterator:
- Iterator: Traverses in one direction.
- ListIterator: Traverses in both directions.
Fail-Fast vs. Fail-Safe:
-
Fail-Fast: Concurrent modification triggers
ConcurrentModificationException
. - Fail-Safe: Works on a copy, no exception.
Internal Working of HashMap: Uses buckets for storage, hashing for key-value mapping, and linked lists to resolve collisions.
Multithreading and Concurrency
Creating Threads in Java:
-
Extending
Thread
:
class MyThread extends Thread { public void run() { } }
-
Implementing
Runnable
:
class MyRunnable implements Runnable { public void run() { } }
synchronized
Method vs. Block:
- Method: Locks entire method.
- Block: Locks a specific section.
wait()
, notify()
, notifyAll()
:
- Used for inter-thread communication.
Volatile Keyword: Ensures visibility of changes across threads.
Exception Handling
Checked vs. Unchecked Exceptions:
-
Checked: Caught during compile-time (
IOException
). -
Unchecked: Occurs during runtime (
ArithmeticException
).
Custom Exception Example:
class MyException extends Exception { MyException(String msg) { super(msg); } }
Java 8+ Features
Lambda Expression:
(numbers) -> numbers.stream().mapToInt(Integer::intValue).sum();
Need a deeper dive into specific areas? Let me know!
Common Coding Questions
- Write a program to check if a string is a palindrome.
- Write a program to find the second-largest element in an array.
- Write a program to reverse a linked list.
- Write a program to implement a thread-safe Singleton class.
- Write a program to find the frequency of characters in a string.
- Write a program to demonstrate deadlock and resolve it.
- Write a program to check if two strings are anagrams.
Top comments (0)