0% found this document useful (0 votes)
22 views4 pages

Test 1 (ch7 ch8)

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

Test 1 (ch7 ch8)

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

TEST 1 MCOB000 – OOP February 27, 2023

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.

QUESTION 1 MODIFIED TRUE/FALSE [11]


State if the statement is true or false, if false provide the correct answer or justification.
1. When a class has multiple destructors, the parameter lists must different.
2. You can pass an entire object to a function. When you do, the function can change the
contents at the original object’s memory address eliminating the need to make a separate
copy of the passed object within the function.
3. After the following statements execute, any change to cash modifies someMoney, and vice
versa.
double someMoney;
double *cash = &someMoney;
4. Using an object within another object is known as composition; a benefit of object-oriented
programming is that one can reuse well-crafted classes as member-objects.
5. The statement Customer pnp(); declares a Customer object named pnp using a constructor
that does not require any arguments.
6. To create just one memory location that can be altered no matter how many objects you
instantiate, you declare the field to be non-static and constant.

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

4. Which of the following could coexist with a function prototyped as


void function(double, int = 0); with no ambiguity possible?
a) double function (double); c) void function(double = 0.0, int = 0);
b) double function(double, int); d) none of the above

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);
};

Write an implementation of the withdraw operation (i.e. function). All withdrawal


transactions are free. No overdrafts allowed. The program should be user friendly and give
feedback to the user. (8)

TOTAL: 50
*** END OF TEST ***

Page 4 of 4

You might also like