BIT2108  OBJECT ORIENTED PROGRAMMING KCA Past Paper

UNIVERSITY EXAMINATIONS: 2016/2017
EXAMINATION FOR THE DEGREE OF BACHELOR OF SCIENCE
IN INFORMATION TECHNOLOGY
BIT2108 OBJECT ORIENTED PROGRAMMING
FULL TIME & PART TIME
DATE: DECEMBER, 2016 TIME: 2 HOURS
INSTRUCTIONS: Answer Question One & ANY OTHER TWO
questions.

QUESTION ONE – COMPULSORY (30 MARKS)
a) Preprocessor are commands that are executed before any other statement in your
program. Describe any two such preprocessors used in C++ programming
[4 Marks]
a) What will be the output of the following program
#include <iostream>
using namespace std;
int main()
{
int x = 0;

while ( x < 10 ) {
cout<< x <<endl;
x++;
}
cin.get();
}
[3 Marks]
b) Rewrite the above program using the for control construct
[3 Marks]
c) Describe the bolded sections of the program below
#include <iostream>
using namespace std;
int mult ( int x, int y );
int main()
{
int x;
int y;

cout<<“Please input two numbers to be multiplied: “;
cin>> x >> y;
cin.ignore();
cout<<“The product of your two numbers is “<< mult ( x, y ) <<“\n”;
cin.get();
}
int mult ( int x, int y )
{
return x * y;
}
[6 Marks]
d) Give the functional difference between if and switch control construct
[2 Marks]
e) Define what a pointer is and describe two operators associated with it operation
[3 Marks]
f) Create a structure person with members’ id, age, height and weight. Write a
program that shows how this structure is implemented in main. In main, the
programs also compute the Body Mass Index (BMI) given as weight (kg)/height2
(m2
). The data is input by the user through program prompting and re-echo the
same on the screen in an appropriate format
[8 Marks]
QUESTION TWO
a) With an example demonstrate how the new and delete keywords are used with
pointers in dynamic memory allocation
[4 Marks]
b) Differentiate between the following
i). Object oriented and object based programming languages
ii). Class and Object
iii). Overriding and overloading
[6 Marks]
c) Give FOUR characteristics of object oriented programming languages
[4 Marks]
d) What is the difference between Procedural and OOPs?
[3 Marks]
e) What is the difference between constructor and method?
[3 Marks]
QUESTION THREE
a) Predict the output of the following program
#include <iostream>
using namespace std;
class vector
{
public:
double x;
double y;
double surface ()
{
double s;
s = x * y;
if (s < 0) s = -s;
return s;
}
};
int main ()
{
vector a;
a.x = 3;
a.y = 4;
cout << “The surface of a: ” << a.surface() << endl;
return 0;
}
[4 Marks]
b) Describe the following terms as used in OOP
i). Static memory allocation
ii). Dynamic memory allocation
iii). Constructors
iv). Destructors
[8 Marks]
c) Write a program that displays an invoice of several items. It should contain the
item name, quantity, price, and total cost on each line for the quantity and item
cost. It also contains methods to get and set item name, quantity, and price.
Objects are created in main and invoices printed.
[8 Marks]
QUESTION FOUR
a) Write a program that calculates and displays the weekly salary for an employee
who earns $25 an hour, works 40 regular hours, 13 overtime hours, and earns
time and one-half (wage * 1.5) for overtime hours worked. Create a separate
method to do the calculation and return the result to be displayed. Your program
contain a class that holds the employee details indicated as well as the method to
do the computation
[8 Marks]
b) Define the following
i). Method
ii). Inheritance
iii). Class
[6 Marks]
c) Create a class named Box that includes integer data fields for length, width, and
height. Create three constructors that require one, two, and three arguments,
respectively. When one argument is used, assign it to length, assign zeros to
height and width, and print “Line created”. When two arguments are used, assign
them to length and width, assign zero to height, and print “Rectangle created”.
When three arguments are used, assign them to the three variables and print “Box
created”.
[6 Marks]
QUESTION FIVE
a) Define what a friend function is and predict the output of the following program
#include <iostream>
using namespace std;
class CRectangle {
int width, height;
public:
void set_values (int, int);
int area () {return (width * height);}
friend CRectangle duplicate (CRectangle);
};
void CRectangle::set_values (int a, int b) {
width = a;
height = b;
}
CRectangle duplicate (CRectangle rectparam)
{
CRectangle rectres;
rectres.width = rectparam.width*2;
rectres.height = rectparam.height*2;
return (rectres);
}
int main () {
CRectangle rect, rectb;
rect.set_values (2,3);
rectb = duplicate (rect);
cout << rectb.area();
return 0;
}
[6 Marks]
b) Give the following class CPolygon, give a definition of the class CRectangle that
inherits CPolygon with a method that compute the area and perimeter of a
Rectangle
Design also the class CTriangle that inherits from CPolygon with additional
method that compute the area of a triangle.
Create the main program that demonstrate how the two derived classes are
invoked
#include <iostream>
using namespace std;
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b;}
};
[9 Marks]
c) When implementing inheritance, some elements from the base class are not
passed to the derived class. List those methods that are not inherited
[3 Marks]
d) Define1 what you understand by the following
i). Virtual function
ii). Multiple inheritance
[2 Marks]

(Visited 74 times, 1 visits today)
Share this:

Written by