1. In C++, class members are private by default.

2.In C++, destructors may return a value.

3. In C++, pointers can be used as array names.

4. Public members of a public base class become protected members of the derived class.

5.A member function of a derived class may have the same name as a member function of the base class.

6. C++ allows you to pass an object of a derived class as an argument to a function where the parameter is of base class type.

7. A class containing one or more pure virtual functions is an abstract class and cannot be instantiated.

8. The compiler creates an instance of a function template in memory as soon as it encounters the function template definition.

9. In C++, linked lists are random access data structures.

10. The programmer does not need to know in advance how many nodes will be needed in a linked list.

11. A stack is a FIFO data structure.

12. A binary tree must be nonempty.

13. Here is a function prototype and some possible function calls:

double sumOfThreeNumbers(double = 1.0, double = 2.0, double = 3.0);
// Possible function calls:
cout << sumOfThreeNumbers ();
cout << sumOfThreeNumbers (2.3);
cout << sumOfThreeNumbers ( , 1.7, -3);
cout << sumOfThreeNumbers (2.3, , -3);

How many of the above function calls are legal?

14. Here is the start of a class declaration:

class ClassA
{
public:
void func1(const ClassA& a);
void func2(const ClassA& a) const;
void func3(ClassA a) const;
...

Which of the three member functions can alter the private member variables of the ClassA object that activates the function?

15. Given a C-string defined by

char name[8] = {'M', 'i', 'k', 'e', ''};

which of the following integers will be returned by strlen(name)?

16. Here is the prototype for a template function:

template < class Item>
void func(Item x);

Which is the right way to call the func function with a double argument a?

17.Name the three basic principles of object-oriented design (OOD).

18.What is the meaning of the following statement?

const double * const ptr;

19. Suppose you define a new class called NewClass. For two NewClass objects x and y, you would like to compare whether x is less than or equal to y using the expression x <= y. Write the prototype of the class member function that you must write to overload the operator <=.

20.Consider the following code segment.

class ClassY
{
public:
void one();
void two(int, int);
ClassY();
private:
int a;
int b;
};

class ClassX : public ClassY
{
public:
void one();
ClassX();
private:
int z;

};

ClassY y;
ClassX x;

(a)Write the definition of the default constructor of ClassY so that the private data members of ClassY are all initialized to 1.

(b)Write the definition of the default constructor of ClassX so that the private data member(s) of ClassX are initialized to 1.

(c)Write the definition of the member function two of ClassY so that the private data member a is set to the value of the first parameter of two, and the private data member b is set to the value of the second parameter of two.

21.Without actually executing the program in Visual C++, what do you expect the output of the following C++ program to be?

#include < iostream>
#include < string>
using namespace std;

class BaseClass
{
public:
void print() const;
BaseClass(string s = " ", int a = 0);
// Postcondition: str = s; x = a
protected:
int x;
private:
string str;
};

class DerivedClass : public BaseClass
{
public:
void print() const;
DerivedClass(string s = "", int a = 0, int b = 0);
// Postcondition: str = s; x = a; y = b
private:
int y;
};

int main()
{
BaseClass baseObject("This is base class");
DerivedClass derivedObject("This is derived class", 7, 9);
baseObject.print();
derivedObject.print();
return 0;
}

void BaseClass::print() const
{
cout << x << " " << str << endl;
}

BaseClass::BaseClass(string s, int a)
{
str = s;
x = a;
}

void DerivedClass::print() const
{
cout << "Derived class: " << y << endl;
BaseClass::print();
}

DerivedClass::DerivedClass(string s, int a, int b)
:BaseClass("Hello Base", a + b)
{
y = b;
}

22.What is the expected output of the following code?

int *p, *q;
p = new int;
q = p;
*q = 7;
q = new int;
*q = 11;
cout << *p << " " << *q << endl;

23.What is the expected output of the following code segment?

int *p;
p = new int[5];
*p = 2;
for (int i = 1; i < 5; i++)
*(p + i) = *(p + i - 1) + 2;
for (int j = 0; j < 5; j++)
cout << p[j] << " ";
cout << endl;

24. Given

int *listA, *listB;
listA = new int[4];
listA[0] = 2;
listA[1] = -1;
listA[2] = 3;
listA[3] = 5;

Write a code segment that implements deep copy of listB from listA.

25.Consider the following class declaration:

class Student
{
public:
Student(const char last[ ] = "", const char first[ ] = "",
const int id = 0 );

const Student& operator=
(const Student& otherStudent); // Overloaded operator=

... // Other member functions

private:
char *lastName;
char *firstName;
int ID;
};

Write the code for the definition (i.e., implementation) of the overloaded assignment operator. Hint: Here you may assume that the cstring header file has already been included, and consequently functions such as strlen, strcpy, strcat may be used . Their prototypes are shown as follows for your information:

size_t strlen ( const char * str );
char * strcpy ( char * destination, const char * source );
char * strcat ( char * destination, const char * source );

26.Consider the following declaration

template < class Type>
class Surprise
{
...
private:
Type a;
Type b;
};

(a)Write a statement that declares sObj to be an object of type Surprise such that the private data members a and b are of type int.

(b)Write a statement that shows the prototype in the class Surprise to overload the operator == as a member function.

(c)Assume that two objects of type Surprise are equal if and only if their corresponding data members are equal. Write the definition of the function operator == for the class Surprise, which is overloaded as a member function.

27.Consider the definition the following function template:

template < class Type>
Type func(Type list[], int size)
{
Type x = list[0];
Type y = list[size - 1];

for (int j = 1; j < size / 2; j++)
{
if (x < list[j])
x = list[j];
if (y > list[size - 1 - j])
y = list[size - 1 - j];
}

return x + y;
}

Further suppose that you have the following declarations:

int list[10] = {5,3,2,10,4,19,45,13,61,11};
string strList[] = {"One", "Three", "Five", "Two",
"Four", "Six"};

(a)What is the expected output of the following statement?

cout << func(list, 10) << endl;

(b)What is the expected output of the following statements?

cout << func(strList, 6) << endl;

28.Consider the linked list shown in the following figure. Assume that the nodes are in the usual info-next form. (Assume that first, p, q, and last are pointers to NodeType; NodeType is a struct consisting of an integer member variable info and a pointer variable next.) What are the outputs of the following statements?see image.

(a)cout << first->info;

(b)cout << q->next->next->next->info;

29.Consider the linked list given in Problem 28. Write a C++ code segment to insert a node containing the integer value 75 after the node containing the value 93. (You may introduce additional pointers to NodeType.)

30.Consider the linked list given in Problem 28. Write a C++ code segment to delete the node pointed by first. (Be sure to adjust first or last if necessary.)

31.What is the expected output of the following C++ code segment? Assume that the nodes are in the usual info-next form with the info of type int. (list and ptr are pointers to NodeType.)

list = new NodeType;
list->info = 20;
ptr = new NodeType;
ptr->info = 15;
ptr->next = NULL;
list->next = ptr;
ptr = new NodeType;
ptr->info = 28;
ptr->next = list->next;
list->next = ptr;
ptr = list;
while (ptr != NULL)
{
cout << ptr->info << endl;
ptr = ptr->next;
}

32.Using the SortedList class (sorted array-based list) given in Week 5 Day 3 Example Programs, without actually executing the program in Visual C++, what do you expect the output of the following program to be if we enter the 5 strings as:

Maryland Indiana Ohio Oregon California

#include < iostream>
#include < string>
#include "SortedList.h"
using namespace std;

int main()
{
SortedList< string> stringList;
string str;

cout << "Please enter 5 strings: ";
for (int counter = 0; counter < 5; counter++)
{
cin >> str;
stringList.insert(str);
}

for (int counter = 0; counter < stringList.getLength();
counter++)
cout << stringList[counter] << endl;

return 0;
}

33.Use the ArrayStackType class (implementation of stacks as arrays) given in Week 7 Day 3 Example Programs. Consider the following statements:

ArrayStackType< int> stack;
int x;

Suppose that the input is:

16 45 34 23 12 5 -999

Without actually executing the program in Visual C++, what do you expect the output of the following code segment to be?

cin >> x;
while ( x != -999 )
{
if ( x % 3 != 0 )
{
if ( !stack.isFull() )
stack.push(x);
}
else
cout << "x = " << x << endl;
cin >> x;
}

cout << "Stack Items: ";
while ( !stack.isEmpty() )
{
cout << " " << stack.top();
stack.pop();
}
cout << endl;

34.Use the LinkedStackType class (implementation of stacks as linked lists) given in Week 7 Day 3 Example Programs. Without actually executing the program in Visual C++, what do you expect the output of the following code segment to be?

LinkedStackType< int> resultStack;

int quotient = 29;
int remainder = 0;

while (quotient != 0)
{

remainder = quotient % 3;
quotient = quotient / 3;
resultStack.push(remainder);
}

while ( !resultStack.isEmpty() )
{
cout << resultStack.top() << " ";
resultStack.pop();
}

35.Use the LinkedQueueType class given in Week 7 Day 3 Example Programs. Without actually executing the program in Visual C++, what do you expect the output of the following code segment to be? LinkedQueueType queue;

queue.add(23);
queue.add(31);
cout << queue.front() << endl;
queue.remove();
queue.add(3 * queue.back());
queue.remove();
queue.add(2 * queue.front());
queue.add(queue.back() - 10);

LinkedQueueType< int> tempQueue;

tempQueue = queue;

while ( !tempQueue.isEmpty() )
{
cout << tempQueue.front() << " ";
tempQueue.remove();
}
cout << endl;
cout << queue.front() << " " << queue.back() << endl;

36.List the nodes of the following binary tree in an inorder sequence. see image.

37.Consider the following binary search tree. Delete the node containing 60 and redraw the binary search tree. (You may draw on a paper and then scan it or take a picture of it and insert the picture in your solutions file.) see image.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.