Java Programming
Questions with Answer.
2 marks Question
1. Define throw and throws block.
• throw:
Used within a method or code block to create and raise an exception. It halts the
normal execution flow and transfers control to the nearest catch block or, if no catch block is found, to
the program's default exception handler. You can throw both checked and unchecked exceptions.
Example: throw new IllegalArgumentException("Invalid input");
• throws:
The caller is then responsible for handling these exceptions using try-catch blocks or by
declaring that they will also throw the same exceptions further up the call stack.
Example: throws IOException{ ... }
2. Define AWT Component.
Java's Abstract Window Toolkit (AWT), a component is an object with a graphical
representation that can be displayed on the screen and interact with the user, serving as the
foundation for building GUI elements like buttons, text fields, and windows
3. Define Button and Textfeild class
Button
A "Button class” refers to a UI element or component that allows users to trigger an action or
event when clicked
Textfeild
A text field is a basic text control that enables the user to type a small amount of text
4. Explain any two methods of URL connection.
1. getInputStream():
Once a connection is established using openConnection(), you can use getInputStream() to obtain
an input stream that allows you to read data from the connected resource. This stream can be used
to read the content of the resource, such as an HTML page, image, or other data.
2. getOutputStream():
Similarly, getOutputStream() allows you to obtain an output stream to write data to the connected
resource, enabling you to send data to the server. This is often used for sending data to a server via
HTTP POST requests.
5. Explain Datagram packet.
A datagram packet is a self-contained, independent unit of data transmitted over a network,
used in connectionless protocols like UDP, where each packet is routed independently and
delivery, arrival time, and order are not guaranteed.
6. Explain Datagram socket.
A datagram socket is a type of network socket that uses a connectionless protocol like UDP
(User Datagram Protocol) for sending and receiving data packets, where each packet is
individually addressed and routed, and there's no guaranteed order or delivery.
7. Explain layout managers in java.
In Java, layout managers are objects that control the size and position of components within a
container.
Java provides several built-in layout managers, each with its own approach to arranging
components
BorderLayout
GridLayout
CardLayout
FlowLayout
8. List any five classes of java sql package with their use.
1. java.sql.Connection
creates a connection with specific database
2. java.sql.Driver
create an instance of a driver with the DriverManager
3. java.sql.Statement
This class manages database drivers.
4. java.sql.PreparedStatement
Used to create and execute parameterized query.
5. java.sql.Statement
This interface is used to execute SQL statements.
9. Explain following AWT methods.
1. setLayout()
2. setSize()
1. setLayout()
setLayout() sets the layout manager for a container, controlling how components are
arranged
2. setSize()
sets the width and height of a component in pixels.
4 Marks Question
1. Explain any four swing classes with syntax.
1. JFrame
Represents the main window or frame of a Swing application. It's the top-level container
where you add other components.
• Syntax:
JFrame frame = new JFrame("My Window");
2. JButton
Represents a push button that can trigger an action when clicked.
• Syntax:
JButton button = new JButton("Click Me");
3. JLabel
Displays text or images (static content) on the GUI
• Syntax:
JLabel label = new JLabel("Hello, Swing!");
4. JTextField
Allows the user to enter and display text.
• Syntax:
JTextField textField = new JTextField();
2. Define and explain event classes in java
In Java, event classes represent specific occurrences or actions, like a button click or mouse
movement, and are used in event handling to manage interactions with the user interface.
• ActionEvent: Represents events triggered by actions, such as button clicks or menu selections.
• MouseEvent: Represents events related to mouse actions, like clicks, movements, and button
presses.
• KeyEvent: Represents events related to keyboard actions, like key presses and releases.
3. Explain constructor of class socket.
The Socket class constructor in Java is used to create a socket object, which represents an
endpoint for communication between two computers.
Socket(SocketType, ProtocolType): Initializes a new instance of the Socket class using the specified
socket type and protocol.
Socket(String hostName, int port): Creates a socket and connects it to the specified host and port.
Socket(InetAddress address, int port): Creates a socket and connects it to the specified address and
port.
4. Explain any four methods of URL connection with example.
1. openConnection(): This method, part of the URL class, creates
a URLConnection object, which represents the connection to the resource identified by
the URL.
2. getInputStream(): This method, part of the URLConnection class, returns
an InputStream that allows you to read data from the connected resource.
3. getOutputStream(): This method, part of the URLConnection class, returns
an OutputStream that allows you to write data to the connected resource (if the
connection supports it).
4. connect(): This method, part of the URLConnection class, establishes the actual
connection to the resource.
5. Difference between TCP and UDP protocol.
6. Explain Two tier architecture in JDBC application.
In a two-tier JDBC architecture, a Java application (client) directly communicates with a database
server (server) using JDBC drivers to send SQL queries and receive results, making it a simple and
direct approach, suitable for smaller applications.
• Direct Communication:
The Java application interacts directly with the database, without any intermediary layers.
• Client/Server Model:
This architecture is often referred to as a client/server configuration, where the Java application
acts as the client, and the database server acts as the server.
• JDBC Drivers:
JDBC drivers are essential for establishing the connection between the Java application and the
database.
• Example:
A simple desktop application that interacts with a local database to store and retrieve user data can
be a good example of a two-tier JDBC application.
7. Explain Listener interfaces in java.
Interface Description
ActionListener The listener interface for receiving action events.
AdjustmentListener The listener interface for receiving adjustment events.
AWTEventListener The listener interface for receiving notification of events
dispatched to objects that are instances of Component or
MenuComponent or their subclasses.
ComponentListener The listener interface for receiving component events.
ContainerListener The listener interface for receiving container events.
8. Difference between AWT and Swing component.