MODULE 1 - Advanced Java
Q.1 (a) Collection Framework & Interfaces
Collection Framework:
Java Collection Framework provides architecture to store and manipulate a group of objects. It includes interfaces like
Collection, List, Set, Map, and classes like ArrayList, HashSet, TreeSet, etc.
i) Collection Interface:
Defines basic methods like:
- add(E e), addAll(Collection<? extends E> c)
- remove(Object o), clear()
- contains(Object o), containsAll(Collection<?> c)
- size(), isEmpty()
- iterator()
ii) List Interface:
Extends Collection. Ordered collection that allows duplicate elements.
- get(int index), set(int index, E element)
- add(int index, E element)
- remove(int index), indexOf(Object o)
- Implemented by ArrayList, LinkedList, etc.
iii) NavigableSet Interface:
Sorted set with navigation methods.
- lower(E e), floor(E e), ceiling(E e), higher(E e)
- pollFirst(), pollLast()
- Implemented by TreeSet.
iv) Queue Interface:
Designed for holding elements prior to processing.
- offer(E e), poll(), remove()
- peek(), element()
- Implemented by LinkedList, PriorityQueue.
Q.1 (b) Comparator Interface with TreeSet
Comparator Interface:
Used for custom sorting. Key methods:
- compare(T o1, T o2)
- equals(Object obj)
Example:
import java.util.*;
class ReverseOrder implements Comparator<Integer> {
public int compare(Integer a, Integer b) {
return b - a; // reverse order
public class TreeSetExample {
public static void main(String[] args) {
TreeSet<Integer> set = new TreeSet<>(new ReverseOrder());
set.add(10); set.add(5); set.add(20);
System.out.println(set); // Output: [20, 10, 5]
Q.1 (c) Array Class Methods
java.util.Arrays class provides utility methods:
- sort(array): Sorts array.
- binarySearch(array, key): Searches for key.
- fill(array, val): Fills array with val.
- equals(array1, array2): Compares arrays.
- copyOf(array, newLength)
Example:
import java.util.Arrays;
public class ArrayExample {
public static void main(String[] args) {
int[] arr = {3, 1, 4, 2};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr)); // [1, 2, 3, 4]
int index = Arrays.binarySearch(arr, 3);
System.out.println("Index of 3: " + index);
}
Q.2 (a) Legacy Classes
Legacy Classes in Java Collection:
1. Vector - Synchronized list.
2. Stack - Subclass of Vector.
3. Hashtable - Synchronized map.
4. Enumeration - Legacy iterator.
Example (Stack):
import java.util.*;
public class StackExample {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
stack.push("Java");
stack.push("Python");
System.out.println(stack.pop()); // Python
Q.2 (b) Spliterator in Java
Spliterator Interface:
Used for traversing and partitioning elements for parallel processing.
Key Methods:
- tryAdvance(Consumer action)
- trySplit()
- estimateSize()
- characteristics()
Example:
import java.util.*;
public class SpliteratorExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("Java", "Python", "C++");
Spliterator<String> spliterator = names.spliterator();
spliterator.tryAdvance(System.out::println); // Java
spliterator.forEachRemaining(System.out::println); // Python, C++
MODULE 2 - Strings in Java
Q.3 (a) StringBuffer Methods
Example:
StringBuffer sb = new StringBuffer("Java");
sb.append(" Programming"); // Java Programming
sb.insert(5, "is "); // Java is Programming
sb.reverse(); // gnimmargorP si avaJ
sb.delete(0, 5); // removes first 5 characters
Q.3 (b) String Comparison Methods
- equals(): checks content
- equalsIgnoreCase(): ignores case
- compareTo(): lexicographical difference
- contentEquals(): compares with StringBuffer
- regionMatches(): matches part of string
- matches(): regex match
Example:
String s1 = "Java", s2 = "JAVA";
s1.equals(s2); // false
s1.equalsIgnoreCase(s2); // true
s1.compareTo(s2); // 32
Q.3 (c) String vs StringBuffer vs StringBuilder
| Feature | String | StringBuffer | StringBuilder |
|-------------|------------|--------------|----------------|
| Mutability | Immutable | Mutable | Mutable |
| Thread Safe | Yes | Yes | No |
| Performance | Slow | Moderate | Fast |
Q.4 (a) Overloaded Constructors of String
- String(): creates empty string
- String(String original)
- String(char[] chars)
- String(byte[] bytes)
Example:
char[] ch = {'H','e','l','l','o'};
String s = new String(ch); // Hello
Q.4 (b) String Modification Methods
- replace(), replaceAll(), toUpperCase(), toLowerCase(), trim()
Example:
String str = " Java ";
System.out.println(str.trim()); // "Java"
Q.4 (c) indexOf(), lastIndexOf()
String str = "programming";
str.indexOf("g"); // 3
str.lastIndexOf("g"); // 10
MODULE 3 - Java Swing
Q.5 (a) Four Commonly Used Buttons in Swing
1. JButton - push button
2. JToggleButton - switch
3. JCheckBox - check option
4. JRadioButton - mutually exclusive options
Example:
JButton b = new JButton("Click Me");
Q.5 (b) MVC Connection in Swing
- Model: data layer
- View: UI representation
- Controller: handles events
Swing uses MVC by decoupling UI and data models.
Q.5 (c) Painting in Swing
Custom painting is done by overriding paintComponent(Graphics g)
Example:
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("Hello", 20, 20);
Q.6 (a) What is Java Swing?
- GUI toolkit for Java
- Lightweight, pluggable look
- Replaces AWT
- Features: MVC, platform-independent, customizable
Q.6 (b) Swing Components:
1. JLabel: Display text or image
2. JTextField: Input field
3. JScrollPane: Adds scrollable view
4. JTable: Tabular data
Example:
JTextField t = new JTextField(20);
JScrollPane pane = new JScrollPane(t);
MODULE 4 - JSP and Servlets
Q.7 (a) JSP Components
- Tags: <% %>, <%= %>, <%! %>
- Variables: page, request, session, application
- Objects: request, response, out, config
Q.7 (b) Servlet Lifecycle
1. init()
2. service()
3. destroy()
Example:
public void init() {}
public void service() {}
public void destroy() {}
Q.7 (c) Session Tracking Techniques
1. Cookies
2. URL Rewriting
3. Hidden Form Fields
4. HttpSession
Example:
HttpSession session = request.getSession();
Q.8 (a) jakarta.servlet Package
- Interfaces: Servlet, ServletConfig, ServletRequest, ServletResponse
- Classes: GenericServlet, HttpServlet
Q.8 (b) Cookie Methods
- setMaxAge(), getName(), getValue()
Handling:
Cookie c = new Cookie("user", "Harsha");
response.addCookie(c);
MODULE 5 - JDBC
Q.9 (a) JDBC & Driver Types
JDBC: API to connect Java to DB.
Types:
1. JDBC-ODBC Bridge
2. Native-API
3. Network Protocol
4. Thin Driver
Q.9 (b) Steps to Connect via ODBC
1. Create DSN in ODBC
2. Load driver: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
3. Get connection: DriverManager.getConnection("jdbc:odbc:dsn")
Q.10 (a) JDBC Overview
Steps:
1. Load driver
2. Create connection
3. Create statement
4. Execute query
5. Process result
6. Close connection
Q.10 (b) JDBC Features
i) Metadata - Info about database
ii) ResultSetMetaData - Info about resultset columns
iii) Data Types - Correspond to SQL types
iv) Exceptions - Handled via SQLException