0% found this document useful (0 votes)
11 views

BIT 123 Object Oriented Programming MS

exam

Uploaded by

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

BIT 123 Object Oriented Programming MS

exam

Uploaded by

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

ST.

PAUL’S UNIVERSITY

Private Bag 00217 Limuru


Tel: 020-2020505/510, 0728-669000, 0736-424440
Email: [email protected]
Website: http://www.stpauls-limuru.org

DEPARTMENT OF BUSINESS STUDIES


BACHELOR OF BUSINESS & INFORMATION TECHNOLOGY
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.

DATE: TIME:

INSTRUCTIONS:

1. Candidates answer all questions in section A and any other four


from section B. All questions carry equal marks
2. Write your registration number on all sheets of the answer book
used.
3. Use a new page for every question attempted and indicate the
question number on the space provided on each page of the answer
sheet.
4. Switch off mobile phones and PDA’s.

5. Tie together all loose sheets.


BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.

SECTION A: ANSWER ALL QUESTIONS {20


MARKS}.

QUESTION 1

a. Briefly explain the available visibility modes used in OOP. [6 Marks]


Public: data members and member functions are accessible outside
the class.
Protected: data members and member functions are only available to
derived classes.
Private :data members and member functions can’t be accessed
outside the class.
b. Distinguish between function Overloading and operator overloading.

[2 Marks]

a program can consist of two functions where one can perform integer
addition and other can perform addition of floating point numbers but
the name of the functions can be same such as add.
The function add() is said to be overloaded.
Operator overloading:the ability to use one function more than one
time

c. Define Polymorphism. How is it implemented in OOP environment


[2
Marks]

Page 2 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.

 By overloading functions.
 Inheritance

d. Differentiate between the following.

i. Parameter constructor. [2 Marks]

Accepts no parameters to its declaration

ii. Copy constructor [2 Marks]

Used to declare and initialize an object form another.

e. Using a syntax code(for i,ii,iii) define the following terms as used in C+


+ object oriented

Programming.

i. Constructor [2 Marks]

This is a special function in a class which has the same name as its
class, has no return type and returns no value

Class A

{int m,n; public: A();//constructor declared };

A::A(){ }

ii. Arrays [2 Marks]

Page 3 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.

An array is a fixed-size collection of objects that are all the same


type.
iii. Keyword [2 Marks]

A word with special meaning when used in a program and can not
be used to declare variables by programerseg integer,class,

SECTION B: ANSWER ANY FOUR QUESTIONS {20 MARKS


EACH}.

QUESTION 2:

a) State the purpose of the following in programming

i. Destructor [2 Marks]
Used to destroy the objects that have been created by a constructor
ii. Reference variable [2 Marks]
Provides an alias (alternative name) for previously defined varriable

iii. Object [2 Marks]


Generally, any item that can be individually selected and manipulated.
This can include shapes and pictures that appear on a display screen as
well as less tangible software entities. In object-oriented programming, for
example, an object is a self-contained entity that consists of both data
(attributes) and procedures (functions or behaviors) to manipulate the
data.

b) Define a class using a syntax code [3 Mark]

Page 4 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.

A class in C++ is an encapsulation of data members and functions that


manipulate the data. The class can also have some other important
members which are architecturally important.

c) Highlight three advantages of inheritance. . [3 Marks]

i. It permits code reusability.


ii. Reusability saves time in program development.

iii. It encourages the reuse of proven and debugged high-quality software,


thus reducing problem after a system becomes functional.

d) Develop main function such that the sides and area of your square are shown
when the program is executed. [8 Marks]

Int main()

Int length=20;; (Other values can do)

Square sqrt(length);

Cout<<”width is “<<rect.Getwidth<<endl;

Cout<<”Area is “<<rect.Area<<endl;

Return 0;

Page 5 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.

QUESTION 3:

a) Implement a class named rect from the base class Sqr given below.

[7 Marks]

#include<iostream.h>

classsqr // Base Class

int lenght;

public:

sqr(int x) {

lenght=x;

int get_lenght() {

Page 6 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.

return lenght;

double Area() {

return lenght*lenght;

~sqr() {

cout<<"\nDestructor1 called";

};

Class rect: public sqr

Private:

Int width,

Public:

rect (int x, int y){ lenth=x; width=y;}

Int Getwidth(){return width;}

Int Getlength(){return length;}

Double area(){return length *width;}

Page 7 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.

};

b) Write a program which calculates the average of the elements of the row of
the two dimensional array. Say row 1 elements{2,3,4,5,5}, row 2 elements
{ 2,6,7,3,2} [10 Marks]
#include<iostream>
using namespace std;

int main ()
{
int age[2][5]= { {2,3,4,5,5}, { 2,6,7,3,2}};
int i,j;
int sum=0,avg=0;
for(i=0;i<2;i++)
{
for(j=0;j<5;j++)
{
sum=sum+age[i][j];
}
avg=sum/5;
cout << "Average of the elements of the row " << i+1 << " is
" <<avg << endl;
sum=0;
}
return(0);
}

c) What is a function? Explain any 2 ways for defining functions in


programming. [3 Marks]

Page 8 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.

Set of code used to perform some specified task with return types of
void,int orfloat to the calling program

i. Outside the class definition


ii. Inside the class definition

QUESTION 4:

a) Using classes, write a program that captures student data. The program
should prompt a user to enter a student Name, Age, course and
telephone number. The program should then display the details.

[15 Marks]
#include<iostream>
using namespace std;
class student
{
Public:
char name[50];
int age;
char course70];
int teleno;

Page 9 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.

}stud1;

int main ()
{
student stud2;
cout << "Enter the name of the student" << endl;
cin >> stud1.name;
cout << endl << "Enter the age of the student" << endl;
cin >> stud1.age;
cout << endl << "Enter the course he undertakes " << endl;
cin >> stud1.course;
cout << endl << "Enter the telephone no of the employee" << endl;
cin >> stud1.teleno;

stud2=stud1;
cout << endl << "The name of the student : " <<stud2.name << endl;
cout << "The age of the student : " << stud2.age << endl;
cout << "The course he/she studies: " << stud2.course << endl;
cout << "His phone number is: " << stud2. teleno << endl;

return(0);
}

b) What is a structure? Highlight 2 of its different characteristics


[2 Marks]

Is a form of compound data type

It is an aggregate of data items of different data types.

Page 10 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.

It acts as a cluster of variables of different data types.


c) What is a static variable [3 Marks]

static: a variable that is known only in the function that contains its definition
but is never destroyed and retains its value between calls to that function. It
exists from the time the program begins execution

QUESTION 5

a) Differentiate between the following terms. [6 Marks]

i. Pointer and array

A pointer is an address in memory where a variable is located.


An array is a conceptual data representation consisting of a list
of more than one item of a particular scalar type

ii. Structure and function

Structure- A data structure is a group of data elements grouped


together under one name

Function- A function is a group of statements that is executed


when it is called from some point of the program.

iii. Derived data type and system data type.

Derived type- defined by the system include pointer, array and


reference. User defined derived datatypes are built on existing
types

User defined- The include functions,sturutures enum they are


provided by the user.

Page 11 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.

b) Develop a program that demonstrates hybrid inheritance showing the


expected output. [8 Marks]

class M
{
public:
void display(void)
{
cout<<"class M\n";
}
};
class N
{
public:
void display(void)
{
cout<<"class N\n";
}
}; // what display ()function will be used?
This can be resolved by overriding.
i.e
class P: public M; public N;
{
public:
void display(void) //overrides display() of M and N
{M:: display();
}
}
};
Now we use the derived class as follows;
int main()
{
Pp
p.display();
return 0;
}

c) Use a program to differentiate between switch and nested if


statements. Explain why we need do while and for in any programming
languages.

[6 Marks]

Page 12 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.

Switch()

case 1:

cout<<””:

case 2:

cout<<””:

case 3:

cout<<””:

default:

cout<<””:

QUESTION 6

a) List FOUR characteristics of Object Oriented Programming. [4 marks]

Any four

 Emphasis is on data rather than on procedures


 The programming problem is divided into objects

 Data and functions that operate together are tied together

 Objects can communicate with one another through functions

 Models the real world objects very well

Page 13 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.

b) Write a full C++ program that keeps printing (i.e. non-stop) the multiples of
the integer 2 (i.e. 2, 4, 8, 16, 32, 64…). [8 marks]

# include <iostream.h>
void main (void)
{
cout << “\n Printing multiples of 2…;
for (int i = 2; i > 0 ; i *= 2)
cout << i << “ “;

c)
d) Below is program that uses do-while loop.

#include<iostream>
Using namespace std;
int main()
{
int digit=0;
Do {
cout<<digit<<endl;
digit++;
while(digit<=9)
}}
1. Write the expected output after running the program.
(2marks)
0
1
2
3
4
5
6

Page 14 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.

7
8
9
e) Identify SIX errors in the following code. [6 marks]

/calculating the total amount of money earned in n days

#include <iostream>

using namespace std;

int main( );

Int, n;

int total, rate= 20;

cout>>"Enter number of days worked: ";

cin>>n

total = n * rate;

cout<<"\n| For rate RM20 per day |";

cout<<"For"<<”n”<<"days worked, you have earned $ ";

cout<<total<<endl;

return 0;

 /calculating the total amount of money earned in n


 int main( );
 int, n; - comma
 cin>>n – no termination

 cout>>"Enter number of days worked: "; >> wrong


 cout<<"For"<<”n”<<"days worked, you have earned $ ";- “n” should
be a variable

Page 15 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.

QUESTION 7.

a). Using examples differentiate between initializing a value and assigning


a value. [4 marks]

Initializing is inserting a value to a variable with the type declaration


e.g. int a=2

Assigning is inserting a value into a variable without type


declaration e.g. a=2

b). Based on the following table, identify the applicable data type for
variables Staff ID, Name, Salary and Taxable respectively.

Staff ID Name Salary ($) Taxable


John
3929 4500.00 Yes
Martin
Ruth
3204 956.50 No
Danny

[4 marks]

 StaffID – int
 Name

 Salary – float

 Taxable – bool (Boolean)

Page 16 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.

c). Function overloading is a powerful feature in OOP languages. Using


function named computesalary, write a program that can be used to
compute salary of a permanent employee given : basic salary,
Allowances and PAYE. The same function is used to calculate the
salary of Casual worker given: hours worked and hourly rate.[6 marks]

Int computesalary (basicsal,allowances,paye) {

Int salary;

Salary=basicsal +allowances – paye;

Return(salary); }

Int computesalary (hours,rate) {

Int salary;

Salary=hours*rate;

Return(salary); }

d). Using a syntax code and simple diagram show an example of an inheritance hierarchy
for different types of bank account s. [6 mark]

ACCOUNT

SAVINGS CURRENT FIXED DEPOSIT

Page 17 of 18
BIT 123: OBJECT ORIENTED PROGRAMMING IN C++.

CLASS ACCOUNT

};

CLASS SAVINGS:PUBLIC ACCOUNT

};

CLASS CURRENT:PRIVATE ACCOUNT

};

CLASS FIXED_DEPOSIT:PUBLIC ACCOUNT

};

End of Exam

End of Exam

Page 18 of 18

You might also like