KEMBAR78
Java QB Ans | PDF | Network Socket | Port (Computer Networking)
0% found this document useful (0 votes)
21 views12 pages

Java QB Ans

The document outlines important Java concepts and questions, including the finally block, Swing components, InetAddress methods, URL connection methods, and Datagram packets. It also covers AWT components, TCP protocol, socket constructors, JDBC architecture, and listener interfaces. Additionally, it highlights the differences between AWT and Swing components.

Uploaded by

akshayryakawar
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)
21 views12 pages

Java QB Ans

The document outlines important Java concepts and questions, including the finally block, Swing components, InetAddress methods, URL connection methods, and Datagram packets. It also covers AWT components, TCP protocol, socket constructors, JDBC architecture, and listener interfaces. Additionally, it highlights the differences between AWT and Swing components.

Uploaded by

akshayryakawar
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/ 12

Java Important Ques on

2 Marks Ques on
1. Define finally block with syntax.
The finally block in Java is used to execute important code such as closing
resources, clean-up operations, etc. It is always executed after the try block,
regardless of whether an exception is thrown or caught.

Syntax:
try {
// Code that may throw an excep on
} catch (Excep onType e) {
// Code to handle the excep on
} finally {
// Code that will always execute (e.g., closing files or
database)

2. Define swing component.

A Swing component is a GUI (Graphical User Interface) element


in Java used to build desktop applica ons. These components
are part of the javax.swing package and are used to create
windows, bu ons, text fields, labels, checkboxes, etc.

3. Explain any two instance methods of class InetAddress.


InetAddress Class Methods: This class represents an Internet
Protocol IP address. Here are following usefull methods which
you would need while doing socket programming
1.String getHostAddress
Returns the IP address string in textual presenta on. String
2.getHostName
Gets the host name for this IP address
3. String toString Converts this IP address to a String

4. Explain any two methods of URL connec on.


connect():
Establishes a connec on to the URL resource. It is automa cally called
when reading or wri ng data, but can also be called manually.
getInputStream():
Returns an input stream to read data from the URL connec on,
commonly used to read web page content or API responses.

5. Explain Datagram packet.

A DatagramPacket is a Java class used for sending or receiving


data via UDP (User Datagram Protocol). It represents the data
packet exchanged between machines over a network.
It includes the data, destination IP address, and port number.

6. Explain Datagram socket.


A DatagramSocket is used to send or receive DatagramPackets
over UDP. It provides a way to transmit data without
establishing a connec on, making it faster but less reliable than
TCP sockets.

7. Explain layout managers in java.


A layout manager is a class that is useful to arrange components
in a par cular manner in container or a frame.
Java so people have created a LayoutManager interface in
java.awt package which is implemented in various classes which
provide various types of layouts to arrange the components. The
following classes represents the layout managers in Java:
1. FlowLayout
2. BorderLayout
3. GridLayout
4. CardLayout 5. GridBagLayout 6. BoxLayout
8. List any five classes of java sql package with their use

1. Connection – Connects to a database.


2. Statement – Executes static SQL queries.
3. PreparedStatement – Executes precompiled SQL queries with
parameters.
4. ResultSet –Stores results returned by a query.
5. DriverManager – Manages JDBC drivers for database connections.

9. Explain following AWT methods.


1. setLayout()
2. setSize()
setLayout(LayoutManager layout):
Sets the layout manager for arranging components in a container.
Example: setLayout(new FlowLayout());
setSize(int width, int height):
Sets the size of the window or component in pixels.
Example: setSize(400, 300);

4 Marks Ques on
1. Explain thread life cycle methods with syntax.
2. Explain any four swing classes with syntax.
1.JBu on Class :
The JBu on class provides the func onality of a push bu on.
JBu on allows an icon, a string, or both to be associated with
the push bu on
Syntax: JFrame frame = new JFrame("Title");

2.JFrame:
Frame represents a window with a tle bar and borders. Frame
becomes the basis for crea ng the GUIs for an applica on
because all the components go into the frame.
Syntax: JBu on bu on = new JBu on("Click Me");

3.JLabel:
• Jlabel is used to display a text – JLabel(string str) – JLabel(Icon
i) – JLabel(String s, Icon i, int align)
• CENTER, LEFT, RIGHT, LEADING, TRAILING
• Icon – is an interface – The easiest way to obtain icon is to use
ImageIcon class. ImageIcon class implements Icon interface
Syntax: JLabel label = new JLabel("Hello");

4. JText Fields :
The Swing text field is encapsulated by the JTextComponent
class, which extends JComponent. It provides func onality that
is common to Swing text components. One of its subclasses is
JTextField, which allows you to edit one line of text.
Syntax: JTextField textField = new JTextField(20);

3. Explain AWT component with its use.


AWT (Abstract Window Toolkit) is Java’s original GUI toolkit that
provides pla orm-dependent components.
AWT (Abstract Window Toolkit) components are GUI elements
in Java used for building desktop applica ons.
Common AWT Components:
 Label – Displays sta c text.
 Bu on – Creates a clickable bu on.
 TextField – Accepts user input.

4. Explain TCP protocol in java.


TCP: TCP stands for Transmission Control Protocol, which allows
for reliable communica on between two applica ons. TCP is
typically used over the Internet Protocol, which is referred to as
TCP/IP.
TCP (Transmission Control Protocol) provides reliable, ordered,
and connec on-oriented communica on.
Java supports TCP using:
 Socket – For client-side communica on.
 ServerSocket – For server-side.
Or
In Java, TCP/IP Client Socket used to implement reliable,
bidirec onal,persistent, point-to-point, stream-based
connec ons 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 ini ate protocol exchanges.

5. Explain constructor of class socket.

The constructor of the Socket class a empts to connect the


client to the specified server and port number. If communica on
is established, the client now has a Socket object capable of
communica ng with the server.

Java's Socket class is used to create client-side connec ons to a


server.
Constructors:
 Socket(String host, int port) – Connects to the specified
host and port.
 Socket(InetAddress address, int port) – Connects using IP
address.
Example:

Socket s = new Socket("127.0.0.1", 8080);

6. Explain any four methods of URL connec on with example.


1. connect()
 Descrip on:
Establishes a communica on link to the resource
referenced by the URL.
 Use:
It must be called before reading from or wri ng to the
connec on.
 Syntax:
urlConnec on.connect();

2. getInputStream()
 Descrip on:
Returns an InputStream to read the response data from
the URL.
 Use:
Used to read content like HTML, JSON, text from the
connected URL.
 Syntax:
InputStream in = urlConnec on.getInputStream();

3. getContentType()
 Descrip on:
Returns the MIME type of the content (e.g., "text/html",
"applica on/json").
 Use:
Helps iden fy the type of data returned by the server.
 Syntax:
String type = urlConnec on.getContentType();

4. getContentLength()
 Descrip on:
Returns the size of the content in bytes.
 Use:
Useful for checking the file size or data size of the URL
response.
 Syntax:
int size = urlConnec on.getContentLength();

7. Explain Three- er architecture in JDBC applica on.

1. Presenta on Layer (UI Layer)


 Role: Interface between the user and the system.
 Technologies: Java Swing, JavaFX, JSP, or HTML/CSS (in
web apps).
 Func on:
o Takes user input.
o Displays data from the backend.
o Sends user requests to the logic layer.
🔹 2. Business Logic Layer (Applica on Layer / Service Layer)
 Role: Processes user input and interacts with the database
using JDBC.
 Func on:
o Contains JDBC code to connect and communicate
with the database.
o Validates and applies logic to data before
inser ng/retrieving.

🔹 3. Data Layer (Database Layer)


 Role: Stores data in a database system like MySQL, Oracle,
PostgreSQL.
 Func on:
o Executes SQL queries.
o Returns results to the logic layer.
o Ensures data persistence and integrity

8. Explain Listener interfaces in java.

What are Listener Interfaces?

In Java, listener interfaces are part of event handling


mechanism used in GUI applica ons (like AWT or Swing).
They "listen" for user ac ons, such as bu on clicks, key presses,
mouse movement, etc.
Listeners are used to handle events triggered by user interac on.
Ac onListener
Purpose: Handles ac on events (e.g., bu on click).
This interface is used for receiving the ac on events
Method:
public void ac onPerformed(Ac onEvent e)
Used With: Bu on, MenuItem, etc.
KeyListener
Purpose: Handles keyboard events.
This interface is used for receiving the key events.
Methods:
public void keyPressed(KeyEvent e)
public void keyReleased(KeyEvent e)
public void keyTyped(KeyEvent e)
Used With: TextField, TextArea, etc.

MouseListener
Purpose: Handles mouse clicks and movement.
This interface is used for receiving the mouse events.
Methods:
public void mouseClicked(MouseEvent e)
public void mouseEntered(MouseEvent e)
public void mouseExited(MouseEvent e)
public void mousePressed(MouseEvent e)
public void mouseReleased(MouseEvent e)

WindowListener
Purpose: Handles window events like opening, closing,
minimizing, etc.
This interface is used for receiving the window events.
Methods:
public void windowOpened(WindowEvent e)
public void windowClosing(WindowEvent e)
public void windowClosed(WindowEvent e)
// etc.

9. Difference between AWT and Swing component.

You might also like