0% found this document useful (0 votes)
86 views9 pages

California Baptist University, Online & Professional Studies

This document contains the homework assignment for Week 3 of EGR 221 - Data Structures in C++. It includes 22 multiple choice and coding questions related to object-oriented design principles, classes, inheritance, pointers, arrays, and dynamic memory allocation in C++. Students are instructed to type their answers after each question and submit the completed assignment.

Uploaded by

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

California Baptist University, Online & Professional Studies

This document contains the homework assignment for Week 3 of EGR 221 - Data Structures in C++. It includes 22 multiple choice and coding questions related to object-oriented design principles, classes, inheritance, pointers, arrays, and dynamic memory allocation in C++. Students are instructed to type their answers after each question and submit the completed assignment.

Uploaded by

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

EGR 221 --- Data Structures in C++

Week 3 Homework
California Baptist University, Online & Professional Studies

Please typeset your answers to the following homework problems (you may save this file as
week3hwk_your_lastname.doc and then directly type your answers after each problem). Submit
this assignment on Blackboard after you have finished by the due time.

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


2. Consider the Entry class shown in the Week 3 Day 1 Example Programs for Composition and
Polymorphism (which contains a class Name object name and a class Time object time as
private members). What is the output of the following client code?
#include "Entry.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
Entry entry1("John", "Walker", "McAlpine");
Entry entry2("Mary", "Beth", "Smith", 10, 30);

entry1.printTime();
cout << " " << entry1.getNameStr() << endl;
entry2.printTime();
cout << " " << entry2.getNameStr() << endl;
return 0;
}

3. Consider the Entry class shown in the Week 3 Day 1 Example Programs for Composition and
Polymorphism (which contains a class Name object name and a class Time object time as
private members). If we now want to add one more member function to overload the operator
== for the Entry class, which has the declaration as
bool operator==(const Entry& otherEntry) const;
write an implementation of this operator overload function.
4. What is the output of the following C++ program?
#include <iostream>
using namespace std;

class First
{
protected:
int a;
public:
First(int x = 1)
{ a = x; }

int getVal()
{ return a; }
};

class Second : public First


{

1
private:
int b;
public:
Second(int y = 5)
{ b = y; }

int getVal()
{ return b; }
};

int main()
{
First object1;
Second object2;

cout << object1.getVal() << endl;


cout << object2.getVal() << endl;
return 0;
}

5. What is the output of the following C++ program?


#include <iostream>
using namespace std;

class First
{
protected:
int a;
public:
First(int x = 1)
{ a = x; }

void twist()
{ a *= 2; }

int getVal()
{ twist(); return a; }
};

class Second : public First


{
private:
int b;
public:
Second(int y = 5)
{ b = y; }

void twist()
{ b *= 10; }
};

int main()
{
First object1;
Second object2;

cout << object1.getVal() << endl;


cout << object2.getVal() << endl;
return 0;
}

6. What is the output of the following C++ program?


#include <iostream>

2
using namespace std;

class First
{
protected:
int a;
public:
First(int x = 1)
{ a = x; }

virtual void twist()


{ a *= 2; }

int getVal()
{ twist(); return a; }
};

class Second : public First


{
private:
int b;
public:
Second(int y = 5)
{ b = y; }

virtual void twist()


{ b *= 10; }
};

int main()
{
First object1;
Second object2;

cout << object1.getVal() << endl;


cout << object2.getVal() << endl;
return 0;
}

7. What is the output of the following C++ program?


#include <iostream>
using namespace std;

class BaseClass
{
public:
BaseClass(int a = 1, int b = 2)
{ x = a; y = b; }
virtual void func1() const
{ cout << "x = " << x << " in BaseClass" << endl; }
void func2() const
{ cout << "y = " << y << " in BaseClass" << endl; }
protected:
int x;
int y;
};

class DerivedClass : public BaseClass


{
public:
DerivedClass(int a = 3, int b = 4)
:BaseClass(a,b)
{}

3
void func1() const
{ cout << "x = " << x << " in DerivedClass" << endl; }
void func2() const
{ cout << "y = " << y << " in DerivedClass" << endl; }
};

void print(BaseClass& s);

int main()
{
BaseClass baseObject;
DerivedClass derivedObject(7, 8);
print(baseObject);
print(derivedObject);
return 0;
}

void print(BaseClass& s)
{
s.func1();
s.func2();
}

8. What is the difference between compile-time binding and run-time binding?


9. Indicate the variable types of p and q for each of the following statements.
(a) int *p, *q;
(b) int* p, q;
(c) int *p, q;
(d) int p, *q;
10. What are the meanings of the following statements.
(a) const double *rates;
(b) int * const ptr;
(c) const int * const ptr;
11. Given the declaration
int x;
int *p;
int *q;
Mark the following statements as valid or invalid. If a statement is invalid, explain why.
(a)
p = q;
(b)
*p = 56;
(c)
p = x;
(d)
*p = *q;
(e)
q = &x;

4
(f)
*p = q;
12. What is the output of the following C++ code?
int x;
int y;
int *p = &x;
int *q = &y;
*p = 35;
*q = 98;
*p = *q;
cout << x << " " << y << endl;
cout << *p << " " << *q << endl;
13. What is the output of the following C++ code?
int x;
int y;
int *p = &x;
int *q = &y;
x = 35; y = 46;
p = q;
*p = 78;
cout << x << " " << y << endl;
cout << *p << " " << *q << endl;
14. Given the declaration
int num = 6;
int *p = &num;
which of the following statement(s) increment the value of sum?
(a)
p++;
(b)
(*p)++;
(c)
num++;
(d)
(*num)++;
15. Mark the following statements as true or false.
(a) In C++, pointer is a reserved word.
(b) In C++, pointer variables are declared using the reserved word pointer.
(c) The statement delete p; deallocates the variable pointer p.
(d) The statement delete p; deallocates the dynamic variable to which p points.
(e) Given the declaration
int list[10];
int *p;

5
the statement
p = list;
is valid in C++.
(f) Given the declaration
int *p;
the statement
p = new int[50];
dynamically allocates an array of 50 components of type int, and p contains the base
address of the array.
(g) The address of operator returns the address and value of its operand.
(h) If p is a pointer variable, the statement p = p * 2; is valid in C++.
16. What is the output of the following code?
int *p;
int * q;
p = new int;
q = p;
*p = 46;
*q = 39;
cout << *p << " " << *q << endl;
17. What is the output of the following code?
int *p;
int *q;
p = new int;
*p = 43;
q = p;
*q = 52;
p = new int;
*p = 78;
q = new int;
*q = *p;
cout << *p << " " << *q << endl;
18. What is wrong with the following code?
int *p; //Line 1
int *q; //Line 2

p = new int; //Line 3


*p = 43; //Line 4

q = p; //Line 5
*q = 52; //Line 6

delete q; //Line 7

cout << *p << " " << *q << endl; //Line 8

6
19. What is the output of the following code?
int x;
int *p;
int *q;
p = new int[10];
q = p;
*p = 4;
for (int j = 0; j < 9; j++)
{
x = *p;
p++;
*p = x + j;
}
for (int k = 0; k < 10; k++)
{
cout << *q << " ";
q++;
}
cout << endl;

20. Is the following program correct? If yes, what is the output of the program? If no, what is
wrong with the program?
#include <iostream>
using namespace std;

double * calcAvg(double *, int);

int main()
{
const int NO_OF_STUDENTS = 5;
double grade[NO_OF_STUDENTS] = {90, 90, 100, 80, 75.5};

double *ptr;
ptr = calcAvg(grade, NO_OF_STUDENTS);

cout << "The class average grade is: " << *ptr << endl;
delete ptr;
ptr = NULL;

return 0;
}

double * calcAvg(double * list, int size)


{
double * avgPtr;
avgPtr = new double;
double sum = 0;
for (int i = 0; i < size; i++)
sum += *(list+i);
*avgPtr = sum/size;
return avgPtr;
}

21. What is the output of the following code?


int *secret;

secret = new int[10];


secret[0] = 10;
for (int j = 1; j < 10; j++)
secret[j] = secret[j - 1] + 5;

7
for (int j = 0; j < 10; j++)
cout << secret[j] << " ";
cout << endl;
22. What is wrong with the following code?
int *p; //Line 1
int *q; //Line 2

p = new int [5]; //Line 3


*p = 2; //Line 4

for (int i = 1; i < 5; i++) //Line 5


p[i] = p[i-1] +i; //Line 6

q = p; //Line 7

delete [] p; //Line 8

for (int j = 0; j < 5; j++) //Line 9


cout << q[j] << " "; //Line 10

cout << endl; //Line 11


23. What is the output of the following code?
int *p;
int *q;

p = new int[5];
p[0] = 5;

for (int i = 1; i < 5; i++)


p[i] = p[i - 1] + 2 * i;

cout << "Array p: ";


for (int i = 0; i < 5; i++)
cout << p[i] << " ";
cout << endl;

q = new int[5];

for (int i = 0; i < 5; i++)


q[i] = p[4 - i];

cout << "Array q: ";


for (int i = 0; i < 5; i++)
cout << q[i] << " ";
cout << endl;
24. What is the output of the following code?
int **p;

p = new int* [5];

8
for (int i = 0; i < 5; i++)
p[i] = new int[3];

for (int i = 1; i < 5; i++)


for (int j = 0; j < 3; j++)
p[i][j] = 2 * i + j;

for (int i = 1; i < 5; i++)


{
for (int j = 0; j < 3; j++)
cout << p[i][j] << " ";
cout << endl;
}

You might also like