2077

CSC419 · TU past paper

Advanced Java Programming 2077 question paper

The complete TU 2077 exam paper for Advanced Java Programming (CSC419), all 13 questions with solved model answers written to the mark scheme.

Tap a question to open its answer.

  1. 110 marksIntroduction of RMI, Architecture of RMI, Answer

    What is the significance of stub and skeleton in RMI? Create a RMI application such that a client sends an Integer number to the server and the server return the factorial value of that integer. Give a clear specification for every step.[10]

    --- RMI (Remote Method Invocation) is a Java API that provides a mechanism to create distributed applications. It allows an object in one JVM to invoke methods on an object running in another JVM. The two key objects that enable this rem...

  2. 210 marksDDL and DML Operations using Java, PrepareAnswer

    You are hired by a reputed software company which is going to design an application for 'Movie Rental System'. Your responsibility is to design a schema named MRS and create a table named Movie(id, Title, Genre, Language, Length). Write a program to design a GUI form to take input for this table and insert the data into table after clicking the OK button.[10]

    --- Note: Java AWT (Abstract Windowing Toolkit) is used to develop GUI/window-based applications. JDBC interfaces such as Connection, Statement, and ResultSet are used to communicate with the database. A Type 4 (Pure Java) JDBC driver is...

  3. 310 marksWorking with FilesAnswer

    Describe the responsibility of Serializable interface. Write a program to read an input string from the user and write the vowels of that string in VOWEL.TXT and consonants in CONSONANT.TXT.[10]

    The Serializable interface in Java is found in the java.io package. It is a marker interface, meaning it has no methods or fields. Its sole purpose is to mark a class so that the Java runtime knows the objects of that class can be serial...

  4. 45 marksNumericalArraysAnswer

    A non-empty array A of length n is called an array of all possibilities if it contains all numbers between 0 and A.length-1 inclusive. Write a method named isAllPossibilities that accepts an integer array and returns 1 if the array is an array of all possibilities, otherwise it returns 0. [5]

    isAllPossibilities Method

    Given data

    • Input: a non-empty integer array A of length n.
    • Condition: A must contain all integers from 0 to n-1 inclusive.
    • Output: return 1 if the condition holds, otherwise 0.

    (No specific test arrays are mandated by the question; behaviour must be general.)

    Concept

    An array of length n qualifies if and only if it is a permutation of ${0, 1, 2, \dots, n-1}$. Since the array has exactly n slots and must hold n distinct values all in range $[0, n-1]$, it suffices to check:

    1. every element lies in $[0, n-1]$, and
    2. no value repeats.

    If both hold across n elements, each of the n distinct values must appear exactly once (pigeonhole principle).

    Java Code

    public static int isAllPossibilities(int[] A) {
        int n = A.length;
    
        boolean[] visited = new boolean[n];   // track which numbers seen
    
        for (int i = 0; i < n; i++) {
            // out of range check
            if (A[i] < 0 || A[i] >= n) {
                return 0;
            }
            // duplicate check
            if (visited[A[i]]) {
                return 0;
            }
            visited[A[i]] = true;
        }
    
        return 1;   // all values 0..n-1 present exactly once
    }
    

    Dry Run

    ArraynResultReason
    {0,1,2,3}41Permutation of 0..3
    {3,0,2,1}41Same set, reordered
    {1,2,3,4}404 >= n out of range
    {0,0,1,2}40Duplicate 0 detected
    {-1,0,1,2}40-1 < 0 out of range

    Complexity

    • Time: $O(n)$ - single pass.
    • Space: $O(n)$ - visited array.
  5. 55 marksEvent Handling Concept, Listener InterfaceAnswer

    Define event delegation model. Why do we need adapter class in event handling? [5]

    Event Delegation Model and Adapter Classes in Java

    Event Delegation Model (2 marks)

    The Event Delegation Model is the standard event handling mechanism used in Java AWT and Swing. In this model, the task of handling an event is delegated from the source (component that generates the event) to a separate object called the listener (or handler).

    Key Components:

    ComponentRole
    Event SourceThe UI component that generates the event (e.g., Button, TextField)
    Event ObjectEncapsulates information about the event (e.g., ActionEvent, KeyEvent)
    Event ListenerThe object that receives and handles the event

    How it Works:

    User Action --> Event Source --> Event Object --> Event Listener --> Handler Method
    (e.g., click)  (Button)        (ActionEvent)    (ActionListener)  (actionPerformed())
    

    Steps in Event Delegation:

    1. Implement the appropriate listener interface (e.g., ActionListener, KeyListener)
    2. Register the listener with the source using addXxxListener(this)
    3. Override the required handler method(s)

    Example (from notes):

    class AEvent extends Frame implements ActionListener {
        Button b = new Button("click me");
        b.addActionListener(this);  // Step 2: Register listener
    
        public void actionPerformed(ActionEvent e) {  // Step 3: Handle event
            tf.setText("Welcome");
        }
    }
    

    Why We Need Adapter Classes (3 marks)

    The Problem:

    Some listener interfaces in Java contain multiple abstract methods that must ALL be overridden when implementing the interface, even if we only need one or two of them.

    For example, KeyListener has 3 methods:

    • keyPressed(KeyEvent e)
    • keyReleased(KeyEvent e)
    • keyTyped(KeyEvent e)

    If we implement KeyListener directly, we are forced to provide the body of all 3 methods, even if we only care about keyPressed. This leads to unnecessary, cluttered code:

    // Without Adapter - must override ALL methods (messy)
    class MyClass extends Frame implements KeyListener {
        public void keyPressed(KeyEvent e)  { /* actual code */ }
        public void keyReleased(KeyEvent e) { /* empty - not needed */ }
        public void keyTyped(KeyEvent e)    { /* empty - not needed */ }
    }
    

    The Solution - Adapter Classes:

    An Adapter class is a pre-built class that implements a listener interface with empty method bodies. We simply extend the adapter class and override only the methods we need.

    // With Adapter - override ONLY what is needed (clean)
    class KeyChecker extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            // only this method is needed
        }
        // No need to write keyReleased() or keyTyped()
    }
    

    MouseAdapter Example (from notes):

    public class MouseAdapterExample extends MouseAdapter {
        // Only mouseClicked() is overridden - no other methods needed
        public void mouseClicked(MouseEvent e) {
            Graphics g = f.getGraphics();
            g.setColor(Color.BLUE);
            g.fillOval(e.getX(), e.getY(), 30, 30);
        }
    }
    

    Common Adapter Classes:

    Adapter ClassCorresponding Listener Interface
    KeyAdapterKeyListener
    MouseAdapterMouseListener
    WindowAdapterWindowListener
    FocusAdapterFocusListener

    Summary:

    Adapter classes reduce code complexity by providing default (empty) implementations of all methods in a listener interface, allowing the programmer to override only the required methods.

  6. 65 marksLayout ManagementAnswer

    What is the task of Layout manager? Describe about default layout manager. [5]

    A Layout Manager is responsible for controlling the size and position (arrangement) of components placed inside a container. The main tasks of a layout manager are: - Positioning components within a container automatically - Determining ...

  7. 75 marksHandling ExceptionsAnswer

    When does the finally block is mandatory in while handling exception? Describe with a suitable scenario. [5]

    The finally block in Java is a block that always executes regardless of whether an exception occurs or not. It is used to place important cleanup code such as closing connections, releasing resources, or closing files. --- The finally bl...

  8. 85 marksWeb Container, Introduction to Servlets, LAnswer

    Explain the life cycle of a servlet. [5]

    A servlet is a small Java program that executes on the server side of a web connection and dynamically extends the functionality of a web server. All servlets must implement the Servlet interface, which defines the life-cycle methods. Th...

  9. 95 marksPackagesAnswer

    What is the task of manifest file? Write the procedure to create it. [5]

    A manifest file is a special metadata file included inside a JAR (Java Archive) file. It is named MANIFEST.MF and is located in the META-INF/ directory within the JAR. The main tasks of the manifest file are: - Specifies the Main Class: ...

  10. 105 marksInheritanceAnswer

    Why multiple inheritance is not allowed in Java using classes? Give an example. [5]

    Java does not support multiple inheritance with classes primarily to avoid the Diamond Problem (also called the "Deadly Diamond of Death"). This is an ambiguity problem that arises when a class inherits from two or more classes that have...

  11. 115 marksServlet vs JSP, JSP Access Model, JSP SyntAnswer

    How forms can be created and processed using JSP? Make it clear with your own assumptions. [5]

    JSP (JavaServer Pages) allows embedding Java code inside HTML pages. Forms can be created using standard HTML <form tags within a JSP page and processed by reading the submitted data using JSP's built-in request object. --- Object Purpos...

  12. 125 marksConcurrencyAnswer

    Why synchronization is essential in multithreading? Describe. [5]

    In a multithreaded environment, multiple threads share the same process resources (memory, data, objects). When two or more threads try to access and modify a shared object or variable simultaneously, it leads to data inconsistency and r...

  13. 135 marksIntroduction to Java Web FrameworksAnswer

    Write short notes on: a. JAVA beans and JAR file b. MVC design pattern [2.5+2.5]

    --- A JavaBean is a reusable software component written in Java that follows specific conventions. It is essentially a Java class that encapsulates multiple objects into a single object (the bean). Key Conventions / Properties of a JavaB...