UNIVERSITY EXAMINATIONS: 2017/2018
EXAMINATION FOR THE DEGREE OF BACHELOR OF SCIENCE IN
INFORMATION TECHNOLOGY/ BACHELOR OF BUSINESS IN
INFORMATION TECHNOLOGY
BIT2108 OBJECT ORIENTED PROGRAMMING
FULL TIME/PART TIME/ DISTANCE LEARNING
DATE: APRIL, 2018 TIME: 2 HOURS
INSTRUCTIONS: Answer Question One & ANY OTHER TWO questions.
QUESTION ONE
i. Give the correct answer in the following cases (8 Marks)
a) Which of these statements about constructors is false?
A. A constructor has no return type
B. Its name must be the same as the class in which it is defined
C. There must be exactly one constructor defined for a class
D. Constructors are almost always declared as public
E. They can appear anywhere in the class where it is legal to declare a method
b) When does CPP know an object is no longer needed? And what happens to an unneeded
object’s storage?
A. The programmer tells CPP when an object is no longer needed by calling destructor ()
on it; the object’s memory is released back to the memory pool.
B. If there are no references to the object, CPP knows the object is no longer needed and
automatically returns its memory to the memory pool.
C. If there are no references to the object, CPP Marks it as no longer needed; the memory
stays in use until the programmer explicitly returns it to the memory pool.
c) D. Objects, once constructed, stay active until the program terminates, so though the
programmer may know an object is it no longer needed, CPP does not know this; objects’
memory is returned to the memory pool when the program terminates.
E. Objects, once constructed, stay active until the method in which they were constructed
terminates, so though the programmer may know an object is no longer needed, Java does
not know this; objects’ memory is returned to the memory pool when the method
terminates.
d) Which of these word-pairs represents the IS-A relationship (in English)?
A. duck/bird B. bird/fly C. beak/bird D. bird/nest
e) Which of these pairs represents the HAS-A relationship?
A. bird/duck B. bird/fly C. bird/beak D. bird/flying animal
f) Which of these pairs represents the BEHAVES-LIKE relationship?
A. bird/duck B. bird/fly C. bird/beak D. bird/flying animal
g) Which phrase below best describes the relationship between Country and Capital?
A. IS-A B. HAS-A C. USES D. BEHAVES-LIKE
h) Is it possible to use the techniques of object oriented programming in a language that
does not specifically support OOP?
A. Yes B. No
ii. Differentiate between the following (@2 Marks. Total 8 Marks)
a. Object and class
b. Virtual and pure virtual function
c. Overloading and overriding
d. Object based and object Oriented Programming Languages
iii. Write a function that can be used to swap two numbers (4 Marks)
iv. Define a class called circle with the members’ radius and methods diameter, area, and
circumference. The diameter of the circle is given as 2*radius, area is given as
2*pi*radius2 while the circumference is gives as 2*pi*radius
5 Marks
v. Create a class called cylinder that inherit from the above class but with added members
height and method to compute the surface area and volume of the cylinder. The surface
area is given as 2*(circle area)*height and the volume is given as (circle area) * height.
5 Marks
QUESTION TWO
i. Correct errors in the following statements
a.) int x = 1;
while ( x < 10 );
cout << x;
x ++;
3 Marks
b.) if ( response = “yes” or “YES” )
cout << “You said yes.”;
3 Marks
c.) if ( 1 < x < 5 )
cout << x;
3 Marks
d.) double investment = 2000.0;
double return = 1.016 * investment;
cout << “The return on your investment of $”
<< investment << ” is $” << return;
5 Marks
ii. Write the output of the following segments of code.
a.) cout << “\top ba\na\\na”;
2 Marks
b.) int x = 2;
double y = 3.0;
cout << setprecision(2);
cout << x * y << ” and ” << x / y;
2 Marks
c.) int x = 1;
while ( x < 9 ) {
x ++;
if ( x%2 == 1 )
cout << x << “+”;
}
2 Marks
QUESTION THREE
What is the output of the following CPP programs
a) int x = 13;
int y = 3;
cout << x/y << y/x << x%y;
3 Marks
b) string name = “Sam Gamgee”;
name.erase (3,3); //Sammgee
name.insert (2,”hi”); //Sahimmgee
cout << name;
3 Marks
c) Write a program (starting from #include) that repeatedly collects positive integers from
the user, stopping when the user enters a negative number or zero. After that, output the
largest positive number entered. You may not use any library ther than <iostream>. A
sample run should appear on the screen like the text below.
Enter a number: 3
Enter a number: 10
Enter a number: 2
Enter a number: -213
The largest positive number you entered was 10.
14 Marks
QUESTION FOUR
Examine the following section of C++ code that has several errors and omissions:
#include<iostream.h>
class account
{
private:
float balance;
int actNum;
};
class Deposit: public Account
{
private:
float interestRate;
public:
Deposit(float, int, float);
virtual void display();
}
Deposit::Deposit(float aBalance, int anAccNum, float iRate)
{
balance = aBalance;
actNum = anAccNum;
interestRate = iRate;
}
Deposit::display()
{
cout << “Balance is ” << balance << endl;
cout << “Act Num is ” << actNum << endl;
cout << “Interest Rate is ” << interestRate << endl;
}
void main(void);
{
Deposit d(10.0f, 12345);
d.display();
Deposit *p = d;
p->display();
}
Locate the errors in the code and explain why you believe there is an error at that location.
20 Marks
QUESTION FIVE
Write the implementation for the methods listed below:
#include<iostream.h>
#include<string.h>
class Shape
{
protected:
float area;
float perimeter;
public:
virtual void display();
virtual string getShapeName() = 0;
};
class Rectangle: public Shape
{
public:
Rectangle(float width, float height);
virtual string getShapeName();
};
class Circle: public Shape
{
public:
Circle(float radius);
virtual string getShapeName();
};
12 Marks
(b) Write a main () method to test the Rectangle and Circle classes above.
4 Marks
(c) The Shape class above cannot be instantiated – why not? What type of class is it, and why
do we use this type of class?
6 Marks