ADVANCED JAVA (BIS402)
Programs for Repeated Questions
MODULE - 1: COLLECTIONS FRAMEWORK
Q1: ArrayList Operations & Adding Elements
Question: Create a class STUDENT with two private members: USN, Name using LinkedList
class in Java. Write a program to add at least 3 objects of above STUDENT class and display
the data.
import [Link].*;
class Student { private String USN; private String Name; public Student(String USN,
String Name) { [Link] = USN; [Link] = Name; } public String getUSN()
{ return USN; } public String getName() { return Name; } public void display()
{ [Link]("USN: " + USN + ", Name: " + Name); } } public class
StudentLinkedList { public static void main(String[] args) { LinkedList<Student> list = new
LinkedList<>(); [Link](new Student("USN001", "Raj Kumar")); [Link](new
Student("USN002", "Priya Singh")); [Link](new Student("USN003", "Amit Patel"));
[Link]("\n========== Student List =========="); for(Student s : list) {
[Link](); } }}
Q2: StringBuffer Methods (MOST REPEATED - 4 times)
Question: Explain StringBuffer methods - append(), insert(), reverse(), replace()
public class StringBufferDemo { public static void main(String[] args) { StringBuffer sb =
new StringBuffer("Hello"); // append() - adds at end [Link](" World");
[Link]("After append(): " + sb); // Output: Hello World // insert() -
inserts at specific index [Link](5, " Java"); [Link]("After insert(): " + sb);
// Output: Hello Java World // reverse() - reverses the string StringBuffer sb2 =
new StringBuffer("Java"); [Link](); [Link]("After reverse(): " + sb2);
// Output: avaJ // replace() - replaces characters in range StringBuffer sb3 = new
StringBuffer("Hello World"); [Link](0, 5, "Hi"); [Link]("After replace():
" + sb3); // Output: Hi World } }
MODULE - 2: STRING HANDLING
Q3: String Constructors (Repeated 3 times)
Question: What is String in Java? Write a program demonstrating any six constructors of String
class.
public class StringConstructorsDemo { public static void main(String[] args) { //
Constructor 1: Empty string String s1 = new String(); [Link]("1. Empty: '" +
s1 + "'"); // Constructor 2: From string literal String s2 = new String("Hello
World"); [Link]("2. From literal: " + s2); // Constructor 3: From
character array char[] chars = {'J', 'a', 'v', 'a'}; String s3 = new String(chars);
[Link]("3. From char array: " + s3); // Constructor 4: From byte array
byte[] bytes = {72, 101, 108, 108, 111}; String s4 = new String(bytes);
[Link]("4. From byte array: " + s4); // Constructor 5: Substring from char
array String s5 = new String(chars, 0, 2); [Link]("5. Substring from char
array: " + s5); // Constructor 6: StringBuffer/StringBuilder StringBuffer sb = new
StringBuffer("Buffered"); String s6 = new String(sb); [Link]("6. From
StringBuffer: " + s6); } }
Q4: Duplicate Character Removal (Repeated 2 times)
Question: Write a program to remove duplicate characters from a given String and display the
String.
public class RemoveDuplicates { public static void main(String[] args) { String input =
"programming"; String output = removeDuplicates(input); [Link]("Original
String: " + input); [Link]("After removing duplicates: " + output); // Output:
progamin } public static String removeDuplicates(String str) { StringBuilder result =
new StringBuilder(); boolean[] seen = new boolean[256]; for(char c :
[Link]()) { if(!seen[c]) { [Link](c); seen[c] = true;
} } return [Link](); } }
MODULE - 3: SWING & GUI
Q5: Simple Swing Application
Question: Write a program to create a simple swing application with buttons.
import [Link].*; import [Link].*; public class SimpleSwingApp extends JFrame {
private JLabel label; private int count = 0; public SimpleSwingApp()
{ setTitle("Swing Demo"); setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(null); label = new
JLabel("Count: 0"); [Link](50, 30, 200, 30); add(label); JButton
btn1 = new JButton("Increment"); [Link](50, 80, 100, 30);
[Link](e -> { count++; [Link]("Count: " + count); });
add(btn1); JButton btn2 = new JButton("Reset"); [Link](160, 80, 80,
30); [Link](e -> { count = 0; [Link]("Count:
0"); }); add(btn2); setVisible(true); } public static void main(String[]
args) { new SimpleSwingApp(); } }
MODULE - 4: SERVLETS & JSP
Q6: Simple Servlet Program (MOST REPEATED - 4 times)
Question: Write a servlet program to accept two parameters from webpage, find their sum and
display result. Also provide HTML script to create webpage.
[Link]:
import [Link].*; import [Link].*; import [Link].*; public class SumServlet
extends HttpServlet { protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{ [Link]("text/html"); PrintWriter out = [Link]();
int num1 = [Link]([Link]("num1")); int num2 =
[Link]([Link]("num2")); int sum = num1 + num2;
[Link]("<html><body>"); [Link]("<h1>Sum: " + sum + "</h1>");
[Link]("</body></html>"); } }
[Link]:
<html> <body> <h1>Sum Calculator</h1> <form method="post" action="sumservlet">
Number 1: <input type="text" name="num1"><br> Number 2: <input type="text"
name="num2"><br> <input type="submit" value="Calculate"> </form> </body> </html>
Q7: Cookie Handling in Servlet
Question: Create cookie with name 'User name' and value 'xyz'. Display stored cookie in
webpage.
public class CookieServlet extends HttpServlet { protected void doGet(HttpServletRequest
request, HttpServletResponse response) throws
ServletException, IOException { [Link]("text/html"); PrintWriter out
= [Link](); // Create cookie Cookie cookie = new
Cookie("User_name", "xyz"); [Link](24 * 60 * 60); // 24 hours
[Link](cookie); // Display cookies [Link]("<html><body>");
[Link]("<h2>Cookies Set:</h2>"); Cookie[] cookies = [Link]();
if(cookies != null) { for(Cookie c : cookies) { [Link]([Link]() + " = " +
[Link]()); } } [Link]("</body></html>"); } }
MODULE - 5: JDBC
Q8: JDBC Database Connection (MOST REPEATED - 4 times)
Question: Explain different steps involved in JDBC process with code snippet.
import [Link].*; public class JDBCDemo { public static void main(String[] args)
{ Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null;
try { // Step 1: Load Driver [Link]("[Link]");
// Step 2: Create Connection conn =
[Link]( "jdbc:mysql://localhost:3306/testdb", "root",
"password"); [Link]("Connection Established"); // Step 3:
Create Statement pstmt = [Link]( "SELECT * FROM student
WHERE USN=?"); [Link](1, "USN001"); // Step 4: Execute
Query rs = [Link](); // Step 5: Process Result
while([Link]()) { [Link]("USN: " + [Link](1));
[Link]("Name: " + [Link](2)); } }
catch(ClassNotFoundException e) { [Link]("Driver not found: " + e); }
catch(SQLException e) { [Link]("Database error: " + e); } finally {
// Step 6: Close Resources try { if(rs != null) [Link](); if(pstmt !=
null) [Link](); if(conn != null) [Link](); } catch(SQLException e) {
[Link]("Error closing: " + e); } } }}
Q9: JDBC Transaction Processing
Question: Explain transaction processing in JDBC.
public class TransactionDemo { public static void main(String[] args) { Connection conn
= null; try { conn = [Link](
"jdbc:mysql://localhost:3306/bank", "root", "password"); // Disable
auto-commit for transaction [Link](false); Statement stmt
= [Link](); // Transfer money: Debit from A, Credit to B
[Link]("UPDATE accounts SET balance = " + "balance - 1000 WHERE
accno = 101"); [Link]("UPDATE accounts SET balance = " +
"balance + 1000 WHERE accno = 102"); // Commit transaction if no error
[Link](); [Link]("Transaction Committed"); }
catch(SQLException e) { try { // Rollback on error [Link]();
[Link]("Transaction Rolled Back"); } catch(SQLException ex)
{ [Link](); } } }}
===== END OF DOCUMENT =====