0% found this document useful (0 votes)
3 views12 pages

Advanced Java

The document contains 25 advanced Java questions and answers covering topics such as JDBC, Servlets, JSP, connection pooling, session management, and caching strategies. Each question provides a brief explanation, examples, and where the concepts are typically used in projects. The document serves as a study guide for understanding key Java concepts and their applications in web development.

Uploaded by

Shubhada
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Advanced Java

The document contains 25 advanced Java questions and answers covering topics such as JDBC, Servlets, JSP, connection pooling, session management, and caching strategies. Each question provides a brief explanation, examples, and where the concepts are typically used in projects. The document serves as a study guide for understanding key Java concepts and their applications in web development.

Uploaded by

Shubhada
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ADVANCED JAVA – 25 Questions (Casual, Detailed)

1) What is JDBC and why do we use it?

Answer:
JDBC stands for Java Database Connectivity. It’s how Java applications talk to a database. You
can run queries, get results, and manage database connections all from Java code.

Example:

Connection conn = [Link](url, user, pass);

Statement stmt = [Link]();

ResultSet rs = [Link]("SELECT * FROM employee");

while([Link]()) {

[Link]([Link]("name"));

Where used in project:

 Fetching records from relational databases

 CRUD operations in services

Interview Line:
“JDBC is how Java apps interact with databases.”

2) What are the main types of JDBC drivers?

Answer:
There are four types of JDBC drivers: Type-1 (JDBC-ODBC bridge), Type-2 (native API driver),
Type-3 (network protocol driver), and Type-4 (pure Java driver). Type-4 is the most common
because it is pure Java and works across platforms.

Where used in project:

 Connecting to Oracle, MySQL, or Postgres from Java apps

Interview Line:
“Type-4 drivers are widely used because they are portable and efficient.”
3) What is a Connection Pool and why is it important?

Answer:
Creating a database connection every time is slow. A connection pool keeps a set of connections
ready to use, making database operations faster and reducing load on the database.

Example:

DataSource ds = new BasicDataSource();

[Link](url);

[Link](user);

[Link](pass);

Connection conn = [Link]();

Where used in project:

 High-performance APIs

 Web applications with many concurrent users

Interview Line:
“Connection pools save time and resources when hitting the database.”

4) What is a Servlet?

Answer:
A Servlet is a Java class that runs on a server and handles HTTP requests and responses. It works
like a backend worker for web requests.

Example:

@WebServlet("/hello")

public class HelloServlet extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException


{

[Link]().write("Hello World");

}
}

Where used in project:

 Handling web requests

 Backend controllers in Java web applications

Interview Line:
“Servlets respond to HTTP requests in Java web applications.”

5) What is JSP and why do we use it?

Answer:
JSP (Java Server Pages) allows you to write HTML pages that can also run Java code. It is mainly
used to display dynamic content from the backend.

Example:

<html>

<body>

<%= "Hello " + [Link]("name") %>

</body>

</html>

Where used in project:

 Dynamic web pages

 Rendering database data in UI

Interview Line:
“JSP helps generate dynamic HTML using Java.”

6) What is the difference between Servlet and JSP?

Answer:
Servlet is Java code that generates output, usually HTML. JSP is HTML that can run Java code
inside it. Servlets are more programmatic, and JSP is more markup-friendly.

Where used in project:


 Servlets → processing requests

 JSP → generating dynamic pages

Interview Line:
“Servlets handle logic, JSP handles presentation.”

7) What are Filters in Servlets?

Answer:
Filters act like middleware for requests. They can intercept requests or responses before
reaching a Servlet. They are useful for logging, authentication, or modifying data.

Example:

public class AuthFilter implements Filter {

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws


IOException, ServletException {

// check authentication

[Link](req, res);

Where used in project:

 Authorization checks

 Logging requests and responses

Interview Line:
“Filters let us process requests before they reach the main servlet.”

8) What is Session and Cookie? How are they different?

Answer:
A session is server-side storage that remembers user data during a visit. A cookie is client-side
storage stored in the browser. Cookies travel with each request, while sessions stay on the
server.

Example:
HttpSession session = [Link]();

[Link]("user", "John");

Cookie cookie = new Cookie("token", "abc123");

[Link](cookie);

Where used in project:

 Maintaining user login state

 Personalizing user experience

Interview Line:
“Sessions store data on server, cookies store data on client.”

9) What is Transaction in JDBC?

Answer:
A transaction is a set of database operations treated as a single unit. Either all operations
succeed, or all fail. You can commit or rollback depending on success.

Example:

[Link](false);

try {

[Link]("INSERT INTO ...");

[Link]("UPDATE ...");

[Link]();

} catch(Exception e) {

[Link]();

Where used in project:

 Banking or telecom systems where operations must be atomic

 Batch updates
Interview Line:
“Transactions ensure database consistency by committing or rolling back all changes.”

10) What is Caching in Java applications?

Answer:
Caching means storing frequently used data in memory so that it can be retrieved faster
without hitting the database every time. This improves performance and reduces load on
backend systems.

Example:

Map<String,String> cache = new ConcurrentHashMap<>();

[Link]("user_1", "John");

String value = [Link]("user_1");

Where used in project:

 Frequently accessed reference data

 Reducing database calls for repeated queries

Interview Line:
“Caching helps applications respond faster and reduces unnecessary database hits.”

11) What is JPA and why do we use it?

Answer:
JPA (Java Persistence API) is a specification that allows us to map Java objects to database
tables. It handles CRUD operations, relationships, and transactions, so we don’t have to write a
lot of boilerplate SQL.

Example:

@Entity

class Employee {

@Id

private int id;

private String name;


}

Where used in project:

 Mapping entities to tables

 CRUD operations in services

Interview Line:
“JPA lets me work with objects instead of writing raw SQL.”

12) What is the difference between Lazy and Eager loading?

Answer:
Lazy loading means an object’s related data is loaded only when it’s actually needed. Eager
loading means all related data is loaded immediately. Lazy is better for performance if we don’t
always need the related data.

Where used in project:

 Fetching related entities in services

 Avoiding unnecessary database queries

Interview Line:
“Lazy loading loads data only when needed, eager loading loads it all at once.”

13) What is JMS (Java Message Service)?

Answer:
JMS allows Java applications to send messages to each other asynchronously. It helps decouple
services so one service can send a message without waiting for another to process it.

Example:

MessageProducer producer = [Link](queue);

TextMessage msg = [Link]("Hello");

[Link](msg);

Where used in project:

 Event-driven integrations
 Asynchronous notifications in telecom systems

Interview Line:
“JMS helps different parts of a system communicate asynchronously.”

14) What are Message Queues and Topics in JMS?

Answer:
A queue is a point-to-point messaging model where one consumer receives a message. A topic
is a publish-subscribe model where multiple subscribers can receive the same message.

Where used in project:

 Queues → processing requests one at a time

 Topics → broadcasting events to multiple services

Interview Line:
“Queue is one-to-one, topic is one-to-many.”

15) What is a Thread Pool and why do we use it?

Answer:
A thread pool is a collection of pre-created threads ready to execute tasks. Instead of creating a
new thread every time, we reuse threads, which saves memory and improves performance.

Example:

ExecutorService executor = [Link](10);

[Link](() -> [Link]("Running task"));

Where used in project:

 Handling concurrent tasks in backend

 Async processing of API calls

Interview Line:
“Thread pools improve performance and prevent creating too many threads.”

16) What is a Callable vs Runnable?


Answer:
Runnable is used to run a task without returning a result. Callable is similar but can return a
value and throw checked exceptions.

Example:

Callable<Integer> task = () -> 123;

Future<Integer> future = [Link](task);

Where used in project:

 Background tasks where we need results or exceptions

Interview Line:
“Callable returns a result, Runnable doesn’t.”

17) What is a Future in Java?

Answer:
Future represents the result of an asynchronous computation. We can check if it’s done, wait for
it, or cancel it. Useful with thread pools.

Example:

Future<Integer> future = [Link](() -> 42);

Integer result = [Link]();

Where used in project:

 Async API responses

 Batch processing

Interview Line:
“Future lets us work with results that aren’t ready yet.”

18) What is Session Management in Java Web Apps?

Answer:
Session management is how a web application keeps track of a user across multiple requests.
This can be done via HttpSession, cookies, or URL rewriting.

Where used in project:


 User login state

 Maintaining preferences or temporary data

Interview Line:
“Session management helps track users between requests.”

19) What is Clustering in Java Web Applications?

Answer:
Clustering is running multiple server instances so the application can handle more users and
provide high availability. Sessions can be replicated across nodes to avoid losing user data.

Where used in project:

 High availability systems

 Load-balanced web apps

Interview Line:
“Clustering improves scalability and fault tolerance.”

20) What is Connection Management in Java?

Answer:
Connection management is controlling how your application opens, reuses, and closes
connections to resources like databases or message brokers. Connection pools are the most
common approach.

Where used in project:

 Efficient database usage

 Preventing resource leaks

Interview Line:
“Good connection management ensures reliability and performance.”

21) What are Deadlocks in Java Multithreading?


Answer:
A deadlock happens when two or more threads are waiting for each other to release resources,
and none can proceed. Avoid it by acquiring locks in a consistent order or using timeouts.

Where used in project:

 Multithreaded backend jobs

 Concurrent processing in integration systems

Interview Line:
“Deadlocks happen when threads wait on each other indefinitely.”

22) What is the difference between Checked and Unchecked exceptions in Advanced Java?

Answer:
Checked exceptions must be handled at compile time (like IOException). Unchecked exceptions
occur at runtime (like NullPointerException) and are usually programmer errors.

Where used in project:

 Checked → network, DB, file operations

 Unchecked → bugs or unexpected runtime issues

Interview Line:
“Checked exceptions force handling, unchecked signal errors.”

23) How do you handle Transactions in Advanced Java?

Answer:
Transactions ensure multiple operations either all succeed or all fail. We can manage them
programmatically via JDBC or declaratively using frameworks.

Example:

[Link](false);

try {

[Link](...);

[Link]();

} catch(Exception e) {
[Link]();

Where used in project:

 Ensuring data consistency in iPaaS systems

 Batch updates

Interview Line:
“Transactions make sure the system doesn’t end up in a partial state.”

24) What is Lazy Initialization and where do we use it?

Answer:
Lazy initialization means creating objects only when they are needed, instead of at startup. This
saves memory and improves startup time.

Where used in project:

 Large objects or data that aren’t always needed

 Reducing memory footprint

Interview Line:
“Lazy initialization delays object creation until it’s actually required.”

25) What is Caching Strategy in Java Applications?

Answer:
Caching strategy defines what data to store, how long to keep it, and when to invalidate it.
Frequently accessed reference data is a good candidate for caching.

Where used in project:

 Reduce repeated DB hits

 Improve API response times

Interview Line:
“A good caching strategy improves performance and reduces load on backend systems.”

You might also like