The Kenya National Examinations Council
Diploma in Information Communication Technology
MODULE II – OBJECT ORIENTED PROGRAMMING – July 2023
Time: 3 hours

INSTRUCTIONS TO THE CANDIDATE:

This paper consists of EIGHT questions.
Answer FIVE of the EIGHT questions in the answer booklet provided.
All questions carry equal marks.
Candidates should answer the questions in English.

  1. (a) Define an inline function. (2 marks)

Answer:
An inline function is a function defined with the inline keyword, which suggests to the compiler to insert the function’s code directly into the calling location, rather than performing a typical function call.

(b) With the aid of a syntax code explain how the member functions can be accessed using pointers. (6 marks)

Answer:
class Sample {
public:
void display() {
cout << “Hello World”; } }; Sample* ptr = new Sample(); ptr->display(); // Accessing member function using pointer
(c) Explain how a virtual function is used in object-oriented programming. (2 marks)

Answer:
A virtual function is used to achieve polymorphism in C++. It allows derived classes to override a function in a base class so that the correct function is called at runtime depending on the type of object.

(d) Differentiate between encapsulation and abstraction as used in C++. (4 marks)

Answer:

Encapsulation: Bundles data and methods that operate on the data within one unit, such as a class.
Abstraction: Hides complex implementation details and shows only the necessary features of an object.
(e) With the aid of a syntax code explain inline function. (4 marks)

Answer:
inline int add(int a, int b) {
return a + b;
}
(f) Define the term overloading. (2 marks)

Answer:
Overloading is a feature in C++ that allows the creation of multiple functions with the same name but different parameters or the use of an operator in different ways.

(g) Write a program in C++ that would initialize the values of an array of integers. The program should display the sum, maximum, and minimum of the array. (10 marks)

Answer:

include

using namespace std;

int main() {
int arr[5] = {10, 20, 30, 40, 50};
int sum = 0, max = arr[0], min = arr[0];for (int i = 0; i < 5; i++) { sum += arr[i]; if (arr[i] > max) max = arr[i]; if (arr[i] < min) min = arr[i]; } cout << "Sum: " << sum << endl; cout << "Max: " << max << endl; cout << "Min: " << min << endl; return 0;

}

  1. (a) Outline four characteristics of constructors as used in Object-Oriented Programming. (4 marks)

Answer:

Automatically called when an object is created.
Can be overloaded to accept parameters.
Cannot have a return type.
Has the same name as the class.

With the aid of a syntax code explain the copy constructor. (6 marks)

Answer:
class Sample {
int data;
public:
Sample(int x) { data = x; }
Sample(const Sample &obj) { data = obj.data; }
};
(c) A C++ class is to have three student attributes: Roll number, Name, and Age. Write a C++ program to declare the class. (6 marks)

Answer:
class Student {
public:
int rollNumber;
string name;
int age;
};
(d) The following code was written by a student. Use it to answer the questions that follow. (4 marks)
class Car {
public:
int speed;
string model;
void showDetails() {
cout << “Speed: ” << speed << endl;
cout << “Model: ” << model << endl;
}
};

Car myCar;
myCar.speed = 100;
myCar.model = “Toyota”;
myCar.showDetails();
(i) Identify three bugs in the code. (3 marks)

Answer:

The object myCar should be declared within the main function.
The showDetails() method should be called within a function like main().
The program is missing a main() function.
(ii) Rewrite the code correctly. (4 marks)

Answer:

include

using namespace std;

class Car {
public:
int speed;
string model;
void showDetails() {
cout << “Speed: ” << speed << endl;
cout << “Model: ” << model << endl;
}
};

int main() {
Car myCar;
myCar.speed = 100;
myCar.model = “Toyota”;
myCar.showDetails();return 0;

}

  1. (a) Outline three differences between overloading and overriding as used in Object-Oriented Programming. (6 marks)

Answer:

Overloading allows the same function name to be used with different parameters, while overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.
Overloading is determined at compile-time, while overriding is determined at runtime.
Overloading occurs within the same class, while overriding occurs in a derived class.
(b) Define the term complex class pseudonym. (4 marks)

Answer: A complex class pseudonym is a type alias created using the typedef or using keyword in C++ to refer to complex data types.

(a) Differentiate between base class and derived class as used in C++. (4 marks)

Answer:

Base Class: The class whose properties are inherited by another class.
Derived Class: The class that inherits properties from another class.
(b) Explain two types of class access specifiers in C++. (4 marks)

Answer:

Public: Members are accessible from outside the class.
Private: Members are accessible only within the class.
(c) Explain three types of errors that can be generated in each of the following cases: (6 marks)

Division by zero
Uninitialized variable
Missing semicolon
Answer:

Division by zero: Runtime error.
Uninitialized variable: Logical error.
Missing semicolon: Syntax error.

  1. (a) Differentiate between ifstream and ofstream file operations. (4 marks)

Answer:

ifstream: Used for reading data from a file.
ofstream: Used for writing data to a file.
(b) Write a program in C++ to prompt a user to enter a figure (either Circle or Rectangle). The program then calculates the area of the figure entered using a derived function and displays the result. (10 marks)

Answer:

include

include

using namespace std;

class Figure {
public:
virtual double area() = 0; // Pure virtual function
};

class Circle : public Figure {
double radius;
public:
Circle(double r) : radius(r) {}
double area() {
return M_PI * radius * radius;
}
};

class Rectangle : public Figure {
double length, width;
public:
Rectangle(double l, double w) : length(l), width(w) {}
double area() {
return length * width;
}
};

int main() {
int choice;
cout << “Enter 1 for Circle or 2 for Rectangle: “; cin >> choice;
Figure *fig;
if (choice == 1) {
double r;
cout << “Enter radius: “; cin >> r;
fig = new Circle(r);
} else {
double l, w;
cout << “Enter length and width: “; cin >> l >> w;
fig = new Rectangle(l, w);
}
cout << “Area: ” << fig->area() << endl;
delete fig;return 0;

}

  1. (a) Define each of the following terms as used in Object-Oriented Programming: (4 marks)

Virtual function
Abstract class
Inheritance
Polymorphism
Answer:

Virtual function: A function in a base class that is overridden in a derived class and is determined at runtime.
Abstract class: A class that cannot be instantiated and contains at least one pure virtual function.
Inheritance: A mechanism by which one class acquires the properties of another class.
Polymorphism: The ability of a function or method to work in different ways depending on the object it is operating on.
(b) Explain the term object slicing as applied in inheritance. (2 marks)

Answer: Object slicing occurs when an object of a derived class is assigned to an object of the base class, resulting in the loss of derived class-specific data.

(c) Table 1 shows criteria used to award bonuses to employees. Use it to answer the questions that follow. (6 marks)

Criteria:
Credit score 5000: 100
Credit score 10000: 200
Credit score 15000: 300
Write a program in C++ that would prompt a user to enter the credit score of an employee. The program should display the bonus. Use the criteria shown in Table 1. (7 marks)

Answer:

include

using namespace std;

int main() {
int creditScore, bonus;
cout << “Enter credit score: “; cin >> creditScore;if (creditScore == 5000) bonus = 100; else if (creditScore == 10000) bonus = 200; else if (creditScore == 15000) bonus = 300; else bonus = 0; cout << "Bonus: " << bonus << endl; return 0;

}

  1. (a) Explain two types of exceptions in Object-Oriented Programming. (6 marks)

Answer:

Standard exceptions: Predefined exceptions provided by the language, such as divide-by-zero or out-of-bounds exceptions.
User-defined exceptions: Exceptions created by programmers for specific error handling in applications.
(b) Describe how namespaces are used in C++ programming language. (2 marks)

Answer: Namespaces are used to organize code into logical groups and prevent name collisions that can occur especially when your code base includes multiple libraries.

(c) With the aid of a syntax code, explain the use of break and continue statements. (4 marks)

Answer:


for (int i = 0; i < 10; i++) {
if (i == 5) continue; // Skips the rest of the code for this iteration
if (i == 8) break; // Exits the loop
cout << i << ” “;
}.

Explain two types of exceptions in Object-Oriented Programming. (6 marks)

Answer:

Standard exceptions: Predefined exceptions provided by the language, such as divide-by-zero or out-of-bounds exceptions.
User-defined exceptions: Exceptions created by programmers for specific error handling in applications.
(b) Describe how namespaces are used in C++ programming language. (2 marks)

Answer: Namespaces are used to organize code into logical groups and prevent name collisions that can occur especially when your code base includes multiple libraries.

(c) With the aid of a syntax code, explain the use of break and continue statements. (4 marks)

Answer:
for (int i = 0; i < 10; i++) {
if (i == 5) continue; // Skips the rest of the code for this iteration
if (i == 8) break; // Exits the loop
cout << i << ” “;
}
(d) Write a program in C++ to accept a string through the keyboard and store it in a file. (3 marks)

Answer:

include

include

using namespace std;

int main() {
string input;
cout << “Enter a string: “;
getline(cin, input);ofstream file("output.txt"); file << input; file.close(); cout << "String saved to file." << endl; return 0;

}
THIS IS THE LAST PRINTED PAGE.

Leave a Reply