KEMBAR78
Java Question Bank Answers | PDF | Network Socket | Port (Computer Networking)
0% found this document useful (0 votes)
34 views43 pages

Java Question Bank Answers

The document is a comprehensive Java Question Bank that covers various topics including TCP/IP Client and Server Sockets, Datagram Sockets, InetAddress, URL and URLConnection classes, RMI architecture, JDBC architecture, and servlet technology. It includes explanations, methods, and examples related to each topic, along with practical programming tasks. The content is structured into chapters with specific questions and answers aimed at enhancing understanding of Java networking and database connectivity.

Uploaded by

tiktakkids01
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)
34 views43 pages

Java Question Bank Answers

The document is a comprehensive Java Question Bank that covers various topics including TCP/IP Client and Server Sockets, Datagram Sockets, InetAddress, URL and URLConnection classes, RMI architecture, JDBC architecture, and servlet technology. It includes explanations, methods, and examples related to each topic, along with practical programming tasks. The content is structured into chapters with specific questions and answers aimed at enhancing understanding of Java networking and database connectivity.

Uploaded by

tiktakkids01
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/ 43

lOMoARcPSD|39694526

JAVA Question BANK Answers

java question answer (Adani Institute of Infrastructure Management)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Brian Trodd (troddbrian@gmail.com)
lOMoARcPSD|39694526

JAVA QUESTION BANK ANSWERS

CHAPTER - 1

1. Explain TCP/IP Client Socket and its methods.


In Java, TCP/IP Client Socket used to implement reliable, bidirectional,
persistent, point-to-point, stream-based connections between hosts on the
Internet. A socket can be used to connect Java’s I/O system to other programs
that may reside either on the local machine or on any other machine on the
Internet.
There are two kinds of TCP/IP Client Socket in Java. One is for servers, and the
other is for clients. The ServerSocket class is designed to be a “listener,” which
waits for clients to connect before doing anything. Thus, ServerSocket is for
servers. The Socket class is for clients. It is designed to connect to server sockets
and initiate protocol exchanges.

METHODS :-

InetAddress getInetAddress(): Returns the InetAddress associated with the


Socket object. It returns null if the socket is not connected.

int getPort(): It returns the remote port to which the invoking Socket object is
connected. It returns 0 if the socket is not connected.

int getLocalPort(): It returns the local port to which the invoking Socket object is
bound. It returns –1 if the socket is not bound.

InputStream getInputStream( ) throws IOException: Returns the InputStream


associated with the invoking socket.

OutputStream getOutputStream( ) throws IOException: Returns the


OutputStream associated with the invoking socket.

2. Explain TCP/IP Server Socket and its methods.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

3. What is a Datagram socket? Explain it with Example.


Datagrams are collection of information sent from one device to another device
via the established network.
Java DatagramSocket class
Java DatagramSocket class represents a connection-less socket for sending and
receiving datagram packets. It is a mechanism used for transmitting datagram
packets over network.`
A datagram is basically an information but there is no guarantee of its content,
arrival or arrival time.
Commonly used Constructors of DatagramSocket class

● DatagramSocket() throws SocketEeption: it creates a datagram socket and


binds it with the available Port Number on the localhost machine.
● DatagramSocket(int port) throws SocketEeption: it creates a datagram
socket and binds it with the given Port Number.
● DatagramSocket(int port, InetAddress address) throws SocketEeption: it
creates a datagram socket and binds it with the specified port number
and host address.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

4.Write a TCP or UDP client and server program to do the following:


Client>LocalHost/IP Port<enter> Enter text:WelcomeSOU<enter> Response
from server: uos EMOCLEw

5. Explain Inet Address in detail.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

Java InetAddress class represents an IP address. The java.net.InetAddress class


provides methods to get the IP of any host name for example
www.javatpoint.com, www.google.com, www.facebook.com, etc.
An IP address is represented by 32-bit or 128-bit unsigned number. An instance
of InetAddress represents the IP address with its corresponding host name.
There are two types of addresses: Unicast and Multicast. The Unicast is an
identifier for a single interface whereas Multicast is an identifier for a set of
interfaces.
Moreover, InetAddress has a cache mechanism to store successful and
unsuccessful host name resolutions.

6. Describe the URL and URLConnection class in detail.


Java URLConnection Class

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

The Java URLConnection class represents a communication link between the


URL and the application. It can be used to read and write data to the specified
resource referred by the URL.
What is the URL?

● URL is an abbreviation for Uniform Resource Locator. An URL is a form of


string that helps to find a resource on the World Wide Web (WWW).
● URL has two components:
1. The protocol required to access the resource.
2. The location of the resource.

Features of URLConnection class

1. URLConnection is an abstract class. The two subclasses


HttpURLConnection and JarURLConnection makes the connection
between the client Java program and URL resource on the internet.
2. With the help of URLConnection class, a user can read and write to and
from any resource referenced by an URL object.
3. Once a connection is established and the Java program has an
URLConnection object, we can use it to read or write or get further
information like content length, etc.

7. Explain RMI Architecture.


The RMI (Remote Method Invocation) is an API that provides a mechanism to
create distributed application in java. The RMI allows an object to invoke
methods on an object running in another JVM.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

The RMI provides remote communication between the applications using two
objects stub and skeleton.
Understanding stub and skeleton
RMI uses stub and skeleton object for communication with the remote object.
A remote object is an object whose method can be invoked from another JVM.

Architecture of an RMI Application


In an RMI application, we write two programs, a server program (resides on the
server) and a client program (resides on the client).

● Inside the server program, a remote object is created and reference of


that object is made available for the client (using the registry).
● The client program requests the remote objects on the server and tries to
invoke its methods.

Let's understand the stub and skeleton objects:


stub
The stub is an object, acts as a gateway for the client side. All the outgoing
requests are routed through it. It resides at the client side and represents the
remote object. When the caller invokes method on the stub object, it does the
following tasks:

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

1. It initiates a connection with remote Virtual Machine (JVM),


2. It writes and transmits (marshals) the parameters to the remote Virtual
Machine (JVM),
3. It waits for the result
4. It reads (unmarshals) the return value or exception, and
5. It finally, returns the value to the caller.

skeleton
The skeleton is an object, acts as a gateway for the server side object. All the
incoming requests are routed through it. When the skeleton receives the
incoming request, it does the following tasks:

1. It reads the parameter for the remote method


2. It invokes the method on the actual remote object, and
3. It writes and transmits (marshals) the result to the caller.

8. Explain RMI components.

9. Same as ques 4

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

CHAPTER - 2

1. Write a Short note on JDBC Architecture.


JDBC stands for Java Database Connectivity, which is a standard Java API for
database-independent connectivity between the Java programming language
and a wide range of databases.
JDBC Architecture
The JDBC API supports both two-tier and three-tier processing models for
database access but in general, JDBC Architecture consists of two layers −

● JDBC API − This provides the application-to-JDBC Manager connection.


● JDBC Driver API − This supports the JDBC Manager-to-Driver Connection.

The JDBC API uses a driver manager and database-specific drivers to provide
transparent connectivity to heterogeneous databases.
The JDBC driver manager ensures that the correct driver is used to access each
data source. The driver manager is capable of supporting multiple concurrent
drivers connected to multiple heterogeneous databases.
Following is the architectural diagram, which shows the location of the driver
manager with respect to the JDBC drivers and the Java application −

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

2. What is JDBC Connection? Explain Steps to get Database


Connection.
JDBC stands for Java Database Connectivity, which is a standard Java API for
database-independent connectivity between the Java programming language
and a wide range of databases.
There are 5 steps to connect any java application with the database using JDBC.
These steps are as follows:

● Register the Driver class


● Create connection
● Create statement
● Execute queries
● Close connection

1) Register the driver class


The forName() method of Class class is used to register the driver class. This
method is used to dynamically load the driver class.

Syntax of forName() method:-


public static void forName(String className)throws
ClassNotFoundException

2) Create the connection object


The getConnection() method of DriverManager class is used to establish
connection with the database.

Syntax of getConnection() method:-


public static Connection getConnection(String url,String name,String
password)throws SQLException

3) Create the Statement object


The createStatement() method of Connection interface is used to create
statement. The object of statement is responsible to execute queries with the
database.
Syntax of createStatement() method:-
public Statement createStatement()throws SQLException

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

4) Execute the query


The executeQuery() method of Statement interface is used to execute queries
to the database. This method returns the object of ResultSet that can be used
to get all the records of a table.
Syntax of executeQuery() method:-
public ResultSet executeQuery(String sql)throws SQLException

5) Close the connection object


By closing connection object statement and ResultSet will be closed
automatically. The close() method of Connection interface is used to close the
connection.
Syntax of close() method:-
public void close()throws SQLException

3. Which package is used to perform almost all JDBC operations?

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

4. List JDBC API components. Explain in brief.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

5. List JDBC driver. Explain JDBC – TO-ODBC bridge driver


(Type-1).

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

6. List JDBC driver .explain JDBC .NET pure java driver (Type -3)

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

7. What is a JDBC connection? Explain steps to get database


connection using java program.

8. Give the use of the following class: 1.driver managers 2.


Connection.

1. Driver Managers
The JDBC Driver Manager can connect Java application to a JDBC driver. Driver
Manager is the backbone of the JDBC architecture. It is quite small and simple.
2. Connection
A Connection is the session between java application and database.
The Connection interface is a factory of Statement, PreparedStatement, and
DatabaseMetaData i.e. object of Connection can be used to get the Statement
and DatabaseMetaData.
The Connection interface provide many methods for transaction like
commit(),rollback() etc

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

9. Develop a JDBC application that uses any JDBC drivers to


display all records.

10.Develop a JDBC application that uses any JDBC drivers to


delete a record.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

11. Develop a JDBC application that uses any JDBC drivers to


insert a record.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

12. Write the steps to develop a JDBC application with proper


examples.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

13. List advantages and disadvantages of using JDBC API.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

14. Write a short note on JDBC architecture.

15. Write a brief note on three tier database design.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

16. What is the difference between doGet () method and doPost


() method?

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

CHAPTER - 3

1. What is Servlet?Explain the life cycle of Servlet.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

2. Write a Servlet to read and display parameters using the


doGet method.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

3. Create a web form which processes servlets and


demonstrates use of cookies?

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

4. Write a short note on Reading initialization parameters in


servlet?

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

5. What is http/servlet Technology?

6. List the Methods of cookie class.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

7. Explain session tracking with examples.

8. Explain the advantages of Servlet.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

9. Describe HTTPServlet class with its methods with syntax.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

10. Explain JAVA servlet development kit.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

Chapter - 4

1. Develop JSP code to retrieve data from MySQL database.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

2. Explain the architecture of JSP with a neat diagram.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

3. Explain JSP Expressions.

4. Explain the life cycle of JSP.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

5. Write a JSP program of user login form with static data.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

6. Explain relation of applets and servlet with JSP.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

7. Explain JSP scripting elements.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

8. Write a simple JSP program to print “HELLO”.

9. Explain <scriptlet> tag with syntax.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

10. Develop a JSP program to display the grade of a student


by accepting the marks of five subjects.

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

Downloaded by Brian Trodd (troddbrian@gmail.com)


lOMoARcPSD|39694526

11. Develop a JSP application to insert records in MySQL


database.

Downloaded by Brian Trodd (troddbrian@gmail.com)

You might also like