2080

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.

Past Papers208220812080

Tap a question to open its answer.

  1. 110 marksInterface design and implementationAnswer

    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]

    Interfaces are needed for the following reasons: 1. Abstraction: Interfaces define a contract that specifies what methods a class must implement, without specifying how they should be implemented. 2. Polymorphism: Interfaces allow object...

  2. 210 marksText boxes and text fieldsAnswer

    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]

    Feature Description ---------------------- JFrame Main window container JLabel Display text labels for input fields JTextField Input fields for numbers; result field is non-editable JButton Four operation buttons ActionListener Interface...

  3. 310 marksJSP tags and directivesAnswer

    What are the various tags used in writing JSP programs? How HTML forms can be processed using JSP? Explain with example.[10]

    JSP provides several types of tags for embedding Java code and dynamic content in web pages: Used to declare variables and methods that become part of the servlet class. Contains Java code that executes every time the page is requested. ...

  4. 45 marksConstructor chainingAnswer

    Describe the chain of constructor with example. [5]

    Constructor chaining is a technique in object-oriented programming where one constructor calls another constructor of the same class or parent class. This allows code reuse and initialization of objects in a flexible manner. One construc...

  5. 55 marksThread class and Runnable interfaceAnswer

    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 execution
    

    Key Points Explained

    1. Extend Thread Class: MyThread extends the Thread class
    2. Override run() Method: Contains the code to be executed by the thread
    3. Create Thread Objects: Three separate instances (thread1, thread2, thread3) are created
    4. Call start() Method: Invokes the thread's execution; never call run() directly
    5. Concurrent Execution: All three threads run concurrently, producing interleaved output
    6. Thread.sleep(): Pauses thread execution for specified milliseconds
  6. 65 marksLayout managersAnswer

    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:

    1. Components are placed horizontally from left to right
    2. When a row is full, components wrap to the next row
    3. Each component takes its preferred size
    4. Components are centered by default within the container
    5. 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.

  7. 75 marksException types and hierarchyAnswer

    Describe about exception handlings with example. [5]

    Exception handling is a mechanism in programming that allows a program to detect, manage, and respond to runtime errors (exceptions) gracefully without terminating abruptly. It enables the program to continue execution or terminate in a ...

  8. 85 marksDifferences between Statement and PreparedAnswer

    How prepared statements differs from statements? Explain use of prepared statements with example. [5]

    Aspect Statement Prepared Statement -------------------------------------- Compilation SQL query compiled every time it executes SQL query pre-compiled once, reused multiple times Performance Slower for repeated queries Faster for repeat...

  9. 95 marksSteps of writing JavaFX programsAnswer

    Discuss the steps of writing JavaFX programs. [5]

    Since reference notes are not provided, I'll outline the standard steps for writing JavaFX programs based on the BSc CSIT curriculum: Import the necessary JavaFX libraries: Create a class that extends javafx.application.Application: Impl...

  10. 105 marksTCP socket programming stepsAnswer

    Write a socket program using TCP where client sends an integer to server and server response whether it is positive or negative. [5]

    A TCP client-server program where: - Client: Connects to server and sends an integer - Server: Receives integer, determines if positive/negative, sends response back --- --- --- 1. Compile both programs: 2. Run server (Terminal 1): 3. Ru...

  11. 115 marksRMI architecture and componentsAnswer

    How distributed programs can be written in Java? Explain RMI architecture. [5]

    Distributed programs in Java can be written using several approaches: 1. Socket Programming - Using TCP/IP sockets for low-level network communication between Java applications 2. RMI (Remote Method Invocation) - Allows Java objects to i...

  12. 125 marksFinal keyword and immutabilityAnswer

    Write short notes on a. Final keyword b. Object class [5]

    The final keyword in Java is a modifier used to restrict modification. It can be applied in three contexts: 1. Final Variables: - Once assigned, the value cannot be changed - Must be initialized when declared or in the constructor - Exam...