0% found this document useful (0 votes)
11 views55 pages

AJP Answers

The document contains answers to 12 questions related to Java programming concepts. Question 1 defines an ArrayList in Java and provides examples of how it can contain duplicate elements, maintain insertion order, and allow random access through indexes. Question 2 provides details on the BorderLayout and GridLayout layout managers in Java, including their constructors and usage. Question 3 states that AWT stands for Abstract Window Toolkit and asks to explain some AWT components.

Uploaded by

anurag.kakade20
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)
11 views55 pages

AJP Answers

The document contains answers to 12 questions related to Java programming concepts. Question 1 defines an ArrayList in Java and provides examples of how it can contain duplicate elements, maintain insertion order, and allow random access through indexes. Question 2 provides details on the BorderLayout and GridLayout layout managers in Java, including their constructors and usage. Question 3 states that AWT stands for Abstract Window Toolkit and asks to explain some AWT components.

Uploaded by

anurag.kakade20
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/ 55

ANSWERS are below :-

Q1) What is ArrayList in Java? Explain with example. [6]

Java ArrayList class uses a dynamic array for storing the elements. It is
like an array, but there is no size limit. We can add or remove elements
anytime. So, it is much more flexible than the traditional array. It is found
in the java.util package. It is like the Vector in C++.

The ArrayList in Java can have the duplicate elements also. It


implements the List interface so we can use all the methods of the List
interface here. The ArrayList maintains the insertion order internally.

It inherits the AbstractList class and implements List interface.

• Java ArrayList class can contain duplicate elements.


• Java ArrayList class maintains insertion order.
• Java ArrayList class is non synchronized.
• Java ArrayList allows random access because the array works on
an index basis.
• In ArrayList, manipulation is a little bit slower than the LinkedList in
Java because a lot of shifting needs to occur if any element is
removed from the array list.
• We can not create an array list of the primitive types, such as int,
float, char, etc. It is required to use the required wrapper class in
such cases. For example :
ArrayList <Integer> al = new ArrayList <Integer> () ;
• Java ArrayList gets initialized by the size. The size is dynamic in
the array list, which varies according to the elements getting added
or removed from the list.
Java ArrayList Vs Array
In Java, we need to declare the size of an array before we can use it.
Once the size of an array is declared, it's hard to change it.

To handle this issue, we can use the ArrayList class. It allows us to


create resizable arrays.

Unlike arrays, arraylists can automatically adjust their capacity when we


add or remove elements from them. Hence, arraylists are also known as
dynamic arrays.
Q2 ) Write a short note on : [6]
i) Border Layout
ii) Grid Layout

The LayoutManagers are used to arrange components in a particular


manner.

The Java LayoutManagers facilitates us to control the positioning and


size of the components in GUI forms.

LayoutManager is an interface that is implemented by all the classes of


layout managers.

There are the following classes that represent the border layout and Grid
layout managers:

java.awt.BorderLayout
java.awt.GridLayout

Java BorderLayout

The BorderLayout is used to arrange the components in five regions:


north, south, east, west, and center. Each region (area) may contain one
component only. It is the default layout of a frame or window. The
Constructors of BorderLayout class:

BorderLayout(): creates a border layout but with no gaps between the


components.

BorderLayout(int hgap, int vgap): creates a border layout with the given
horizontal and vertical gaps between the components.

BorderLayout provides five constants for each region:

public static final int NORTH


public static final int SOUTH
public static final int EAST
public static final int WEST
public static final int CENTER

JAVA GRIDLAYOUT

The Java GridLayout class is used to arrange the components in a


rectangular grid. One component is displayed in each rectangle.

Constructors of GridLayout class

GridLayout(): creates a grid layout with one column per component in a


row.
GridLayout(int rows, int columns): creates a grid layout with the given
rows and columns but no gaps between the components.
GridLayout(int rows, int columns, int hgap, int vgap): creates a grid
layout with the given rows and columns along with given horizontal and
vertical gaps.

Example of GridLayout class: Using GridLayout() Constructor


The GridLayout() constructor creates only one row. The following
example shows the usage of the parameterless constructor.
Q3) What are AWT component? Explain some AWT component. [6]

AWT stands for Abstract Windows Toolkit


Q4) Write a program to add Three Menu Item and One Sub Menu to
Menu,
add two MenuItem to SubMenu and add these components to
Menubar.
[10]

Java AWT MenuItem and Menu


The object of MenuItem class adds a simple labeled menu item on
menu. The items used in a menu must belong to the MenuItem or any of
its subclass.

The object of Menu class is a pull down menu component which is


displayed on the menu bar. It inherits the MenuItem class.
Q5) Write a short note on : [8]
i) Hashing in java
ii) Java.util Package

• The hash function is a key-value mapping function.


• When two or more keys are mapped to the same value using
these hashing methods, there exists duplicate values.
• The use of chain hashing prevents collisions.
• Each hash table cell should lead to a linked list of entries that have
the same hash function value.
• Example :
Java.util Package
It contains the collections framework, legacy collection classes, event
model, date and time facilities, internationalization, and miscellaneous
utility classes (a string tokenizer, a random-number generator, and a bit
array).

The use of java.util package is as follows

• It can be use for Java collections.


• It can be use for internationalization support by using the
internationalization supported classes from java.util package.
• It can be use for random number generation.
• It can be use for string parsing.
• It can be use for base64 encoding and decoding.
Q6) What is JDBC? Explain driver types of JDBC. [9]
Q7) Give the details of the Executing SQL commands. [8]
Q8) What is database connection? Explain how to connect any java
application with the database using JDBC. [9]
Q9) Give a brief explanation for executing queries. [8]
Q10) Define RMI and explain the architecture of RMI with suitable
diagram. State the goals of RMI. [9]
Following are the goals of RMI −

• To minimize the complexity of the application.


• To preserve type safety.
• Distributed garbage collection.
• Minimize the difference between working with local and remote
objects.
Q11) Explain the concept of stub, skeleton, parameter marshalling
and un-marshalling in the context of RMI. [9]

RMI stands for Remote Method Invocation. It is a mechanism that allows


an object residing in one system (JVM) to access/invoke an object
running on another JVM.

RMI is used to build distributed applications; it provides remote


communication between Java programs. It is provided in the package
java.rmi.

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.
RMI Architecture
Let us now discuss the components of this architecture.

• Transport Layer − This layer connects the client and the server. It
manages the existing connection and also sets up new
connections.

• Stub − A stub is a representation (proxy) of the remote object at


client. It resides in the client system; it acts as a gateway for the
client program.

• Skeleton − This is the object which resides on the server side. stub
communicates with this skeleton to pass request to the remote
object.
• RRL(Remote Reference Layer) − It is the layer which manages the
references made by the client to the remote object.

Working of an RMI Application


The following points summarize how an RMI application works −

When the client makes a call to the remote object, it is received by the
stub which eventually passes this request to the RRL.

When the client-side RRL receives the request, it invokes a method


called invoke() of the object remoteRef. It passes the request to the RRL
on the server side.

The RRL on the server side passes the request to the Skeleton (proxy
on the server) which finally invokes the required object on the server.

The result is passed all the way back to the client.


Stub & Skleton
Marshalling and Unmarshalling
Whenever a client invokes a method that accepts parameters on a
remote object, the parameters are bundled into a message before being
sent over the network. These parameters may be of primitive type or
objects. In case of primitive type, the parameters are put together and a
header is attached to it. In case the parameters are objects, then they
are serialized. This process is known as marshalling.

At the server side, the packed parameters are unbundled and then the
required method is invoked. This process is known as unmarshalling.
RMI Registry
RMI registry is a namespace on which all server objects are placed.
Each time the server creates an object, it registers this object with the
RMIregistry (using bind() or reBind() methods). These are registered
using a unique name known as bind name.

To invoke a remote object, the client needs a reference of that object. At


that time, the client fetches the object from the registry using its bind
name (using lookup() method).

The following illustration explains the entire process −


Q12)
a) Compare between Stub and Skelton? Explain with neat diagram
the RMI Architecture. [9]
RMI Architecture :- refer Q10, Q11
Comparing stub and skeleton :
stub : A stub for a remote object acts as a client's local
representative or proxy for the remote object. The caller
invokes a method on the local stub which is responsible for
carrying out the method call on the remote object. In RMI,
a stub for a remote object implements the same set of
remote interfaces that a remote object implements.
When a stub's method is invoked, it does the following:

1.initiates a connection with the remote JVM containing the


remote object,
2.marshals (writes and transmits) the parameters to the
remote JVM,
3.waits for the result of the method invocation,
4.unmarshals (reads) the return value or exception
returned, and
returns the value to the caller.
* The stub hides the serialization of parameters and the
network-level communication in order to present a simple
invocation mechanism to the caller.
Skeleton :In the remote JVM, each remote object may have a
corresponding skeleton (in Java 2 platform-only
environments, skeletons are not required). The skeleton is
responsible for dispatching the call to the actual remote
object implementation. When a skeleton receives an incoming
method invocation it does the following:

1.unmarshals (reads) the parameters for the remote method,


2.invokes the method on the actual remote object
implementation, and
3.marshals (writes and transmits) the result (return value
or exception) to the caller.
Q13) Write a java program to perform addition of two numbers with
Client/ Server Application using RMI. [9]
Q14) Difference between Servlet and JSP. [5]
Q15) Explain in brief what are cookies. [4]
Q16) Explain Datagram, Datagram Socket and Datagram packet. [8]
Datagram
Datagram Socket
Datagram packet
Q17) Write a simple Java socket programming where client sends a
text and server receives and prints it. [10]
Q18) What is TCP/IP client socket in JAVA? Discuss some methods
of TCP / IP client socket class. [7]

You might also like