0% found this document useful (0 votes)
7 views

Paper 2 Solutions

The document is an examination paper for Advanced Java Programming for B. Tech (CSE) students in their fifth semester. It includes very short answer questions, short answer questions, and long answer questions covering topics like Java concepts, servlets, JDBC, JSP, and design patterns. The paper tests students' understanding of Java programming techniques, frameworks, and best practices.

Uploaded by

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

Paper 2 Solutions

The document is an examination paper for Advanced Java Programming for B. Tech (CSE) students in their fifth semester. It includes very short answer questions, short answer questions, and long answer questions covering topics like Java concepts, servlets, JDBC, JSP, and design patterns. The paper tests students' understanding of Java programming techniques, frameworks, and best practices.

Uploaded by

real.lucifer.007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

TCS–503

B. Tech. (CSE) (Fi h Semester)

EXAMINATION, 2017-18

ADVANCED JAVA PROGRAMMING

Sec on—A

(Very Short Answer Type Ques ons)

1. A empt all ques ons. 2 each

(a) JIT stands for Just-In-Time compila on. It's a technique used by Java to improve performance by
compiling bytecode into na ve machine code at run me.

(b) Debugging is the process of iden fying and fixing errors, bugs, or defects in a so ware program.

(c) GET and POST are HTTP methods used for sending data to a server. GET appends data to the URL,
while POST sends data in the HTTP request body.

(d) The finally block is used in excep on handling in Java. It contains code that always executes,
regardless of whether an excep on is thrown or not.

(e) Runnable is an interface in Java used for defining a task that can be executed concurrently.

(f) JVM allocates memory in mul ple areas, including Heap, Method Area, Stack, PC Registers, and Na ve
Method Stacks.

(g) Classloader is a subsystem of JVM responsible for loading classes into memory.

(h) Yes, Empty.java is a valid source file name in Java.

(i) The default value of local variables in Java is not assigned. They must be explicitly ini alized before
use.

(j) Sta c binding occurs at compile me and involves binding of methods to their respec ve class,
whereas dynamic binding occurs at run me and involves binding of methods to their respec ve objects.

(k) The join() method in Java is used to wait for a thread to die.

(l) No, calling the run() method directly won't start a new thread. You need to call the start() method to
start a new thread and execute the run() method.

(m) Shutdown hook is a thread that runs when the JVM is shu ng down, allowing cleanup opera ons to
be performed.

(n) Synchroniza on in Java is the process of controlling the access of mul ple threads to shared
resources to avoid data inconsistency.

(o) no fy() wakes up one wai ng thread, while no fyAll() wakes up all wai ng threads.
Sec on—B

(Short Answer Type Ques ons)

2. A empt any eight ques ons. 5 each

(a) The toString() method in Java is used to return a string representa on of an object. It is o en
overridden to provide meaningful informa on about the object's state.

(b) The life-cycle of a servlet involves ini aliza on, handling client requests, and destruc on. It includes
methods like init(), service(), and destroy().

(c) JDBC Driver is a so ware component that enables Java applica ons to interact with a database. There
are four types: JDBC-ODBC bridge driver, Na ve-API driver, Network Protocol driver, and Thin driver.

(d) Statement interface is used to execute sta c SQL queries, while PreparedStatement interface is used
to execute precompiled SQL queries with parameters, providing be er performance and security. Stored
procedures and func ons can be executed using CallableStatement interface.

(e) JSP (JavaServer Pages) is used in the MVC (Model-View-Controller) model as the View component.
Requests can be forwarded from a JSP page to a servlet using RequestDispatcher's forward() method.

(f) HTML is more lenient and allows errors, while XHTML is stricter and requires well-formed markup. For
example, in HTML, tags like <br> don't require closing, while in XHTML, they do (<br/>).

(g) ArrayList is not synchronized, while Vector is synchronized. ArrayList is faster but not thread-safe,
while Vector is slower but thread-safe.

(h) Iterator can traverse a collec on in one direc on only, whereas ListIterator can traverse a list
bidirec onally and modify the list during traversal.

(i) forward() method forwards the request from one servlet to another resource (servlet, JSP, or HTML)
within the same applica on on the server-side, while sendRedirect() method sends a response to the
client with a new URL, causing the client to make a new request.

(j) An abstract class can have both abstract and concrete methods, while an interface can only have
abstract methods. Abstract classes can have constructors, while interfaces cannot. Abstract classes
facilitate code reuse through inheritance, while interfaces promote loose coupling.

Sec on—C

(Long Answer Type Ques ons)

3. A empt any three ques ons. 10 each


(a) JSP implicit objects are objects that are automa cally available in JSP pages without any declara on.
Examples include request, response, session, applica on, out, and pageContext. They provide access to
various aspects of the request, response, and applica on environment.

(b) The life-cycle methods for a servlet include init(), service(), and destroy(). During ini aliza on, the
init() method is called. During handling client requests, the service() method is called to process
requests. Finally, during destruc on, the destroy() method is called to release any resources held by the
servlet. This life-cycle is depicted in a diagram showing the sequence of these methods.

(c) Struts is a framework used for building web applica ons in Java. It is based on the MVC architecture
and provides a set of components and classes to streamline the development process. IDE stands for
Integrated Development Environment, which is a so ware applica on that provides comprehensive
facili es to programmers for so ware development.

(d) JDBC code for execu ng the query "select * from T1" in Oracle database:

```java

import java.sql.*;

public class JDBCTest {

public sta c void main(String[] args) {

String url = "jdbc:oracle:thin:@localhost:1521:xe";

String user = "username";

String password = "password";

try {

Connec on connec on = DriverManager.getConnec on(url, user, password);

Statement statement = connec on.createStatement();

ResultSet resultSet = statement.executeQuery("SELECT * FROM T1");

while (resultSet.next()) {

// Process each row of the result set

resultSet.close();
statement.close();

connec on.close();

} catch (SQLExcep on e) {

e.printStackTrace();

```

(e) JavaBeans are reusable so ware components that follow certain conven ons like being serializable,
having a zero-argument constructor, and providing ge er and se er methods. J2EE (Java 2 Pla orm,
Enterprise Edi on) is a set of specifica ons and APIs for building and deploying enterprise applica ons.
N- er architecture is an architectural pa ern where the applica on is divided into mul ple ers or
layers, such as presenta on, business logic, and data storage, to improve scalability and maintainability.
Sec on C brief answers.

(a) JSP (JavaServer Pages) implicit objects are predefined objects available in every JSP page without any
explicit declara on. They provide access to various elements of the JSP environment and the request-
response cycle. Here are the JSP implicit objects:

1. **request**: Represents the client's request to the server. It provides methods to access request
parameters, headers, and a ributes.

2. **response**: Represents the server's response to the client. It allows se ng response headers and
content.

3. **out**: A PrintWriter object used to send output to the client's browser.

4. **session**: Represents the user session and allows storing session-specific data.

5. **applica on**: Represents the servlet context and allows storing applica on-wide data.

6. **config**: Provides access to the servlet configura on parameters specified in the web.xml file.

7. **pageContext**: Provides access to page-specific a ributes and other implicit objects.

8. **page**: Represents the current JSP page itself.

9. **excep on**: Represents any excep on that occurred during JSP page execu on (available only
within error pages).

Each implicit object serves a specific purpose and simplifies the development process by providing
access to commonly used func onali es without the need for addi onal code.

(b) The life-cycle methods for a servlet define the sequence of events that occur from the servlet's
ini aliza on to its destruc on. The three main life-cycle methods for a servlet are:

1. **init()**: This method is called by the servlet container when the servlet is first created. It is used for
one- me ini aliza on tasks such as loading configura on parameters or establishing database
connec ons. The init() method takes a ServletConfig object as its parameter.
2. **service()**: This method is invoked by the servlet container to handle client requests. It is called for
each request and is responsible for processing the request, genera ng a response, and sending it back to
the client. The service() method takes H pServletRequest and H pServletResponse objects as
parameters.

3. **destroy()**: This method is called by the servlet container when the servlet is being destroyed,
typically during server shutdown or when the servlet container decides to remove the servlet from
service. It is used for releasing any resources held by the servlet, such as closing database connec ons or
releasing memory.

Here is a diagram illustra ng the life-cycle of a servlet:

```

Ini aliza on

+------+ +-------+ +-------+

| init | ---> |service| ---> | destroy|

+------+ +-------+ +-------+

```

(c) Struts is a framework for developing Java web applica ons. It is built on top of servlets and JavaServer
Pages (JSP) and follows the Model-View-Controller (MVC) design pa ern. Struts provides a set of
components and classes to facilitate the development process, including controllers, ac ons, forms, and
validators.

The main components of the Struts framework are:

1. **Controller**: The controller component in Struts is responsible for handling incoming requests,
dispatching them to appropriate ac ons, and managing the naviga on flow of the applica on.

2. **Ac ons**: Ac ons are Java classes that encapsulate the business logic of the applica on. They
process user input, interact with the model layer, and prepare data for presenta on.
3. **Forms**: Forms are JavaBeans that represent the data submi ed by the user. They provide an
abstrac on layer for accessing request parameters and valida ng user input.

4. **View**: The view component in Struts typically consists of JSP pages responsible for rendering the
user interface. Views display the data prepared by ac ons and interact with users through HTML forms
and links.

IDE stands for Integrated Development Environment, which is a so ware applica on that provides
comprehensive tools and features for so ware development. In the context of Java web development,
IDEs like Eclipse, IntelliJ IDEA, and NetBeans offer func onali es such as code edi ng, debugging, project
management, and deployment tools, making the development process more efficient and produc ve.

(d) Here's a JDBC code snippet for execu ng the query "select * from T1" on an Oracle database:

```java

import java.sql.*;

public class JDBCDemo {

public sta c void main(String[] args) {

String url = "jdbc:oracle:thin:@localhost:1521:xe";

String user = "username";

String password = "password";

try {

Connec on connec on = DriverManager.getConnec on(url, user, password);

Statement statement = connec on.createStatement();

ResultSet resultSet = statement.executeQuery("SELECT * FROM T1");

while (resultSet.next()) {

// Process each row of the result set


}

resultSet.close();

statement.close();

connec on.close();

} catch (SQLExcep on e) {

e.printStackTrace();

```

Replace "username" and "password" with your Oracle database creden als.

(e) Short notes on:

(i) **JavaBeans**: JavaBeans are reusable so ware components wri en in Java. They are typically
serializable, meaning they can be easily saved to and restored from a persistent storage mechanism.
JavaBeans follow specific conven ons, such as providing a default no-argument constructor, and ge er
and se er methods for accessing and modifying their proper es. They are widely used in Java
development for crea ng modular and maintainable applica ons.

(ii) **J2EE**: J2EE stands for Java 2 Pla orm, Enterprise Edi on. It is a set of specifica ons, APIs, and
protocols for building and deploying enterprise-level applica ons. J2EE provides a comprehensive
pla orm for developing distributed, mul ered, and highly scalable applica ons, with features such as
Enterprise JavaBeans (EJB), Java Servlets, JavaServer Pages (JSP), Java Message Service (JMS), and Java
Naming and Directory Interface (JNDI).

(iii) **N- er architecture**: N- er architecture is a so ware design pa ern that divides an applica on
into mul ple layers or ers, each responsible for specific tasks and func onali es. The typical layers
include presenta on (UI), business logic, and data storage. N- er architecture promotes modularity,
scalability, and maintainability by separa ng concerns and allowing each er to be developed, deployed,
and scaled independently.

You might also like