Question 1
a. Three characteristics of a structured programming language:
1. It divides a program into small modules or functions.
2. It follows a top-down approach in program design.
3. It uses control structures such as sequence, selection, and iteration.
b. Example of a non-structured program performing 10 arithmetic calculations:
START
READ A, B
C=A+B
D=A-B
E=A*B
F=A/B
G = (A + B) * 2
H = (A - B) + 5
I = (A * B) - 10
J = (A / B) + 7
K=A+B+C+D
PRINT C, D, E, F, G, H, I, J, K
STOP
c. Structured program example in Python:
def main():
A = int(input("Enter first number: "))
B = int(input("Enter second number: "))
C=A+B
D=A-B
E=A*B
F=A/B
G = (A + B) * 2
H = (A - B) + 5
I = (A * B) - 10
J = (A / B) + 7
K=A+B+C+D
print(C, D, E, F, G, H, I, J, K)
main()
d. Demonstration of constructor in Java:
class Student {
String name;
int age;
Student(String n, int a) {
name = n;
age = a;
}
void display() {
System.out.println(name + " is " + age + " years old.");
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("John", 20);
Student s2 = new Student("Mary", 22);
s1.display();
s2.display();
}
}
Question 2
a. A constructor is a special type of method that is automatically invoked when an object is
created.
b. Five differences between methods and constructors:
1. Constructor has the same name as the class, while methods have user-defined names.
2. Constructor is automatically invoked when an object is created, while methods must be
explicitly invoked.
3. Constructor has no return type, while methods may or may not return a value.
4. Constructor is used for initializing objects, while methods are used for performing
operations.
5. If no constructor is defined, the compiler provides a default one, while methods must
always be defined.
c. Example of inheritance in Java:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
}
}
Question 3
a. An instance of a class is an object created from that class. It represents a single and
unique copy of the class.
b. Five ways objects interact with themselves:
1. Message passing
2. Method invocation
3. Data sharing
4. Inheritance
5. Polymorphism
c. Java example of polymorphism via overloading and overriding:
class Shape {
void draw() {
System.out.println("Drawing a shape.");
}
void draw(String type) {
System.out.println("Drawing a " + type);
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle.");
}
}
public class Main {
public static void main(String[] args) {
Shape s1 = new Shape();
s1.draw();
s1.draw("Square");
Shape s2 = new Circle();
s2.draw();
}
}
Question 4
a. Basic rules of OOP according to Alan Kay:
1. Everything is an object.
2. Objects communicate by sending and receiving messages.
3. Objects have their own memory.
4. Every object is an instance of a class.
5. The class defines the behavior of its instances.
6. Classes are organized into hierarchies.
b. Java program demonstrating multilevel inheritance:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Mammal extends Animal {
void walk() {
System.out.println("Mammals can walk.");
}
}
class Dog extends Mammal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.walk();
d.bark();
}
}
Question 5
a. Data abstraction is the process of hiding the implementation details of a class and
exposing only the essential features.
b. An array is a collection of elements of the same type stored in contiguous memory
locations.
c. Example Java programs:
Data Abstraction:
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle.");
}
}
public class Main {
public static void main(String[] args) {
Shape s = new Circle();
s.draw();
}
}
Array:
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
Question 6
a. A data structure is a way of organizing and storing data so that it can be accessed and
worked with efficiently.
b. Four types of data structures in OOP:
1. Array: A collection of elements of the same type, stored in contiguous memory.
2. Stack: A linear data structure that follows Last In First Out (LIFO).
3. Queue: A linear data structure that follows First In First Out (FIFO).
4. Linked List: A linear data structure consisting of nodes, each containing data and a
reference to the next node.
c. Java program to demonstrate a simple stack:
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack stack = new Stack<>();
stack.push(10);
stack.push(20);
stack.push(30);
System.out.println("Popped: " + stack.pop());
System.out.println("Stack: " + stack);
}
}