Below are some common Java server-side interview questions, covering a range of topics from core Java to web development, databases, and more. I've provided brief answers to each. Please note, in a real interview, you'd be expected to elaborate on these answers with examples, explanations, or even write code snippets.
1. Java Basics
Question: What is the difference between "==" and .equals() in Java?
Answer:
== checks for reference equality (i.e., both objects point to the same memory location).
.equals() checks for content equality (i.e., the actual content of the objects is the same), as defined by the class's implementation of the equals method.
Question: Can you explain the concept of immutability in Java?
Answer:
Immutability refers to an object's state that cannot be modified once the object is created. Achieved by declaring classes as final, using final for fields, and not providing setter methods. This ensures thread safety and reduces bugs.
2. Java Collections & Data Structures
Question: What is the difference between ArrayList and LinkedList in Java?
Answer:
- ArrayList:
- Implemented as a resizable array.
- Faster for random access and operations that don’t involve inserting/removing elements at arbitrary positions.
- LinkedList:
- Implemented as a doubly-linked list.
- More efficient for inserting/deleting elements at arbitrary positions but slower for random access.
Question: How does a HashMap work in Java?
Answer:
- HashMap uses a hash table to store key-value pairs.
- Keys are hashed, and the resulting hash code is used to index the array where the key-value pair is stored.
- Handles collisions (different keys with the same hash code) using techniques like chaining (linked lists) or open addressing.
3. Java Multithreading & Concurrency
Question: What is the difference between wait() and sleep() in Java?
Answer:
- wait():
- Causes the currently executing thread to wait until another thread notifies it.
- Always used in a synchronized block/context.
- sleep():
- Suspends the execution of the current thread for a specified period.
- Can be used anywhere, not necessarily in a synchronized context.
Question: Explain the concept of deadlock in Java. How can it be avoided?
Answer:
- Deadlock: A situation where two or more threads are unable to proceed because each is waiting for the other to release a resource.
- Avoidance Strategies:
- Lock Ordering: Always acquire locks in the same order.
- Avoid Nested Locks: Minimize locking multiple resources simultaneously.
- Timeouts: Use
lock.tryLock(timeout) to avoid indefinite waits.
4. Java Web Development
Question: What is the difference between HTTP GET and POST methods?
Answer:
- GET:
- Used for retrieving data from a server.
- Data is sent in the URL as parameters.
- Idempotent (making the same request multiple times has the same effect as making it once).
- POST:
- Used for sending data to the server to create/update a resource.
- Data is sent in the request body.
- Not idempotent.
Question: Explain the concept of Dependency Injection in Spring Framework.
Answer:
- Dependency Injection (DI): A design pattern where one or more dependencies (objects) are injected into a dependent object.
- In Spring:
- Implemented using the IoC (Inversion of Control) container.
- Reduces coupling, increases flexibility, and makes testing easier.
- Annotations like
@Autowired are used to inject dependencies.
5. Database Interaction (JDBC, Hibernate, etc.)
Question: What is the difference between JDBC Statement and PreparedStatement?
Answer:
- Statement:
- Used for executing simple SQL queries.
- Vulnerable to SQL injection attacks if user input is directly concatenated into the query.
- PreparedStatement:
- Used for executing precompiled SQL statements with parameters.
- More secure as it prevents SQL injection by separating the SQL logic from the data.
Question: What are the benefits of using Hibernate over plain JDBC?
Answer:
- Object-Relational Mapping (ORM): Maps Java classes to database tables, reducing boilerplate code.
- Lazy Loading: Only loads related data when needed, improving performance.
- Caching: Improves performance by reducing database queries.
- Database Portability: Simplifies switching between different database systems.
Advice for Interviews:
- Prepare to Back Your Answers with Examples: Be ready to write code or provide detailed scenarios.
- Understand the Concepts Deeply: Don’t just memorize answers; comprehend the underlying principles.
- Practice with System Design Questions: Common in server-side development interviews, these questions assess your ability to design scalable systems.
- Review the Company’s Tech Stack: Tailor your preparation to the technologies the company uses.