Oop Five
Oop Five
Question One
a) Description of Object-Oriented Terminologies (3 Marks)
i. Packages
Packages in Java are namespaces that organize classes and interfaces. They help
avoid class name conflicts and control access. Example: java.util, java.io.
ii. Interfaces
An interface in Java is a reference type, similar to a class, that can contain abstract
methods. It is used to achieve abstraction and multiple inheritance.
iii. Garbage Collection
Garbage Collection in Java is the automatic process of reclaiming memory by
deleting objects that are no longer reachable in the program.
Question Two
a) Exception Handling – Factorial Program (8 Marks)
java
CopyEdit
import java.util.*;
panel.add(new JLabel("Age:"));
panel.add(new JTextField(3));
panel.add(new JButton("Submit"));
frame.add(panel);
frame.setSize(300, 150);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Question Three
a) Runtime Polymorphism (7 Marks)
java
CopyEdit
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Horse extends Animal {
void sound() {
System.out.println("Horse neighs");
}
}
Compare(int x, int y) {
a = x;
b = y;
}
void compare() {
if (a == b)
System.out.println("They are equal");
else
System.out.println("They are not equal");
}
c) Terminologies (6 Marks)
i. Function Definition vs. Declaration
Declaration: Announces function signature.
Definition: Provides the actual body.
ii. Class vs. Object
Class: Blueprint.
Object: Instance of a class.
iii. Stubs vs. Skeletons
Stub: Client-side proxy.
Skeleton: Server-side proxy (used in RMI).
Question Four
a) Inheritance: Area of Shapes (8 Marks)
java
CopyEdit
class Rectangle {
int length, width;
Rectangle(int l, int w) {
length = l;
width = w;
}
int area() {
return length * width;
}
}
Shape(int l, int w) {
length = l;
width = w;
}
}
Question Five
a) Three Ways to Unreference an Object (6 Marks)
java
CopyEdit
public class Unreference {
public static void main(String[] args) {
// 1. Set object to null
MyClass obj1 = new MyClass();
obj1 = null;
// 3. Anonymous object
new MyClass();
}
}
class MyClass { }
b) TCP Server Sending Date and Time (6 Marks)
java
CopyEdit
import java.io.*;
import java.net.*;
import java.util.*;
client.close();
server.close();
}
}
// Default constructor
Student() {
name = "Unknown";
age = 0;
}
// Parameterized constructor
Student(String n, int a) {
name = n;
age = a;
}
void display() {
System.out.println(name + " is " + age + " years old.");
}
s1.display();
s2.display();
}
}