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.
- 110 marksIntroduction of RMI, Architecture of RMI, HideAnswer
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...
- 210 marksDDL and DML Operations using Java, PrepareHideAnswer
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...
- 310 marksWorking with FilesHideAnswer
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...
- 45 marksNumericalArraysHideAnswer
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]
isAllPossibilitiesMethodGiven data
- Input: a non-empty integer array
Aof lengthn. - Condition:
Amust contain all integers from0ton-1inclusive. - Output: return
1if the condition holds, otherwise0.
(No specific test arrays are mandated by the question; behaviour must be general.)
Concept
An array of length
nqualifies if and only if it is a permutation of ${0, 1, 2, \dots, n-1}$. Since the array has exactlynslots and must holdndistinct values all in range $[0, n-1]$, it suffices to check:- every element lies in $[0, n-1]$, and
- no value repeats.
If both hold across
nelements, each of thendistinct 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
Array n Result Reason {0,1,2,3}4 1Permutation of 0..3 {3,0,2,1}4 1Same set, reordered {1,2,3,4}4 04 >= nout of range{0,0,1,2}4 0Duplicate 0detected{-1,0,1,2}4 0-1 < 0out of rangeComplexity
- Time: $O(n)$ - single pass.
- Space: $O(n)$ - visited array.
- Input: a non-empty integer array
- 55 marksEvent Handling Concept, Listener InterfaceHideAnswer
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:
Component Role Event Source The UI component that generates the event (e.g., Button, TextField) Event Object Encapsulates information about the event (e.g., ActionEvent, KeyEvent) Event Listener The 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:
- Implement the appropriate listener interface (e.g.,
ActionListener,KeyListener) - Register the listener with the source using
addXxxListener(this) - 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,
KeyListenerhas 3 methods:keyPressed(KeyEvent e)keyReleased(KeyEvent e)keyTyped(KeyEvent e)
If we implement
KeyListenerdirectly, we are forced to provide the body of all 3 methods, even if we only care aboutkeyPressed. 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 Class Corresponding Listener Interface KeyAdapterKeyListenerMouseAdapterMouseListenerWindowAdapterWindowListenerFocusAdapterFocusListenerSummary:
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.
- Implement the appropriate listener interface (e.g.,
- 65 marksLayout ManagementHideAnswer
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 ...
- 75 marksHandling ExceptionsHideAnswer
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...
- 85 marksWeb Container, Introduction to Servlets, LHideAnswer
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...
- 95 marksPackagesHideAnswer
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: ...
- 105 marksInheritanceHideAnswer
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...
- 115 marksServlet vs JSP, JSP Access Model, JSP SyntHideAnswer
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...
- 125 marksConcurrencyHideAnswer
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...
- 135 marksIntroduction to Java Web FrameworksHideAnswer
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...