Test 2 (ch9)
Test 2 (ch9)
INSTRUCTIONS
1. Read the guidelines on each question.
2. Start each question on a new page.
3. Multiple Choice Questions (MCQ): Write the answer in capital letters.
4. You may answer the questions in any preferred order.
5. Do not mix the numbering order of a question.
Page 1 of 4
QUESTION 2 MULTIPLE CHOICE [5]
Select the best choice that completes the statement or answers the question.
1. A function that directly uses private data from a single class should be a _______ function
of that class.
a) member c) both of the above
b) friend d) none of the above
2. If you have correctly overloaded the * operator to multiply two members of the Furniture
class, and you have declared two Furniture objects, aDesk and aChair, then which of the
following is a legal expression?
a) Furniture * Furniture c) Furniture * aChair
b) aDesk * aChair d) all of the above
3. The name of the function that overloads the && operator is ___________.
a) operator&&( )
b) operator&()
c) any valid C++ identifier can be used
d) two of the above
4. If you have correctly overloaded the * operator to multiply two members of the Furniture
class, and you have declared two Furniture objects, aDesk and aChair, then which of the
following is a legal expression?
a) aChair.operator*(aDesk) c) aChair * aDesk
b) aChair * aChair d) all of the above
5. To be able to add three or more objects in sequence, as in x + y + z, you must overload the
+ operator to ___________ .
a) accept multiple parameters
b) accept no parameters
c) return an object of the same type as the parameters
d) return a primitive type
Page 2 of 4
QUESTION 3 SHORT ANSWERS [16]
1. Why is operator overloading needed in programming? (2)
2. What is the difference between the two statements return this; and return *this;? (2)
3. Why pass a constant reference when overloading the insertion operator (<<), but a non-
constant reference when overloading the extraction operator (>>)? (3)
4. Suppose that the << operator is to be overloaded for a user-defined class Time. Why must
the << operator be overloaded as a friend function? (1)
5. Consider a function prototyped as friend void function(Student, Test, Exam);
which is a friend to Student, Test and Exam classes.
After defining all classes is a compilation error. Does not matter which class definition
comes first. Answer the following questions assuming the classes are on the same file.
a) What is the reason for the error? (2)
b) How would you correct the error? (2)
6. What is the purpose of the int parameter when overloading a postfix decrement operator? (2)
7. Consider the statements below, and answer the following question:
int a = 0;
int b = 1;
int c = a++;
int d = ++b;
What are the values of a, b, c and d after the above statements have executed? (2)
Page 3 of 4
QUESTION 4 PROGRAMMING [13]
class Ride {
private:
int rideNumber;
int minutes; //duration of the ride
double charge; //total cost of the ride
};
a) Overload the greater than operator. A Ride is considered greater than another Ride when
the cost per minute is higher. (Write only the function implementation) (5)
b) Overload the insertion operator to display Ride’s values. (Write only the function
implementation) (4)
class Account {
private:
string accHolder;
int accNumber;
double balance;
};
a) Overload the subtraction operator to perform the withdraw operation which allows an
amount to be subtracted from Account’s balance. Overload the operator such that the
amount value that will be used to decrease the Account balance can be on the left or
right of the operator. (Write only the function prototype) (4)
TOTAL: 45
*** END OF TEST ***
Page 4 of 4