BIT401 · TU past paper
Advanced Java Programming 2080 question paper
The complete TU 2080 exam paper for Advanced Java Programming (BIT401), all 12 questions with solved model answers written to the mark scheme.
Tap a question to open its answer.
- Q1.10 marksInterface design and implementationHideAnswer
Why do we need interface? Define an interface named shape with area ( ) method. Create a class named Rectangle with length and breadth circle with radius that implements shape interface. Now create the object of both classes and display their area.[10]
- Q2.10 marksText boxes and text fieldsHideAnswer
Write a Java program to create a UI with three labels and three text boxes for First Number, Second Number and Result. Also put Four buttons (Add, Subtract, Multiply, and Divide) in the form and handle the event accordingly.[10]
- Q3.10 marksJSP tags and directivesHideAnswer
What are the various tags used in writing JSP programs? How HTML forms can be processed using JSP? Explain with example.[10]
- Q4.5 marksConstructor chainingHideAnswer
Describe the chain of constructor with example. [5]
- Q5.5 marksThread class and Runnable interfaceHideAnswer
Write a Java program to create a multi-threaded programs by extending Thread class. Create at least three threads. [5]
Model Answer: Multi-threaded Program by Extending Thread Class
Program Code
// Define a custom thread class by extending Thread class MyThread extends Thread { private String threadName; // Constructor public MyThread(String name) { this.threadName = name; } // Override run() method @Override public void run() { for (int i = 1; i <= 5; i++) { System.out.println(threadName + " is running - Count: " + i); try { // Sleep for 1 second Thread.sleep(1000); } catch (InterruptedException e) { System.out.println(e); } } System.out.println(threadName + " has finished execution"); } } // Main class to create and start threads public class MultiThreadedProgram { public static void main(String[] args) { // Create three thread objects MyThread thread1 = new MyThread("Thread-1"); MyThread thread2 = new MyThread("Thread-2"); MyThread thread3 = new MyThread("Thread-3"); // Start all three threads thread1.start(); thread2.start(); thread3.start(); System.out.println("Main thread continues..."); } }Output (Sample)
Main thread continues... Thread-1 is running - Count: 1 Thread-2 is running - Count: 1 Thread-3 is running - Count: 1 Thread-1 is running - Count: 2 Thread-2 is running - Count: 2 Thread-3 is running - Count: 2 ... Thread-1 has finished execution Thread-2 has finished execution Thread-3 has finished executionKey Points Explained
- Extend Thread Class:
MyThreadextends theThreadclass - Override run() Method: Contains the code to be executed by the thread
- Create Thread Objects: Three separate instances (thread1, thread2, thread3) are created
- Call start() Method: Invokes the thread's execution; never call run() directly
- Concurrent Execution: All three threads run concurrently, producing interleaved output
- Thread.sleep(): Pauses thread execution for specified milliseconds
- Extend Thread Class:
- Q6.5 marksLayout managersHideAnswer
What is layout manager? Explain working of flow layout with example. [5]
Layout Manager - Model Answer
What is Layout Manager?
A layout manager is a component in Java GUI programming that automatically arranges and positions components (like buttons, labels, text fields) within a container. It controls the size and position of components based on predefined rules, eliminating the need for manual positioning.
Key purposes:
- Automatically manages component placement
- Handles resizing when window is resized
- Ensures consistent appearance across different screen resolutions
- Reduces manual coordinate calculations
Flow Layout - Working and Explanation
How Flow Layout Works:
Flow Layout is the default layout manager for JPanel. It arranges components in a left-to-right, top-to-bottom manner, similar to text flow in a document.
Characteristics:
- Components are placed horizontally from left to right
- When a row is full, components wrap to the next row
- Each component takes its preferred size
- Components are centered by default within the container
- Respects gaps between components (horizontal and vertical)
Example:
import javax.swing.*; public class FlowLayoutExample extends JFrame { public FlowLayoutExample() { setTitle("Flow Layout Demo"); setSize(400, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create panel with FlowLayout JPanel panel = new JPanel(new FlowLayout()); // Add components panel.add(new JButton("Button 1")); panel.add(new JButton("Button 2")); panel.add(new JButton("Button 3")); panel.add(new JButton("Button 4")); panel.add(new JButton("Button 5")); add(panel); setVisible(true); } public static void main(String[] args) { new FlowLayoutExample(); } }Output behavior:
- Buttons 1, 2, 3, 4 appear in first row (left to right)
- Button 5 wraps to second row
- When window is resized, components reflow automatically
Note: Standard Java Swing documentation used for layout manager concepts as reference notes were unavailable.
- Q7.5 marksException types and hierarchyHideAnswer
Describe about exception handlings with example. [5]
- Q8.5 marksDifferences between Statement and PreparedHideAnswer
How prepared statements differs from statements? Explain use of prepared statements with example. [5]
- Q9.5 marksSteps of writing JavaFX programsHideAnswer
Discuss the steps of writing JavaFX programs. [5]
- Q10.5 marksTCP socket programming stepsHideAnswer
Write a socket program using TCP where client sends an integer to server and server response whether it is positive or negative. [5]
- Q11.5 marksRMI architecture and componentsHideAnswer
How distributed programs can be written in Java? Explain RMI architecture. [5]
- Q12.5 marksFinal keyword and immutabilityHideAnswer
Write short notes on a. Final keyword b. Object class [5]