[go: up one dir, main page]

0% found this document useful (0 votes)
12 views14 pages

Oop Five

The document is a past paper for Object-Oriented Programming in Java, covering various topics such as object-oriented terminologies, Java sockets, inheritance, polymorphism, exception handling, and GUI programming. It includes code examples and explanations for concepts like garbage collection, function overloading, and constructors. The paper consists of multiple questions that test knowledge and application of Java programming principles.

Uploaded by

solomonmukhobe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views14 pages

Oop Five

The document is a past paper for Object-Oriented Programming in Java, covering various topics such as object-oriented terminologies, Java sockets, inheritance, polymorphism, exception handling, and GUI programming. It includes code examples and explanations for concepts like garbage collection, function overloading, and constructors. The paper consists of multiple questions that test knowledge and application of Java programming principles.

Uploaded by

solomonmukhobe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

OOP-JAVA PAST PAPER 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.

b) Java Socket (3 Marks)


Java Socket allows for communication between computers over a network.
java
CopyEdit
import java.io.*;
import java.net.*;

public class SimpleClient {


public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 5000);
OutputStream output = socket.getOutputStream();
output.write("Hello Server".getBytes());
socket.close();
}
}

c) Declare a Constant Using final (2 Marks)


java
CopyEdit
public class ConstantsExample {
public static final double PI = 3.14159;
}

d) Inheritance vs. Polymorphism (4 Marks)


 Inheritance is a mechanism where one class inherits the properties and
behaviors of another.
 Polymorphism allows methods to perform different tasks based on the
object that is invoking them.
Inheritance enables reusability; polymorphism enables flexibility.

e) Namespace in Java (2 Marks)


A namespace in Java is a context for identifiers (class names, interface names,
etc.). It is implemented through packages to avoid name conflicts.

f) Java Code to Create a Frame (3 Marks)


java
CopyEdit
import javax.swing.*;
public class FrameExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Sample Frame");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

g) Four Elements of GUI Programming in Java (4 Marks)


1. JFrame – Top-level window.
2. JButton – Push button.
3. JTextField – Input field.
4. JLabel – Displays a short string/image icon.

h) Function Overloading in Java (4 Marks)


java
CopyEdit
public class OverloadExample {
void show(int a) {
System.out.println("Integer: " + a);
}
void show(String b) {
System.out.println("String: " + b);
}
}

i) Three Benefits of Inheritance (3 Marks)


1. Code Reusability – Avoid rewriting code.
2. Maintainability – Easy to manage code changes.
3. Extensibility – Allows adding new features with minimal changes.

Question Two
a) Exception Handling – Factorial Program (8 Marks)
java
CopyEdit
import java.util.*;

public class FactorialProgram {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
System.out.print("Enter a positive integer: ");
int n = sc.nextInt();
if (n < 0) throw new Exception("Negative number not allowed");
int fact = 1;
for (int i = 1; i <= n; i++)
fact *= i;

System.out.println("Factorial: " + fact);


} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}

b) Advantages of Garbage Collection (4 Marks)


1. Automatic Memory Management – No manual deallocation.
2. Avoids Memory Leaks – Removes unreachable objects.

c) Java GUI Code (8 Marks)


java
CopyEdit
import javax.swing.*;

public class SimpleGUI {


public static void main(String[] args) {
JFrame frame = new JFrame("Sample GUI");
JPanel panel = new JPanel();
panel.add(new JLabel("Name:"));
panel.add(new JTextField(10));

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");
}
}

public class PolymorphismExample {


public static void main(String[] args) {
Animal obj = new Horse(); // Runtime polymorphism
obj.sound();
}
}

b) Compare Two Parameters (7 Marks)


java
CopyEdit
class Compare {
int a, b;

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");
}

public static void main(String[] args) {


Compare obj = new Compare(10, 20);
obj.compare();
}
}

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;
}
}

class Square extends Rectangle {


Square(int side) {
super(side, side);
}
}
b) Parameterized Constructor (6 Marks)
java
CopyEdit
class Shape {
int length, width;

Shape(int l, int w) {
length = l;
width = w;
}
}

c) Sum of Odd Numbers Between 15 and 70 (6 Marks)


java
CopyEdit
public class OddSum {
public static void main(String[] args) {
int sum = 0;
for (int i = 15; i <= 70; i++) {
if (i % 2 != 0)
sum += i;
}
System.out.println("Sum of odd numbers: " + sum);
}
}

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;

// 2. Assign another object


MyClass obj2 = new MyClass();
MyClass obj3 = new MyClass();
obj2 = obj3;

// 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.*;

public class TCPServer {


public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(5000);
Socket client = server.accept();

PrintWriter out = new PrintWriter(client.getOutputStream(), true);


out.println("Current Date & Time: " + new Date());

client.close();
server.close();
}
}

c) Two Types of Constructors Using Class Student (8 Marks)


java
CopyEdit
class Student {
String name;
int age;

// 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.");
}

public static void main(String[] args) {


Student s1 = new Student();
Student s2 = new Student("Jane", 22);

s1.display();
s2.display();
}
}

You might also like