0% found this document useful (0 votes)
263 views10 pages

Ut 2 JPR (314317) (Set 1) Model Answer

This document is a Unit Test for the Diploma in Artificial Intelligence and Machine Learning program at K.E. Society’s Rajarambapu Institute of Technology for the course Java Programming. It includes instructions for the test, a set of questions covering various Java topics such as thread priority, event handling, socket types, and layout managers, along with their respective marks. The test is designed to assess students' understanding of Java programming concepts and their practical applications.

Uploaded by

sanikadesai2707
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)
263 views10 pages

Ut 2 JPR (314317) (Set 1) Model Answer

This document is a Unit Test for the Diploma in Artificial Intelligence and Machine Learning program at K.E. Society’s Rajarambapu Institute of Technology for the course Java Programming. It includes instructions for the test, a set of questions covering various Java topics such as thread priority, event handling, socket types, and layout managers, along with their respective marks. The test is designed to assess students' understanding of Java programming concepts and their practical applications.

Uploaded by

sanikadesai2707
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
You are on page 1/ 10

Model Answer Set-A

UNIT TEST -II Course Code-314317

Institute Name:K.E.Society’sRajarambapu Institute of


Technology(Polytechnic),LohagaonAcademic Year- 2024-25
Program Name-Diploma in Artificial Intelligence and Machine Learning Program Code: AIML
Semester: Fourth
Course & Course Code: Java Programming (314317)
Marks –30 Time – 1:30 hour

Instructions:
1. All questions are compulsory
2. Illustrate your answer with neat sketches wherever necessary
3. Figures to the right indicate full marks
4. Assume suitable data if necessary
5. Preferably, write the answers in sequential order
Q.1. Attempt any FIVE of the following 10

Q.N TL
Question CO
o. O
a Define Thread Priority.
Ans:-In java each thread is assigned a priority which affects the order in
which it is scheduled for running.Thread priority is used to decide when to
switch from one running thread to another. Threads of same priority are
given equal treatment by the javascheduler.
3.4 3
Thread priorities can take value from 1-10. Thread class defines
defaultpriority constant values as

MIN_PRIORITY=1
NORM_PRIORITY=5(DefaultPriority)
MAX_PRIORITY = 10
b Enlist any 2 Event class and Event listener Interface. 4.5 4
ans:-
Event class:-
1.Action event class
2.Item event class
3. Key event class
4. Mouse event class
5. text event class

Event Listener Interface :-


1. ActionListener
2. ItemListener
3. KeyListener
4. MouseListener
5. MouseMotion
6. TextListener
c Define socket and enlist its types.
ans:-In the Java programming language, a socket is defined as an endpoint
of a communication link that is established between two nodes or two
5.1 5
machines in a network. It has an IP address and port number assigned to it.
The main types of sockets in Java are stream sockets (using TCP) and
datagram sockets (using UDP).
d List two ways to create a Thread.
ans:-
1. By extending thread class
Syntax: - class Mythread extends Thread
{
-----
} 3.3 3
2. Implementing the Runnable Interface
Syntax: class MyThread implements Runnable
{
public void run()
{ ------ }
}
e Difference between AWT and Swing component (any 4 points).
Features Swing AWT
Platform Fully platform- Platform-dependent
Independence independent as it is since it uses native OS
written in Java. components.
Lightweight Yes, as it does not rely No, as it uses OS-
on native OS specific components.
components.
Package javax.swing java.awt
Look & Feel Can be customized OS-dependent look and
using Pluggable Look feel.
and Feel.
Performance Faster, since it is Slower due to reliance
purely Java-based. on native OS 4.3 4
components.
Components Rich set of components Limited components
(JButton, JTable, (Button, Label,
JTree). Checkbox).
Layout Manager Uses Swing Layout Uses AWT Layout
Managers like Managers like
BoxLayout, FlowLayout,
GroupLayout, BorderLayout,
SpringLayout. GridLayout.
Event Handling Uses Event Delegation Uses an older event
Model (more efficient). model (less efficient).
Extensibility More flexible and Less flexible due to OS
customizable. dependency.

f List any four controls from java.awt package. 4.1 4


ans:-
1. Label
2.Button
3.Textfield
4.Textfield Area
5.Checkbox
6.Checkboxgroup
7.List
g Define term a) URL Class b) URL Connection Class.
ans:-
a) URL class:-The java.net.URL class represents a Uniform Resource
Locator (URL), which is an address of a resource on the internet, like a
web page, file, or data stream.It encapsulates the information needed to
locate a resource, such as the protocol (e.g., http, https), hostname, port, 5.2 5
and path.
B) URL Connection Class:-The java.net.URLConnection class represents
a communication link between an application and a URL.It provides
methods for establishing a connection to the resource identified by the
URL and for reading and writing data to/from the resource.

Q.2. Attempt any FIVE of the following 20

Q.N TL C
Question
o. O O
a Explain Event Handling in java. 4.4 4
ans:-In Java, event handling is the mechanism by which a program responds
to user actions or system events, such as button clicks, mouse movements, or
keyboard input, using the Delegation Event Model. It consists of four main
components:

1. Event Source
2. Event Object
3. Event Listener
4. Event Handler

1.Events:

 An event is an action that occurs in a program, such as a button click,


mouse movement, or key press.

 Events are represented as objects containing event-related information.

 Java provides predefined event classes such as:

 ActionEvent (for button clicks)


 MouseEvent (for mouse actions)
 KeyEvent (for keyboard actions)

2. Event Sources:

 An event source is an object that generates an event.

 Examples: Buttons, text fields, checkboxes, frames, etc.


 When a user interacts with an event source, it creates an Event Object.

3.Event Listeners:

 A listener is an object that listens for events and responds when the event
occurs.

 It must implement a listener interface such as:

 ActionListener
 MouseListener
 KeyListener

 The listener is registered with the event source.

4. Event Handler:

 The event handler is the method that executes when an event occurs.
 Event listeners implement specific interfaces and override methods to define
event-handling behavior.

5.Event Handling (Delegation Event Model):

 The Event Source generates an Event Object when an event occurs.

 The Event Listener Object is registered with the Event Source.

 The listener implements the Listener Interface and defines the event-
handling method.

b Draw and Explain Thread life cycle 3.4 3


Ans:-Thread has five different states throughout its life.
1. New born State
2. Runnable State
3. Running State
4. Blocked State
5. Dead State
Thread should be in any one state of above and it can be move from one
state to another by different methods and ways.
1. New born State:-
 When a thread object is created it is said to be in a new born state.
 When the thread is in a new born state it is not scheduled running
from this state it can be scheduled for running by start() or killed by
stop().
Thread t = new Thread();
Moves to Runnable when start() is invoked.
2. Runnable State:-
 It means that thread is ready for execution and is waiting for the
availability of the processor
 When a thread is in the Runnable state, it means it is ready to run but
waiting for the CPU to be free. It has joined the queue of threads
waiting for execution.
 If all threads have the same priority, they take turns in a round-robin
manner, meaning each gets a small time slot to execute.
 When a thread finishes its time slot, it goes back to the queue and
waits for its next turn.
 A thread can give up its turn voluntarily before its time is up by
calling yield(), allowing other threads to run first.

t.start();

3. Running State

 When a thread is in the Running state, it means the CPU has given it time to
execute.
 The thread keeps running until:
o It voluntarily gives up control (for example, by calling yield()).
o A higher-priority thread takes over and interrupts it (preemption).

4. Blocked State:-
 A thread can be temporarily suspended or blocked from entering into
the runnable and running state by using either of the following
thread method.
o suspend() : Thread can be suspended by this method. It can be
rescheduled by resume().
o wait(): If a thread requires to wait until some event occurs, it can be
done using wait method and can be scheduled to run again by
notify().
o sleep(): We can put a thread to sleep for a specified time period
using sleep(time) where time is in ms. It reenters the runnable state
as soon as period has elapsed /over.
5. Dead State:-

 A thread enters the Dead State when it stops running


permanently.

o A thread stops automatically when it finishes its task (reaches


the end of the run() method).
o We can forcefully stop a thread using the stop()
o If a thread is in the Dead State, it cannot be restarted.

c Define Layout Manager. Explain any one with example. 4.1 4


Ans:-
o Layout means the arrangement of components within the container.
o A Layout Manager in Java (Swing or AWT) is responsible for arranging
components (like buttons, text fields, labels) inside a container (like a
JFrame or JPanel) automatically. It controls the size, position, and
alignment of UI elements.
o The layout manager is associated with every Container object. Each
layout manager is an object of the class that implements the Layout
Manager interface.

1. Border Layout:-
o The BorderLayout is used to arrange the components in five
regions: north, south, east, west, and center.
o Each region (area) may contain one component only. It is the
default layout of a frame or window.
o The 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
 Constructors
 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.
 Note: BorderLayout is the default layout manager for a JFrame's
content pane.
 Example:-

d Explain use of JTree in java Swing with example 4.3 4


Ans:-
o JTree is a Swing component with which we can display hierarchical
data.
o JTree is quite a complex component.
o A JTree has a 'root node' which is the top-most parent for all nodes in
the tree.
o A node is an item in a tree. A node can have many children nodes.
These children nodes themselves can have further children nodes.
o If a node doesn't have any children node, it is called a leaf node.
o To create a tree in a Java Swing application by using the following
APIs.
a. JTree(root): - to create a tree.
b. DefaultMutableTreeNode():- to create tree node
c. DefaultMutableTreeNode().add(node):- to add tree node to tree
node(child of root node).

Uses of JTree:

✅ File Explorer – Representing directories & files.


✅ Organizational Charts – Displaying employees under different departments.
✅ Navigation Menus – Creating a dynamic UI menu.
✅ Data Browsing – Viewing structured data in applications.

Example:-

e Draw and Explain AWT Hierarchy. 4.1 4


Ans:-
 Java AWT (Abstract Window Toolkit) is an API to develop GUI or
window-based applications in java.
 Java AWT components are platform-dependent i.e. components are
displayed according to the view of operating system. AWT is
heavyweight i.e. its components are using the resources of OS.
 The java.awt package provides classes for AWT api such as
TextField, Label, TextArea, RadioButton, CheckBox, Choice, List
etc.
 Components:-All the elements like the button, text fields, scroll bars, etc.
are called components. In Java AWT, there are classes for each component
as shown in above diagram. In order to place every component in a particular
position on a screen, we need to add them to a container.
 Container: - The Container is a component in AWT that can contain other
components like buttons, textfields, labels etc. The classes that extend
Container class are known as container such as Frame, Dialog and Panel.
A container itself is a component (see the above diagram), therefore we can
add a container inside container.
o Window:-The window is the container that has no borders and menu bars.
You must use frame, dialog or another window for creating a window.
o Panel:- The Panel is the container that doesn't contain title bar and menu
bars. It can have other components like button, textfield etc.
o Frame:- The Frame is the container that contain title bar and can have
menu bars. It can have other components like button, textfield etc.
o Dialog: The dialog class has a border and a title. A Dialog class instance
cannot exist without a Frame class instance.

f Write a java program in which thread A will display the even numbers 3.3 3
in between 1 to 20 and thread B will display odd numbers in between 1
to 20.
Ans:-
g Write a program to retrieve Host name and IP address using
InetAddress
Class.
Ans:-

5.2 5

You might also like