revision questions Object Oriented programming , Exercises of Computer Science

Object Oriented programming questions with answers

Typology: Exercises

2017/2018

Uploaded on 02/08/2018

awuoche
awuoche 🇰🇪

5

(1)

1 document

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Department of Business Studies
BACHELOR OF BUSINESS & INFORMATION TECHNOLOGY
BIT 3102: object oriented programming II
Marking scheme
DATE: 18th August 2012 TIME: 08.00 am
INSTRUCTIONS:
Answer Question ONE in Section A and Any other
THREE from Section B.
Write your registration number on all sheets of
the answer book used.
Use a new page for every question attempted and
indicate the question number on the space
provided on each page of the answer sheet.
Fasten together all loose answer sheets used.
Switch o all Mobile Phones and PDAs.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download revision questions Object Oriented programming and more Exercises Computer Science in PDF only on Docsity!

Department of Business Studies

BACHELOR OF BUSINESS & INFORMATION TECHNOLOGY

BIT 3102: object oriented programming II

Marking scheme

DATE: 18 th^ August 2012^ TIME: 08.00 am

INSTRUCTIONS:

• Answer Question ONE in Section A and Any other

THREE from Section B.

• Write your registration number on all sheets of

the answer book used.

• Use a new page for every question attempted and

indicate the question number on the space

provided on each page of the answer sheet.

• Fasten together all loose answer sheets used.

• Switch off all Mobile Phones and PDAs.

SECTION A: ANSWER ALL QUESTIONS {30 MARKS}.

QUESTION 1

a) Describe three types of classes required to work with data (disk): [3 Marks]

  • ifstream for input - read from files
  • fstream for both input and output - both read and write from/to files,
  • And ofstream for output - write on files.

b) List and Explain the access Specifiers used in OOP [6Marks]

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.

c) What is public accessor method and why is it necessary? [4 Marks]

This is a class member function that other parts of the program call to either read or set the value of a private member variable. The general rule to ensure security and accessibility of the data is to keep class data private and create a public function called public accessor method.

d) Differentiate between the following.

i. Function overloading. [2 Marks]

Compile time polymorphism. The function is overloaded

ii. Operator overloading [2 Marks]

Compile time polymorphism. The function is overloaded. Requires creation of a function called operator function

e) Using a syntax code explain the following terms as used in C++ Programming.

i. Destructor [4 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. Used to free computer when the data members of the class are destroyed/. Class rectangle { Int length;

SECTION B: ANSWER ANY THREE QUESTIONS {20MARKS EACH}.

QUESTION 2

a) Explain the term Abstract class [2 Marks]

This is a class designed to be specifically used as a base class. It contains at least one pure virtual function.

b) Using a simple program briefly explain the concept for multiple inheritance in C++

[4 Marks]

Process of creating new class from more than one existing classes.

class A //Base class

{}

class B //Base class

{}

class C : public A , public B // derived class

{}

c) (^) When is a virtual function made “pure”? And what is purpose of a having a pure function? [8 Marks]

  • Pure virtual function is a function declared in a base class that has no definition relative to the base class
  • When virtual function is declared in base class and redefined in derived classes.
  • The function in the base class is seldom used for performing any task.
  • When a virtual function equated to zero is called a pure virtual function
  • Its function is described in the base class that has no definition relative to the base class.
  • It only serves as a place holder a “do-nothing” function declare as virtual void display ( )=0;
  • In such case the compiler requires each derived class to either define the function or redeclare it as a pure virtual function

d) Write a program that saves/writes students names to a disk file. [6 Marks]

// #include

#include

using namespace std;

int main ()

ofstream myfile; //object of type ofstream created

myfile.open ("example.txt");

myfile << "Names for Students.\n 1. Silas\n 2. Nkirote\n 3. Faith \n 4. Caleb\n 5. Patience\n";

myfile.close();

.1 return 0;

account::getdata(); cout <<"Enter Interest\n"; cin >>interest; cout<<"Enter Withdrawal amount\n"; cin>>withdrawal; } void putdata (void) { account::putdata(); cout<<"Interest\n :"<<interest; cout<<"Withdraw amount\n: "<<withdrawal; } }; int main () { saving s1; cout << "Enter data for savings account customer 1:"; s1.getdata(); cout<<"Display data on savings account customer 1:"; s1.putdata(); return 0; }

b) Discuss the three major components of the Standard Template Library [5 Marks]

  • A container: is an object that actually stores data. It is a way data is organized in memory. They are implemented by template classes and therefore can be easily customized to hold different types of data.
  • An algorithm: is a procedure that is used to process the data contained in the containers. They are implemented by template functions.
  • An iterator: is an object that points to an element in a container. We can use iterators to move through the contents of containers.

QUESTION 4

a) List and explain three types of programming errors [6 Marks]

  • Logic error – Occur due to poor understanding of the problem and solution procedure.
  • (^) Syntactic error - Arise due to poor understanding of the language itself.
  • Exceptions - runtime anomalies or unusual conditions that a program may encounter while executing.

b) Explain the following terms in exceptions handling mechanism

[6 Marks]

b)..i Hit the exception - Find the problem

b)..ii Throw an exception - Inform that an error has occurred

b)..iii Catch the exception - Receive the error information

b)..iv Handle the exception - Take the corrective action

c) Explain the concept of templates. Differentiate between function and class

templates [4 Marks]

  • Templates make it possible to use function or class to handle many different data types.
  • 2 categories of templates are - functions and classes templates.
  • (^) Function template is a pattern, or blueprint, for making many functions
  • Class templates are generally used for data storage (container) classes.

d) Explain the term namespace [4 Marks]

  • Namespaces allow group entities like classes, objects and functions under a name.

The format of namespaces is:

namespace identifier

{

Entities;

}

To avoid use of goto tightly controlled looping commands have been introduced: for, while, do---while. Use of goto is always a sign of bad design. Avoid using it.

c) Using a simple diagram show an example of an inheritance hierarchy for university

community members. [6 Marks]

End of Exam