Advanced Java Programming (4351603) - Summer 2025 Solution
Solution guide for Advanced Java Programming (4351603) Summer 2025 exam
Question 1(a) [3 marks]
Write a difference between AWT and Swing.
Answer:
Table: AWT vs Swing Comparison
| Feature | AWT | Swing |
|---|---|---|
| Platform | Platform dependent | Platform independent |
| Components | Heavy weight | Light weight |
| Look and Feel | Native OS look | Pluggable look and feel |
| Performance | Faster | Slower than AWT |
- AWT: Uses native OS components
- Swing: Uses Java's own components
- Pluggability: Swing supports customizable UI
Mnemonic: "Swing is Smart - Platform Independent and Pluggable"
Question 1(b) [4 marks]
List out various Layout Managers. Explain Flow Layout manager with example.
Answer:
Layout Managers List:
- FlowLayout: Left to right arrangement
- BorderLayout: North, South, East, West, Center
- GridLayout: Equal sized grid cells
- CardLayout: Stack of components
- BoxLayout: Single row or column
FlowLayout Example:
Java
Mnemonic: "Flow Like Water - Left to Right"
Question 1(c) [7 marks]
Create a Swing program using checkbox that allows users to select multiple items from a list of options. Display the selected items.
Answer:
Java
Key Features:
- Multiple Selection: Users can select multiple checkboxes
- Real-time Display: Shows selected items immediately
- ItemListener: Handles checkbox state changes
Mnemonic: "Check Multiple, Display All"
Question 1(c) OR [7 marks]
Develop a Java program using various swing components.
Answer:
Java
Components Used:
- JTextField: Text input
- JComboBox: Dropdown selection
- JRadioButton: Single selection
- JButton: Action trigger
Mnemonic: "Text, Combo, Radio, Button - Complete Form"
Question 2(a) [3 marks]
Explain Swing controls with example.
Answer:
Table: Common Swing Controls
| Control | Purpose | Example |
|---|---|---|
| JButton | Click actions | new JButton("Click Me") |
| JTextField | Text input | new JTextField(10) |
| JLabel | Display text | new JLabel("Hello") |
| JCheckBox | Multiple selection | new JCheckBox("Option") |
Basic Example:
Java
Mnemonic: "Button, Text, Label, Check - Basic Four"
Question 2(b) [4 marks]
List JDBC drivers and explain any two.
Answer:
JDBC Drivers List:
- Type 1: JDBC-ODBC Bridge
- Type 2: Native API Driver
- Type 3: Network Protocol Driver
- Type 4: Thin Driver
Detailed Explanation:
Type 1 - JDBC-ODBC Bridge:
- Purpose: Converts JDBC calls to ODBC calls
- Advantage: Works with any ODBC database
- Disadvantage: Platform dependent, slower performance
Type 4 - Thin Driver:
- Purpose: Pure Java driver, direct database communication
- Advantage: Platform independent, best performance
- Disadvantage: Database specific
Mnemonic: "Bridge-Native-Network-Thin: 1-2-3-4"
Question 2(c) [7 marks]
Explain Object Relational Mapping (ORM) with its advantages and tools.
Answer:
Object Relational Mapping (ORM): ORM is a technique that maps object-oriented programming concepts to relational database structures.
Table: ORM Advantages
| Advantage | Description |
|---|---|
| Productivity | Reduces coding time |
| Maintainability | Easy to modify and update |
| Database Independence | Switch databases easily |
| Object-Oriented | Works with OOP concepts |
Popular ORM Tools:
- Hibernate: Most popular Java ORM
- JPA: Java Persistence API standard
- MyBatis: SQL mapping framework
- EclipseLink: Reference implementation
Working Model:
- Objects → ORM → Tables
- Automatic SQL generation
- Type-safe queries
Mnemonic: "Objects Relate Magically"
Question 2(a) OR [3 marks]
Describe MOUSEEVENT and MOUSELISTENER interface with example.
Answer:
MouseEvent: Generated when mouse actions occur on components.
MouseListener Interface Methods:
- mouseClicked(): Mouse button clicked
- mousePressed(): Mouse button pressed
- mouseReleased(): Mouse button released
- mouseEntered(): Mouse enters component
- mouseExited(): Mouse exits component
Example:
Java
Mnemonic: "Click-Press-Release-Enter-Exit"
Question 2(b) OR [4 marks]
List and explain the components of the JDBC API.
Answer:
Table: JDBC API Components
| Component | Purpose | Key Classes |
|---|---|---|
| DriverManager | Manages drivers | DriverManager.getConnection() |
| Connection | Database connection | Connection conn |
| Statement | SQL execution | Statement stmt |
| ResultSet | Query results | ResultSet rs |
Component Details:
- DriverManager: Establishes connection with database
- Connection: Represents database session
- Statement: Executes SQL queries
- ResultSet: Holds query results
Basic Usage:
Java
Mnemonic: "Driver Connects, Statement Executes, ResultSet Returns"
Question 2(c) OR [7 marks]
Draw and explain the architecture of Hibernate.
Answer:
Architecture Components:
Table: Hibernate Architecture
| Component | Function |
|---|---|
| Configuration | Reads config files |
| SessionFactory | Creates Session objects |
| Session | Interface to database |
| Transaction | Manages transactions |
| Query | HQL/SQL queries |
Layer Description:
- Application Layer: Java objects and business logic
- Hibernate Layer: ORM mapping and session management
- Database Layer: Actual data storage
Key Features:
- Automatic table creation: Based on entity classes
- HQL support: Object-oriented query language
- Caching: First and second level caching
Mnemonic: "Config-Factory-Session-Transaction: CFST"
Question 3(a) [3 marks]
Describe various features of Servlet.
Answer:
Table: Servlet Features
| Feature | Description |
|---|---|
| Platform Independent | Runs on any OS with JVM |
| Performance | Better than CGI |
| Robust | JVM managed memory |
| Secure | Java security features |
Key Features:
- Server-side processing: Handles client requests
- Protocol independent: HTTP, FTP, SMTP support
- Extensible: Can be extended easily
- Portable: Write once, run anywhere
Mnemonic: "Platform Performance Robust Secure"
Question 3(b) [4 marks]
Explain Servlet life cycle.
Answer:
Life Cycle Stages:
Table: Servlet Life Cycle
| Stage | Method | Purpose |
|---|---|---|
| Loading | Class loading | JVM loads servlet class |
| Instantiation | Constructor | Creates servlet object |
| Initialization | init() | One-time setup |
| Request Processing | service() | Handles requests |
| Destruction | destroy() | Cleanup resources |
Method Details:
- init(): Called once when servlet loads
- service(): Called for each request
- destroy(): Called when servlet unloads
Mnemonic: "Load-Create-Init-Service-Destroy"
Question 3(c) [7 marks]
Explain the session tracking in Servlet with example.
Answer:
Session Tracking Methods:
Table: Session Tracking Techniques
| Method | Description | Pros/Cons |
|---|---|---|
| Cookies | Client-side storage | Easy/Privacy issues |
| URL Rewriting | Append session ID | Universal/Ugly URLs |
| Hidden Fields | Form-based tracking | Simple/Form dependent |
| HttpSession | Server-side object | Secure/Memory usage |
HttpSession Example:
Java
Session Management:
- Creation:
request.getSession() - Storage:
session.setAttribute() - Retrieval:
session.getAttribute() - Invalidation:
session.invalidate()
Mnemonic: "Cookies-URLs-Hidden-HttpSession: CUHS"
Question 3(a) OR [3 marks]
Explain methods of Servlet life cycle.
Answer:
Life Cycle Methods:
Table: Servlet Life Cycle Methods
| Method | Called When | Parameters |
|---|---|---|
| init() | Servlet initialization | ServletConfig config |
| service() | Each request | ServletRequest req, ServletResponse res |
| destroy() | Servlet cleanup | None |
Method Details:
- init(ServletConfig config): Initialization code, database connections
- service(req, res): Request handling, business logic
- destroy(): Cleanup code, close resources
Example:
Java
Mnemonic: "Init-Service-Destroy: ISD"
Question 3(b) OR [4 marks]
Describe HTTPSERVLET class with example.
Answer:
HttpServlet Class: Abstract class extending GenericServlet, specifically for HTTP protocol.
HTTP Methods:
Table: HttpServlet Methods
| Method | HTTP Verb | Purpose |
|---|---|---|
| doGet() | GET | Retrieve data |
| doPost() | POST | Submit data |
| doPut() | PUT | Update data |
| doDelete() | DELETE | Remove data |
Example:
Java
Key Features:
- HTTP-specific: Designed for web applications
- Method handling: Separate methods for different HTTP verbs
- Request/Response: HttpServletRequest and HttpServletResponse
Mnemonic: "Get-Post-Put-Delete: GPPD"
Question 3(c) OR [7 marks]
Differentiate GET and POST methods and write a java code to develop Servlet using POST method.
Answer:
Table: GET vs POST Comparison
| Feature | GET | POST |
|---|---|---|
| Data Location | URL parameters | Request body |
| Data Limit | Limited (~2KB) | Unlimited |
| Security | Less secure | More secure |
| Caching | Cacheable | Not cacheable |
| Bookmarking | Possible | Not possible |
POST Method Servlet Example:
Java
HTML Form:
HTML
Key Differences:
- GET: Data in URL, visible, limited size
- POST: Data in body, hidden, unlimited size
Mnemonic: "GET Grabs, POST Protects"
Question 4(a) [3 marks]
List JSP Implicit Objects and explain any two.
Answer:
JSP Implicit Objects List:
- request (HttpServletRequest)
- response (HttpServletResponse)
- session (HttpSession)
- application (ServletContext)
- out (JspWriter)
- page (Object)
- pageContext (PageContext)
- config (ServletConfig)
- exception (Throwable)
Detailed Explanation:
request Object:
- Type: HttpServletRequest
- Purpose: Access request data and parameters
- Example:
String name = request.getParameter("name");
session Object:
- Type: HttpSession
- Purpose: Store user-specific data across requests
- Example:
session.setAttribute("user", username);
Mnemonic: "Request Response Session Application Out"
Question 4(b) [4 marks]
Explain features of JSP.
Answer:
Table: JSP Features
| Feature | Description | Benefit |
|---|---|---|
| Easy Development | HTML + Java | Faster coding |
| Platform Independent | Write once, run anywhere | Portability |
| Component-based | Reusable components | Maintainability |
| Secure | Java security model | Safe execution |
Key Features:
- Separation of Concerns: Design and logic separated
- Extensible: Custom tags and libraries
- Compiled: Translated to servlets for performance
- Expression Language: Simplified syntax
JSP Elements:
- Directives:
<%@ %> - Declarations:
<%! %> - Expressions:
<%= %> - Scriptlets:
<% %>
Mnemonic: "Easy Platform Component Secure"
Question 4(c) [7 marks]
Describe how to call JSP from servlet with example.
Answer:
Methods to Call JSP from Servlet:
Table: JSP Calling Methods
| Method | Interface | Purpose |
|---|---|---|
| Forward | RequestDispatcher | Transfer control |
| Include | RequestDispatcher | Include content |
| Redirect | HttpServletResponse | New request |
Forward Example:
Servlet Code:
Java
JSP Code (display.jsp):
jsp
Steps:
- Process data in servlet
- Set attributes in request
- Get RequestDispatcher with JSP path
- Forward to JSP
Mnemonic: "Process-Set-Get-Forward: PSGF"
Question 4(a) OR [3 marks]
List and explain JSP scripting elements.
Answer:
Table: JSP Scripting Elements
| Element | Syntax | Purpose | Example |
|---|---|---|---|
| Directive | <%@ %> | Page settings | <%@ page import="java.util.*" %> |
| Declaration | <%! %> | Define methods/variables | <%! int count = 0; %> |
| Expression | <%= %> | Output values | <%= new Date() %> |
| Scriptlet | <% %> | Java code | <% for(int i=0; i<5; i++) { %> |
Detailed Explanation:
Directives:
- Page directive: Import packages, set content type
- Include directive: Include other files
- Taglib directive: Custom tag libraries
Declarations:
- Define instance variables and methods
- Become part of servlet class
Mnemonic: "Direct Declare Express Script"
Question 4(b) OR [4 marks]
Explain JSP life cycle.
Answer:
Life Cycle Phases:
Table: JSP Life Cycle
| Phase | Method | Purpose |
|---|---|---|
| Translation | - | JSP to Java servlet |
| Compilation | - | Java to bytecode |
| Initialization | jspInit() | Setup resources |
| Request Processing | _jspService() | Handle requests |
| Destruction | jspDestroy() | Cleanup |
Key Points:
- Translation: JSP engine converts JSP to servlet
- Compilation: Java compiler creates .class file
- Execution: Servlet container executes compiled servlet
Mnemonic: "Translate-Compile-Init-Service-Destroy"
Question 4(c) OR [7 marks]
Define cookie. Explain working of cookie with example.
Answer:
Cookie Definition: A cookie is a small piece of data stored on the client's computer by the web browser while browsing a website.
Cookie Working Process:
Table: Cookie Attributes
| Attribute | Purpose | Example |
|---|---|---|
| Name | Cookie identifier | username |
| Value | Cookie data | john123 |
| Domain | Valid domain | .example.com |
| Path | Valid path | /shop/ |
| Max-Age | Expiry time | 3600 seconds |
Cookie Example:
Creating Cookie (Servlet):
Java
Reading Cookie (Servlet):
Java
Cookie Benefits:
- User personalization: Remember preferences
- Session tracking: Maintain state
- Analytics: Track user behavior
Mnemonic: "Create-Set-Add-Read: CSAR"
Question 5(a) [3 marks]
Write difference between JSP and Servlet.
Answer:
Table: JSP vs Servlet Comparison
| Feature | JSP | Servlet |
|---|---|---|
| Development | HTML + Java | Pure Java |
| Compilation | Automatic | Manual |
| Maintenance | Easier | More complex |
| Performance | Slower (first request) | Faster |
| Purpose | Presentation layer | Business logic |
Key Differences:
- JSP: Better for presentation, easier for web designers
- Servlet: Better for business logic, more control
- Coding: JSP mixes HTML and Java, Servlet is pure Java
- Compilation: JSP auto-compiles, Servlet needs manual compilation
Mnemonic: "JSP for Presentation, Servlet for Logic"
Question 5(b) [4 marks]
Define Spring Boot and explain its advantages.
Answer:
Spring Boot Definition: Spring Boot is a framework that simplifies the development of Spring-based applications by providing auto-configuration and embedded servers.
Table: Spring Boot Advantages
| Advantage | Description |
|---|---|
| Auto Configuration | Automatically configures Spring applications |
| Embedded Servers | Built-in Tomcat, Jetty support |
| Starter Dependencies | Pre-configured dependency sets |
| Production Ready | Health checks, metrics, monitoring |
Key Features:
- Rapid Development: Minimal configuration required
- Microservices: Perfect for microservice architecture
- No XML: Convention over configuration
- Cloud Ready: Easy deployment to cloud platforms
Example:
Java
Mnemonic: "Auto Embedded Starter Production"
Question 5(c) [7 marks]
Explain the architecture of Spring framework.
Answer:
Architecture Layers:
Table: Spring Framework Modules
| Module | Components | Purpose |
|---|---|---|
| Core Container | Core, Beans, Context | IoC and DI |
| Data Access | JDBC, ORM, JMS | Database operations |
| Web MVC | Web, Servlet, MVC | Web applications |
| AOP | Aspects, Weaving | Cross-cutting concerns |
Core Concepts:
- IoC (Inversion of Control): Framework controls object creation
- DI (Dependency Injection): Dependencies injected automatically
- AOP (Aspect-Oriented Programming): Modular cross-cutting concerns
- MVC: Model-View-Controller pattern
Spring Container:
- BeanFactory: Basic container
- ApplicationContext: Advanced container with additional features
Configuration Methods:
- XML Configuration: Traditional approach
- Annotation Configuration: Modern approach
- Java Configuration: Type-safe configuration
Mnemonic: "Core Data Web AOP Test"
Question 5(a) OR [3 marks]
Write advantages of JSP over Servlet.
Answer:
Table: JSP Advantages over Servlet
| Advantage | JSP | Servlet Limitation |
|---|---|---|
| Easy Development | HTML + Java tags | Complex HTML in Java |
| Automatic Compilation | Auto-compiled | Manual compilation |
| Designer Friendly | Web designers can work | Java knowledge required |
| Maintenance | Easier to modify | Code changes need recompilation |
Key Advantages:
- Separation of Design and Logic: HTML and Java separated
- Rapid Development: Faster prototyping and development
- Less Code: No need for out.println() statements
- Built-in Objects: Implicit objects readily available
Example Comparison:
JSP Code:
jsp
Servlet Code:
Java
Mnemonic: "Easy Auto Designer Maintenance"
Question 5(b) OR [4 marks]
Explain the advantages of Spring Boot.
Answer:
Table: Spring Boot Advantages
| Advantage | Description | Benefit |
|---|---|---|
| Auto Configuration | Automatic setup based on classpath | Reduced configuration |
| Embedded Server | Built-in Tomcat/Jetty | No external deployment |
| Starter POMs | Pre-configured dependencies | Simplified dependency management |
| Actuator | Production monitoring | Health checks and metrics |
Detailed Advantages:
1. Auto Configuration:
- Automatically configures Spring application based on dependencies
- Reduces boilerplate configuration code
- Convention over configuration approach
2. Embedded Servers:
- No need for external application servers
- Easy to run applications with
java -jar - Simplified deployment process
3. Starter Dependencies:
- Pre-configured dependency sets
- Version compatibility managed
- Quick project setup
4. Production Features:
- Health endpoints
- Metrics collection
- Application monitoring
Example:
Java
Mnemonic: "Auto Embedded Starter Production"
Question 5(c) OR [7 marks]
Explain MVC architecture.
Answer:
MVC (Model-View-Controller) Architecture:
MVC Components:
Table: MVC Components
| Component | Responsibility | Example |
|---|---|---|
| Model | Data and business logic | Entity classes, DAOs |
| View | User interface | JSP, HTML, Templates |
| Controller | Request handling | Servlets, Spring Controllers |
Detailed Explanation:
Model:
- Represents data and business logic
- Database operations
- Data validation
- Business rules implementation
View:
- Presentation layer
- User interface components
- Display data to users
- Collect user input
Controller:
- Handles user requests
- Coordinates between Model and View
- Process user input
- Select appropriate View
MVC Flow:
Spring MVC Example:
Controller:
Java
Model:
Java
View (JSP):
jsp
MVC Advantages:
- Separation of Concerns: Clear separation of responsibilities
- Maintainability: Easy to maintain and modify
- Reusability: Components can be reused
- Testability: Each component can be tested independently
- Parallel Development: Different teams can work on different components
MVC in Web Applications:
- Model: Database entities, business logic
- View: JSP pages, HTML templates
- Controller: Servlets, Spring controllers
Design Patterns Used:
- Front Controller: Single entry point for requests
- Observer Pattern: Model notifies View of changes
- Strategy Pattern: Different Views for same Model
Mnemonic: "Model manages data, View shows data, Controller controls flow"