Test 1 (ch7 ch8)
Test 1 (ch7 ch8)
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. However, 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. findSmallest() is a typical function name for ___________ functions.
a) inspector
b) auxiliary
c) mutator
d) none of the above
2. Which of the following is a legal example of the way overloading is used in C++?
a) creating any two functions for a c) creating two constructors for a class
class
b) creating two destructors for a d) all of the above
class
3. What relationship is formed between classes when using composition?
a) has-a(n) c) private
b) is-a(n) d) contained
5. The primary reason you want to use #define, #ifndef, and #endif is to ___________ .
a) provide names for class constructors and destructors
b) save memory
c) avoid declaring a class twice
d) ensure a class has been declared before using it
Page 2 of 4
QUESTION 3 SHORT ANSWERS [19]
1. What is a constructor? Why would you include a constructor in your class? (3)
2. When should you use an inline function? (2)
3. Explain the terms coupling and cohesion as used in object oriented programming. (2)
4. Consider the function shown below, assuming an Employee class that has name, staff
number and salary as data members.
void Employee::setSalary(double salary) {
Employee::salary = salary;
}
a) Why is the scope resolution operator needed within the function body? (2)
b) Modify the function to explicitly use the this pointer (2)
5. What is the output of the program below? If necessary, justify your answer. (8)
1 #include<iostream>
2 using namespace std;
3
4 class House {
5 private:
6 int squareMeters;
7 public:
8 House();
9 void house();
10 ~House();
11 };
12
13 House::House() {
14 cout << "House." << endl;
15 squareMeters = 2885;
16 }
17
18 void House::house(){
19 cout<<"ERF size: "<<squareMeters<<endl;
20 }
21
22 House::~House() {
23 cout << "Transferred." << endl;
24 }
25
26 int main() {
27
28 void createHouse();
29 House aHouse;
30
31 for(int x = 0; x < 4; ++x) {
32 createHouse();
33 }
34
35
36 return 0;
37 }
38
39 void createHouse() {
40 House bHouse;
41 cout << "End of function." << endl;
42 }
Page 3 of 4
QUESTION 4 PROGRAMMING [15]
1. Write a method that receives an integer array and its size then returns the smallest value in the
array. (write only the function definition) (7)
2. Consider the Account class below that models a bank account.
class Account {
private:
string accHolder;
int accNumber;
double balance;
public:
void printAccount();
void deposit(double);
void withdraw(double);
};
TOTAL: 50
*** END OF TEST ***
Page 4 of 4