JAVA Question Bank Second Sessional
1 Explain the key differences between AWT and Swing.
2 What are the steps to connect a Java program with a database using JDBC?
3 Write a Java program to create a simple AWT Frame with a Button.
Explain the difference between Statement, PreparedStatement, and
4
CallableStatement.
5 Explain how ActionListener is used in AWT/Swing with an example program.
6 What is the purpose of the JFrame class in Swing?
7 6. Write a Java program to demonstrate multilevel inheritance.
8 What is an event in Java? Explain any two event listener interfaces.
What is the role of the DatagramSocket and DatagramPacket classes in UDP
9
communication?
10 What is the difference between URL and URLConnection classes in Java
11 . Explain any four Swing components with examples.
12 What is the purpose of the InetAddress class? How is it used?
13 What is JDBC? Explain its architecture
14 What is the role of the ResultSet interface in JDBC?
15 What is a socket in Java? Explain the difference between TCP and UDP sockets.
1|Page
JAVA Question Bank Second Sessional
1. Key Differences Between AWT and Swing:
Feature AWT Swing
Package [Link] [Link]
Components Heavyweight (OS-dependent) Lightweight (OS-independent)
Look & Feel Native OS look Customizable look
Performance Slower Faster
Example Button, Frame JButton, JFrame
2. Steps to Connect Java Program with Database Using JDBC:
1. Load the JDBC driver ([Link]("[Link]");).
2. Establish a connection (Connection con = [Link](url,
user, password);).
3. Create a statement (Statement stmt = [Link]();).
4. Execute a query (ResultSet rs = [Link]("SELECT * FROM
table");).
5. Process the results.
6. Close the connection.
3. Java Program to Create an AWT Frame with a Button:
import [Link].*;
class MyFrame extends Frame {
MyFrame() {
Button b = new Button("Click Me");
[Link](50, 50, 80, 30);
add(b);
setSize(300, 200);
setLayout(null);
setVisible(true);
}
public static void main(String args[]) {
new MyFrame();
}
}
4. Difference Between Statement, PreparedStatement, and CallableStatement:
Feature Statement PreparedStatement CallableStatement
Query Type Static Precompiled Stored Procedures
Performance Slower Faster Efficient for procedures
Parameters No parameters Uses ? placeholders Calls stored procedures
2|Page
JAVA Question Bank Second Sessional
5. ActionListener in AWT/Swing:
• It is used to handle button clicks.
• Example:
import [Link].*;
import [Link].*;
class ActionExample extends Frame implements ActionListener {
Button b;
ActionExample() {
b = new Button("Click");
[Link](50, 50, 80, 30);
[Link](this);
add(b);
setSize(200, 150);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
[Link]("Clicked");
}
public static void main(String args[]) {
new ActionExample();
}
}
6. Purpose of JFrame in Swing:
• It is the top-level container used to create a GUI window in Swing.
• Example:
import [Link].*;
class MySwing {
public static void main(String args[]) {
JFrame f = new JFrame("My Swing Window");
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
3|Page
JAVA Question Bank Second Sessional
7. Java Program to Demonstrate Multilevel Inheritance:
class A {
void displayA() { [Link]("Class A"); }
}
class B extends A {
void displayB() { [Link]("Class B"); }
}
class C extends B {
void displayC() { [Link]("Class C"); }
}
class Test {
public static void main(String args[]) {
C obj = new C();
[Link]();
[Link]();
[Link]();
}
}
8. Event in Java and Two Event Listener Interfaces:
• Event: An action like a button click or mouse movement.
• Two Event Listener Interfaces:
1. ActionListener (Handles button clicks)
2. MouseListener (Handles mouse events)
9. Role of DatagramSocket and DatagramPacket in UDP:
• DatagramSocket: Used for sending and receiving UDP packets.
• DatagramPacket: Represents the data to be sent or received.
10. Difference Between URL and URLConnection:
Feature URL URLConnection
Purpose Represents a web address Establishes a connection to the URL
Usage new URL("[Link] [Link]()
4|Page
JAVA Question Bank Second Sessional
11. Four Swing Components with Examples:
1. JLabel – Display text (JLabel l = new JLabel("Hello");).
2. JButton – Clickable button (JButton b = new JButton("Click");).
3. JTextField – Input field (JTextField t = new JTextField(20);).
4. JCheckBox – Checkbox (JCheckBox c = new JCheckBox("Accept");).
12. Purpose of InetAddress Class:
• Used to get the IP address of a system.
• Example:
import [Link].*;
class InetExample {
public static void main(String args[]) throws Exception {
InetAddress ip = [Link]();
[Link]("IP Address: " + [Link]());
}
}
13. What is JDBC? Explain Its Architecture:
• JDBC (Java Database Connectivity) allows Java programs to interact with
databases.
• Architecture:
o JDBC API: Provides database connection.
o JDBC Driver Manager: Loads database drivers.
o JDBC Driver: Connects Java with the database.
14. Role of ResultSet Interface in JDBC:
• It stores the results of a SQL query.
Example:
ResultSet rs = [Link]("SELECT * FROM students");
while([Link]()) {
[Link]([Link]("name"));
}
5|Page
JAVA Question Bank Second Sessional
15. What is a Socket in Java? Explain the Difference Between TCP and UDP
Sockets.
Socket in Java: A socket is an endpoint for communication between two
computers in a network.
• It allows data exchange using protocols like TCP and UDP.
Difference Between TCP and UDP Sockets:
TCP (Transmission Control UDP (User Datagram
Feature
Protocol) Protocol)
Connection-oriented (establishes a Connectionless (no
Connection
connection) connection setup)
Reliable (ensures data delivery) Unreliable (no guarantee of
Reliability
delivery)
Speed Slower due to error checking Faster but less reliable
Data Order Maintains data order Data may arrive out of order
Example Web browsing, file transfer Live streaming, gaming
Use
6|Page