18
CONFIDENTIAL F3031: OBJECT ORIENTED PROGRAMMING Page 2 of 19 SECTION A OBJECTIVE (50 marks) INSTRUCTIONS: This section consists of FOURTY(40) objective questions. Answer ALL questions in the answers booklet. 1. Data can be protected using _____________ such as public, private and protected. A. Default Constructor B. Method Overloading C. Access Specifiers D. Member Functions 2. Which of the following is TRUE about an abstraction? I. The process by which data and programs are defined with a representation similar to its meaning. II. Do not capture only those details about an object that are relevant to the current perspective. III. Tries to reduce and factor out details so that the programmer can focus on a few concepts at a time. A. I and III B. I and IV C. II and III D. II and IV 3. Choose the best answer for the advantages of OOP I. Code will be written in redundancy. II. Allowed to divide the program into modules III. New data and function can be added easily IV. Building a program around collections of data and code A. I and II B. I and III C. II and III D. II and IV

FP 301 OOP FINAL PAPER

Embed Size (px)

Citation preview

Page 1: FP 301 OOP FINAL PAPER

CONFIDENTIAL F3031: OBJECT ORIENTED PROGRAMMING

Page 2 of 19

SECTION A

OBJECTIVE (50 marks)

INSTRUCTIONS: This section consists of FOURTY(40) objective questions.

Answer ALL questions in the answers booklet.

1. Data can be protected using _____________ such as public, private and protected.

A. Default Constructor

B. Method Overloading

C. Access Specifiers

D. Member Functions

2. Which of the following is TRUE about an abstraction?

I. The process by which data and programs are defined with

a representation similar to its meaning.

II. Do not capture only those details about an object that are relevant to the current

perspective.

III. Tries to reduce and factor out details so that the programmer can focus on a

few concepts at a time.

A. I and III

B. I and IV

C. II and III

D. II and IV

3. Choose the best answer for the advantages of OOP

I. Code will be written in redundancy.

II. Allowed to divide the program into modules

III. New data and function can be added easily

IV. Building a program around collections of data and code

A. I and II

B. I and III

C. II and III

D. II and IV

Page 2: FP 301 OOP FINAL PAPER

CONFIDENTIAL F3031: OBJECT ORIENTED PROGRAMMING

Page 3 of 19

4. Which symbol is used for standard C++ comment?

I. /

II. /* */

III. //

IV. ||

A. I only

B. I and II

C. II and III

D. All the above

Question 5 refers to the diagram below;

Diagram 1

5. Identify the exact concept shown in the diagram?

A. Concept of class

B. Concept of object

C. Concept of abstraction

D. Concept of encapsulation

6. What are the two important elements of the object-oriented approach?

A. Class and Object

B. Class and Function

C. Inheritance and Polymorphism

D. Inheritance and Abstraction

Student Name MatrixNo CPA Course Study() AttendClass()

Student

Page 3: FP 301 OOP FINAL PAPER

CONFIDENTIAL F3031: OBJECT ORIENTED PROGRAMMING

Page 4 of 19

7. Complete the statement below.

__________________ is a programming paradigm that uses object data structures

consisting of data and methods and their interaction with design applications and

computer programs.

A. Structured Programming

B. Inheritance

C. Object Oriented Programming

D. Encapsulation

8. Complete the statement below.

__________________ is a subset of procedural programming that enforces a logical

structure on the program being written.

A. Exception Handling

B. Object Oriented Programming

C. Package

D. Structured Programming

9. Which punctuation must be used to end an expression statement?

A. . (dot)

B. ; (semi-colon)

C. : (colon)

D. ‘(single quote)

10. Which one of the following function must be contained in all C++ programs?

A. start()

B. system()

C. main()

D. program()

Page 4: FP 301 OOP FINAL PAPER

CONFIDENTIAL F3031: OBJECT ORIENTED PROGRAMMING

Page 5 of 19

Figure 1

Figure 1

11. Which of the following indicates the error found in the code segment in Figure 1?

A. Missing semicolon,; in Line 11.

B. Missing access specifier, private in Line 3.

C. Use incorrect keyword class in Line 1.

D. Code written in Line 9 is a prototype of Area ().

12. “Think of a person driving a car. He does not need to know the internal working of

the engine or the way gear changes work, to be able to drive the car” The statement

above relate to:

A. Inheritance

B. Abstraction

C. Encapsulation

D. Data Abstraction

1. Class Rectangle

2. {

3.

4. double length;

5. double width;

6.

7. public:

8.

9. voidArea(double,double);

10.

11. }

Page 5: FP 301 OOP FINAL PAPER

CONFIDENTIAL F3031: OBJECT ORIENTED PROGRAMMING

Page 6 of 19

Questions 13 and 14 are based on the program segment in Figure 2.

Figure 2

Figure 2

13. How many variables can be found in the program?

A. One

B. Two

C. Three

D. Four

14. What is the name of the constructor for the program?

A. Book1()

B. book1()

C. Book()

D. ~Book()

//Program to display the price of a book

#include <iostream.h>

class Book

{

private:

float price 1;

public :

Book(float price1,float price2)

~Book()

{cout <<”\n The object is destroyed \n”; }

};

Book::Book (float price1,float price2)

{ cout<<”\n The object is initialized \n”;

cout<<”\n Price : “<<price1;

cout<<”\n Price : “<<price2;

}

void main()

{ __X__ book1(148.5,130.5);

}

Page 6: FP 301 OOP FINAL PAPER

CONFIDENTIAL F3031: OBJECT ORIENTED PROGRAMMING

Page 7 of 19

15. Identify the error with the constructor shown in the following fragment?

Figure 3

A. Data types must have private access specifier.

B. A constructor cannot have a return value.

C. A constructor must have parameter in parenthesis.

D. A class cannot have semicolon (;) at the end of the class.

16. What is the purpose of a destructor ?

A. To allocate a memory.

B. To deallocate the memory used.

C. To destroy any object.

D. To destruct a program.

17. Which are the characteristics of a destructor?

I. The function has the same name as the class

II. Starts with a tilde (~)

III. The destructor cannot take arguments

IV. Multiple destructors for the same class can be defined

A. I, II, III, IV.

B. I, II, III.

C. I, II, IV.

D. I, III, IV.

18. Which of the following is defined as ~<class_name>(){} ?

A. Default Constructor

B. Constructor

C. Destructor

D. Class

class Invent{

int x, y, z;

public:

int Invent();

};

Page 7: FP 301 OOP FINAL PAPER

CONFIDENTIAL F3031: OBJECT ORIENTED PROGRAMMING

Page 8 of 19

19. Which of the following is not a characteristic of destructor?

A. It reclaims the memory that is allocated to the object.

B. It is called automatically when an object is destroyed or goes out of scope.

C. It has special function whose task is to initialize the objects of its class.

D. It has the same name as the class but is preceded by a tilde (~)

20. Given class C is a friend of class B and class B is a friend of class A. What does that

mean?

A. Class B can access data of a Class C

B. Class C can access data of a Class B

C. Class A can access data of a Class B

D. Class A and B can access data of Class C

21. Which one of the statements is true about abstract class?

I. Consists of pure virtual function

II. The methods in the base class and the derived class use the same method name.

III. Error will occur if the class does not define the method.

A. I only

B. I and II

C. I,II and III

D. II and III

Page 8: FP 301 OOP FINAL PAPER

CONFIDENTIAL F3031: OBJECT ORIENTED PROGRAMMING

Page 9 of 19

22.

Figure 4

Figure 4 shows the C++ segment code. Which of the following statement is TRUE?

A. Class A attempts to access private data of class B.

B. Class B attempts to access the private data of class A.

C. Class A declares to be friend to class B.

D. Class A inherits properties of class B.

23. Why is the overloading method useful?

A. It conserves the identifier.

B. It allows us to give related function common names

C. It reduces the total number of functions, which result in improving program

performance.

D. It increases the total number of functions, which result in improving program

flexibility.

24. Which are the rules of Overloading Function.

I. The numbers of parameter received for each version must be the same.

II. The numbers of parameter received for each version must be different.

III. The type of parameter received must be different.

IV. The type of parameter received must be the same.

V. The return type of the functions is the same.

A. I & III

B. II & IV

C. II, III & V

D. I, III & V

class A

{ friend class B

private :

int x,y;

public :

A ( int a,int b)

{

x = a;

y = b;

}

};

Page 9: FP 301 OOP FINAL PAPER

CONFIDENTIAL F3031: OBJECT ORIENTED PROGRAMMING

Page 10 of 19

25. Which of this set of operators is commonly overloaded for user-defined classes?

I. = (assignment operator)

II. + - * (binary arithmetic operators)

III. += -= *= (compound assignment operators)

IV. == != (comparison operators)

A. I, & II

B. I, II, & III

C. II, III & IV

D. I, II, III, & IV

Question 26 is based on Figure 5 below:

Figure 5

#include <iostream.h>

class overloadQ

{

public:

void ovrldMethod(int num1)

{

cout<<"Result Is: ";

cout<<num1+2;

}

void ovrldMethod(int num1, int num2)

{

cout<<"Result Is: ";

cout<<num1*num2<<endl;

}

};

int main()

{

overloadQ ovrld;

ovrld.ovrldMethod(3);

ovrld.ovrldMethod(5,7);

ovrld.ovrldMethod(6);

}

Page 10: FP 301 OOP FINAL PAPER

CONFIDENTIAL F3031: OBJECT ORIENTED PROGRAMMING

Page 11 of 19

26. What is the output for this program?

A.

B.

C.

D.

27. Which concept is shown in the application of templates?

A. Polymorphism

B. Inheritance

C. Abstraction

D. Encapsulation

28. Consider the definition of the function template in Figure 6.

Figure 6

Based on the above definition, what is the output if, the program called the template

in the main function with the parameters for x1 is “Happy” and x2 is “Holiday”?

A. Happy

Holiday

B. Happy x2

C. Happy Holiday

D. Happy+Holiday

Result Is: 5Result Is: 35

Result Is: 8

Result Is: 5

Result Is: 8Result Is: 35

Result Is: 6Result Is: 35

Result Is: 12

Result Is: 5

Result Is: 35

Result Is: 8

template <class type>

type weekend ( type x1, type x2)

{

return x1+x2;

}

Page 11: FP 301 OOP FINAL PAPER

CONFIDENTIAL F3031: OBJECT ORIENTED PROGRAMMING

Page 12 of 19

29. A class named Car inherits publicly all properties of another class named Vehicle.

Which of the following C++ code describes the situation?

A. class Car : public Vehicle {};

B. class Vehicle : public Car {};

C. class Car : private Vehicle{};

D. class Vehicle : protected Car {};

30. Which is the correct coding to define the inheritance class in Figure 7?

Figure 7

A. class fruit{};

class fruit: public local_ fruit{};

class local_ fruit : langsat{};

B. class fruit{};

class fruit: public local_ fruit{};

class fruit: langsat{};

C. class fruit{};

class local_ fruit : public fruit{};

class langsat: public local_ fruit{};

D. class fruit{};

class local_ fruit :: public fruit{};

class langsat:: public local_ fruit{};

31. To make a member accessible within a hierarchy, but private otherwise, what access

specifier do you use?

A. Public

B. Private

C. Protected

D. Virtual

fruit

local fruit

langsat

Page 12: FP 301 OOP FINAL PAPER

CONFIDENTIAL F3031: OBJECT ORIENTED PROGRAMMING

Page 13 of 19

32. Which of the following is the type of inheritance where multiple derived classes

inherit members from a single base class?

A. Single inheritance

B. Multiple inheritance

C. Hierarchical inheritance

D. Hybrid inheritance

33. Which of the following is the main concept applied in polymorphism?

A. Overload method

B. Overload operator

C. Overload destructor

D. Overload constructor

34. Which of the following are the advantages of polymorphism?

I. Easy to develop libraries

II. Definitions are more general

III. Greater chance for functions re-use

IV. Can be convert into package

A. I,II &III

B. I,II &IV

C. II,III &IV

D. I,III &IV

35. The following are the different types of polymorphism provided by C++ EXCEPT

A. Method overloading

B. Operator overloading

C. Function overloading

D. Virtual function

36. Complete the statements below.

If a catch statement is written to catch exception objects of a base class type, it can

also catch all _____ derived from that base class.

A. Exceptions for objects

B. Objects of classes

C. Arguments

D. Errors

Page 13: FP 301 OOP FINAL PAPER

CONFIDENTIAL F3031: OBJECT ORIENTED PROGRAMMING

Page 14 of 19

37. Complete the statement below.

When the function int some Function(char c) throw( ) is executed, it _____

A. can throw anything

B. may throw an integer

C. may throw a character

D. may not throw anything

38. What happens if an exception is thrown outside a try block?

A. It generates compile error.

B. It generates run time error.

C. It is a call to terminate the program.

D. It is a call to restart the program.

39. If a try block has two or more associated catch blocks, select which one will be

triggered by an exception.

A. All that catches parameter type matches the exception.

B. The first one which catches parameter type matches the exception.

C. The last which catches parameter type matches the exception.

D. The catches parameter type most closely matches the exception.

Page 14: FP 301 OOP FINAL PAPER

CONFIDENTIAL F3031: OBJECT ORIENTED PROGRAMMING

Page 15 of 19

40. Based on Figure 8 below, what the output will be displayed if the input value for a =

15 and value for b = 15?

Figure 8

A. Result (a/x) = 0

B. Exception caught: x = 0

C. Exception caught: x = 15

D. Result (a/x) = 15

#include <iostream>

using namespace std;

void main()

{

int a, b;

cout << "Enter values of a and b \n";

cin >> a;

cin >> b;

int x = a - b;

try

{

if(x != 0)

cout << "Result (a/x) = " << a/x << "\n";

else

throw(x);

}

catch(int x)

{

cout << "Exception caught: x = " << x << "\n";

}

cout << "End"; }

Page 15: FP 301 OOP FINAL PAPER

CONFIDENTIAL F3031: OBJECT ORIENTED PROGRAMMING

Page 16 of 19

SECTION B

SUBJECTIVE (50 marks)

INSTRUCTION

This section consists of TWO (2) subjective questions.

Answer ALL questions.

QUESTION 1

(a) Describe two differences between function overloading and operator overloading.

(6 marks)

(b) Explain the following types of inheritance using a simple program code:

i. Single inheritance (3 marks)

ii. Multiple inheritance (3 marks)

(

c

)

Answer the following questions based on Figure 1.3

Figure 1.3

#include<iostream.h>

class cube {

private:

int side;

public:

(i)___________________

(ii)___________________

};

void cube :: setdata(){

side=20;

cout<<”\n SIDE OF CUBE: ”<<side;

}

(iii)______________________

void main( ){

(iv)______________________

c.setdata();

cout<<”\n VOLUME OF CUBE: ”<<__(v)___;

}

Page 16: FP 301 OOP FINAL PAPER

CONFIDENTIAL F3031: OBJECT ORIENTED PROGRAMMING

Page 17 of 19

i. Declare a prototype method setdata ( ) (1 marks)

ii. Declare the method volume( ) as a friend of class cube. (1 marks)

iii. Write a definition of the non-member method volume() that will

calculate the volume of cube. (3 marks)

iv. Create object c for class cube. (1 marks)

v. Call non-member method volume( ) (1 marks)

vi. Write the output of the program. (1 marks)

(d) Problem :

Figure 1.1 shows an in complete program segment. You are required to complete the

program coding using two forms of function called Addition.

In one form it has two parameters and in the other it has three parameters.

Figure 1.1

(5 marks)

void main()

{

int j, k, l, m, n, p, q;

q = 4; j = 6;

k = Addition (q, j);

cout << endl << "Sum of two is " << k << endl;

l = 5; m = 6; n = 7;

p = Addition (l, m, n);

cout << endl << "Sum of three is " << p << endl;

}

Page 17: FP 301 OOP FINAL PAPER

CONFIDENTIAL F3031: OBJECT ORIENTED PROGRAMMING

Page 18 of 19

QUESTION 2

(a) Describe the difference between private and protected members of a class.

(4 marks)

(b) Based on figure 2.1, give the output of the statement code. (2 marks)

Given :

i. a= 30, b =25

Figure 2.1

Figure 2.1

(c) Convert the segment code in Figure 2.2 to operator overloading. (3 marks)

Figure 2.2

int a, b;

cout << "Enter values of a and b \n";

cin >> a;

cin >> b;

int x = a - b;

try

{

if(x != 0)

cout << "Result (a/x) = " << a/x << "\n";

else

throw(x)

}

catch(int j)

{

cout << "Exception caught: x = " << x << "\n";

}

cout << "End";

}

double Subtraction(double a,

double b, double c)

{

return a-b-c;

}

Page 18: FP 301 OOP FINAL PAPER

CONFIDENTIAL F3031: OBJECT ORIENTED PROGRAMMING

Page 19 of 19

(d) Create a function template named BallArea( ). The function receives a parameterized

argument representing the radius of a ball and returns a value which representing the

area of a ball. Write a main ( ) function demonstrates that the function will works

correctly with either an integer or a double argument.

(9 marks)

(e) Write a program that receives a CGPA from a student, by using exception handling

with a CGPA more than 4.0 which will display error messages to user.

(7 marks)