Java_Assignment_Answers
Java_Assignment_Answers
1. What is the difference between:i) AWT and Swingii) Scroll bar and Scrollpaneiii) Textfield and Textarea
Scrollbar vs ScrollPane:
Scrollbar is a component, ScrollPane is a container that wraps around components and adds scrolling automatically.
TextField vs TextArea:
TextField is for single-line input, TextArea for multi-line input.
2. Explain different types of Layout Manager with example.What are the two ways of creating Multithreading
Multithreading:
1. Extending Thread class
2. Implementing Runnable interface
Synchronization is used to control access to shared resources.
Daemon thread is a low-priority background thread.
3. Write a program for basic JDBC program that connects to a database and fetches some data?
JDBC Program:
import java.sql.*;
public class JDBCExample {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname", "root", "pass");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
while(rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2));
}
con.close();
}
}
4. What is socket Programming in Java explained with example.What is the difference between a byte stream
Socket Programming:
Used for communication between machines.
Server:
ServerSocket ss = new ServerSocket(6666);
Java Assignment Answers
Socket s = ss.accept();
Client:
Socket s = new Socket("localhost", 6666);