You are on page 1of 9

ANSWERS TO EXERCISES

Chapter 1
1. a. true; b. false; c. false; d. false; e. false; f. true; g. false; h. false

2. The black-box refers to testing the correctness of the program; that is, making sure that the program does what it
is supposed to do. In black-box testing, you do not know the internal working of the algorithm or function. You
know only what the function does. Black-box testing is based on inputs and outputs.

3. The white-box refers to testing the correctness of the program; that is, making sure that the program does what
it is supposed to do. White-box testing relies on the internal structure and implementation of a function or
algorithm. The objective is to ensure that every part of the function or algorithm is executed at least once.
4.
Precondition: The value of x must be nonnegative.
Postcondition: If the value of x is nonnegative, the function returns the positive square root
of x; otherwise, the program terminates.

5. a. O(n2)
b. O(n3)
c. O(n3)
d. O(n)
e. O(n)
f. O(nlog2n)

6. 12

7. a. 43
b. 4n + 3
c. O(n)

8. -51, -50, -49, -1, 0, 1, 49, 50, 51

9. One possible answer is as follows:

int sumSquares(int n)
{
int sum = 0;

for (int j = 1; j <= n; j++)


sum = sum + j * j;

return sum;
}

The function sumSquares is of the order O(n).

10. The for loop has n iterations. Each time through the loop a fixed number of statements execute. Hence, this
algorithm is O(n). Now each time through the loop there are two additions . Thus, the number of additions is 2n.
11. The for loop has 2n-4 iterations. Each time through the loop a fixed number of statements execute. Hence, this
algorithm is O(n). Now each time through the loop there is one addition, one subtraction, and one
multiplication. Thus, the numbers of additions is 2n-4, the number of subtractions is 2n-4, and the number of
multiplications is 2n-4.
12. The outer for loop has 2n iterations. For each iteration of the outer loop, the inner loop has n iterations. Hence,
the total number of iterations of these loops is 2nn=2n². This implies that this algorithm is O(n²).
13. There are three nested for loop and each of these loops has n iterations. For each iteration of the outer loop, the
middle loop has n iterations. Thus, the middle loop executes n times and has n² iterations. For each iteration of
the middle loop, the inner most loop has n iterations. It follows that the inner most loop has n³ iterations. Hence,
this algorithm is O(n³).
14. a. Constructors have no type. Therefore, the statement:

int AA(int, int);

should be :

AA(int, int);

b. Missing semicolon after }.


c. There should be a : after the member access specifier public. (Replace ; with : after the label
public.)
15.
a. 6
b. 2
c. 2
d.
void xClass::func()
{
u = 10; v = 15.3;
}

e.
void xClass::print()
{
cout << u << " " << v << endl;
}

f.
xClass::xClass()
{
u = 0;
v = 0;
}

g. x.print();
h. xClass t(20, 35.0);

16.
a. (i) Constructor at Line 1
(ii) Constructor at Line 3
(iii) Constructor at Line 4
b.
CC::CC()
{
u = 0;
v = 0;
}
c.
CC::CC(int x)
{
u = x;
v = 0;
}
d.
CC::CC(int x, int y)
{
u = x;
v = y;
}

CC::CC(double x, int y)
{
u = y;
v = x;
}

17. 00:00:00
23:13:00
06:59:39
07:00:39
The two times are different.

18. (a)-(c)

class secretType
{
public:
void print() const;
void setName(string);
void setAge(int);
void setWeight(int);
void setHeight(double);
string getName() const;
int getAge() const;
int getWeight() const;
int getHeight() const;
secretType(string = "", int = 0, int = 0, double = 0.0);

private:
string name;
int age;
int weight;
double height;
};

d.

void secretType:: print() const


{
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Weight: " << weight << endl;
cout << "Height: " << height << endl;
}
void secretType::setName(string n)
{
name = n;
}

void secretType::setAge(int a)
{
age = a;
}

void secretType::setWeight(int w)
{
weight = w;
}

void secretType::setHeight(double h)
{
height = h;
}

string secretType::getName() const


{
return name;
}

int secretType::getAge() const


{
return age;
}

int secretType::getWeight() const


{
return weight;
}

int secretType::getHeight() const


{
return height;
}

secretType::secretType(string n, int a, int w, double h)


{
name = n;
age = a;
weight = w;
height = h;
}

19. a. personType student("Buddy", "Arora");


b. student.print();
c. student.setName("Susan", "Miller");
Chapter 2
1. a. true; b. true; c. true; d. false; e. false; f. true; g. true; h. false; i. false; j. true; k. false; l. true; m. false; n. false 
2.

baseClass

derivedOne derivedTwo derivedThree

3. Some of the data members that can be added to the class employeeType are: department, salary,
employeeCategory (such as supervisor and president), and employeeID. Some the member functions
are: setInfo, getSalary, getEmployeeCategory, and setSalary.

4. The private members of a class are private; they cannot be directly accessed by the member functions of
the derived class. The protected members of the base class can be directly accessed by the member
functions of the derived class.

5. a. The statement :

class bClass public aClass

should be:

class bClass: public aClass

b. Missing semicolon after }.

6. a. false
b. (i) Valid.
(ii) Invalid: a is a private data member of the class. It cannot be directly accessed outside
the class. b is a private data member of the class. It cannot be directly accessed
outside the class.
(iii) Invalid: a and b cannot be accessed directly in class xClass.
(iv) Invalid: a, b, and z are private data members. They cannot be accessed
directly by the objects x and y.
7.
a.
yClass::yClass()
{
a = 0;
b = 0;
}
b.
xClass::xClass()
{
z = 0;
}
c.
void yClass::two(int u, int v)
{
a = u;
b = v;
}

8. The function setX is a protected member of the class classA. At Line 6, it cannot be directly accessed
by the object aObject.
9. a.
void two::setData(int a, int b, int c)
{
one::setData(a, b);
z = c;
}

b.
void two::print() const
{
one::print();
cout <<z << endl;
}

10.
2 This is base class
Derived class: 7
10 Hello Base

11.
In base: x = 7
In derived: x = 3, y = 8, x + y = 11
**** 7
#### 11

12. A function that is defined outside the scope of a class is called a friend function.
13. Because the left operand of << is a stream object, which is not of the type mystery.
14. One.
15.
a. friend istream& operator>>(istream&, strange&);
b. strange operator+(const strange&) const;
c. bool operator==(const strange&) const;
d. strange operator++(int);
16.
a. friend strange operator+(const strange&, const strange&);
b. friend bool operator==(const strange&, const strange&);
c. friend strange operator++(strange&, int);

17. In Line 3, the word operator before <= is missing. The correct statement is as follows:
bool mystery::operator<=(mystery rightObj) //Line 3
{

18. In Line 2, the word friend before the word bool is missing. The correct statement is:

friend bool operator <= (mystery, mystery); //Line 2


19. In Line 2, the function operator must have two parameters. The correct statement is as follows:

friend operator+ (mystery, mystery); //Line 2

20. None
21. One
22. One
23. Two

24. Error in Line 4. A template instantiation can be only for either a built-in type or user-defined type. The word
type between the angular brackets must be replaced either with a built-in type or user-defined type.

25.
a. strange<int> sObj;
b. bool operator==(strange);
c.
bool strange::operator==(strange right)
{
return(a == right.a && b = right.b);
}

26. (a) 12 (b) Sunny Day

27. (a) 21 (b) OneHow

28.
template <class Type>
void swap(Type &x, Type &y)
{
Type temp;
temp = x;
x = y;
y = temp;
}
Chapter 3
1. a. false; b. false; c. false; d. true; e. true; f. true; g. false; h. false
2. a. valid
b. valid
c. invalid (p is a pointer variable and x is an int variable. The value of x cannot be assigned to p.)
d. valid
e. valid
f. invalid (*p is an int variable and q is a pointer variable. The value of q cannot be assigned to *p.)
3.
98 98
98 98
4.
35 78
78 78
5. b and c
6. 39 39
7. 78 78
8. The statement in Line 5 copies the value of p into q. After this statement executes, both p and q point to the
same memory location. The statement in Line 7 deallocates the memory space pointed to by q, which in turn
invalidates both p and q. Therefore, the values printed by the statement in Line 8 are unpredictable.
9. 4 4 5 7 10 14 19 25 32 40
10. 10 15 20 25 30 35 40 45 50 55
11. In a shallow copy of data, two or more pointers points to the same memory space. In a deep copy of data, each
pointer has its own copy of the data.
12. The statement in Line 7 copies the value of p into q. After this statement executes, both p and q point to the
same array. The statement in Line 8 deallocates the memory space, which is an array, pointed to by p, which in
turns invalidates q. Therefore, the values printed by the statement in Line 10 are unpredictable.
13.
Array p: 5 7 11 17 25
Array q: 25 17 11 7 5
14.
2 3 4
4 5 6
6 7 8
8 9 10
15. The copy constructor makes a copy of the actual parameter data.
16. The copy constructor executes when a variable is passed by value, when a variable is declared and initialized
using another variable, and when the return value of a function is an object.
17. Classes with pointer data members should include the destructor, overload the assignment operator, and
explicitly provide the copy constructor by including it in the class definition and providing its definition.
18. a. const dummyClass& operator=(const dummyClass&);
b.

const dummyClass & dummyClass::operator=(const dummyClass& rightObject)


{
if (this != &rightObject) //avoids self-assignment
{
listLength = rightObject.listLength;
salary = rightObject.salary;
name = rightObject.name;

if (list !- NULL)
delete list;
list = new int[listLength];

for (int j = 0; j < listLength; j++)


list[j] = rightObject.list[j];
}

return *this;
}

19.

ClassA x: 4

ClassA x: 6
ClassB y: 5

20.
ClassA x: 4

ClassA x: 6
ClassB y: 10

21. In compile-time binding, the compiler generates the necessary code to call a function. In run-time binding, the
run-time system generates the necessary code to make the appropriate function call.

22.
public studentType: public personType
{
public:
virtual void print() = 0;
virtual void calculateGPA() = 0;
void setID(long id);
void setCourses(const string c[], int noOfC);
void setGrades(const char cG[], int noOfC);

void getID();
void getCourses(string c[], int noOfC);
void getGrades(char cG[], int noOfC);
void studentType(string fName = "", string lastName = "",
long id, string c[] = NULL,
char cG[] = NULL, int noOfC = 0);

private:
long studentId;
string courses[6];
char coursesGrade[6]
int noOfCourses;
}

23. a. The statement creates the arrayListType object intList of size 100. The elements of intList are
of the type int.
b. The statement creates the arrayListType object stringList of size 1000. The elements of
stringList are of the type string.
c. The statement creates the arrayListType object salesList of size 100. The elements of
salesList are of the type double.

24.

You might also like