COP3330 Midterm 2 Exam Solutions: C++ Programming, Exams of Advanced Education

A comprehensive set of solutions to the cop3330 midterm 2 exam, covering key c++ programming concepts and object-oriented programming principles. It includes multiple-choice questions, code reading exercises, and detailed explanations for each answer. Particularly useful for students studying c++ programming and object-oriented programming concepts, offering insights into dynamic memory allocation, class design, operator overloading, and more.

Typology: Exams

2024/2025

Available from 02/08/2025

Smartsolutions
Smartsolutions 🇺🇸

3

(4)

18K documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COP3330 MIDTERM 2 EXAM WITH COMPLETE SOLUTIONS
100% VERIFIED!!
Which of the following declares proper storage for a dynamic array of dynamic c-style
strings? a. string myString; b. char* myString; c. string* myString; d. char** myString; -
ANSWER>>d. char** myString;
________ is the flag that tells g++ to give a specified name to an executable after linking.
a. -g b. -c c. -o d. -e - ANSWER>>c. -o
Which of the following is NOT an Automatic in C++? a. Default Constructor b. Destructor
c. Deep Copy Constructor d. Shallow Copy Assignment - ANSWER>>c. Deep Copy
Constructor
T/F When construction of an aggregated data member is not specified, the default
constructor of that datamember will be used for construction. - ANSWER>>T
T/F MyClass** contents, refers to a dynamically allocated array of one or more MyClass
objects. - ANSWER>>F
Section 2: Code Reading (20 points - 5 points each)class Inventory {friend ostream&
operator<<(ostream& o, const Inventory& i);private:Weapon* content;int capacity,
size;public:~Inventory(); //Question number 38Inventory(const Inventory& i); //Question
number 39Inventory();Inventory& operator=(const Inventory& i);void Add(const
Weapon& w);Weapon Remove(int i);Weapon& Get(int i);};Inventory::Inventory()
{capacity = 3;size = 0;content = new Weapon[capacity];}void Inventory::Add(const
Weapon& w) {if(capacity == size) {//sad pathcapacity += 7;Weapon* temp = new
Weapon[capacity];for(int i = 0; i < size; i++) {temp[i] = content[i];}delete []
content;content = temp;}//happy pathcontent[size] = w;size++;} How many weapons can
be added to the array called "content" when an Inventory object is first created? -
ANSWER>>3
Section 2: Reading Code (20 points - 5 points a piece)class Inventory {friend ostream&
pf3
pf4
pf5
pf8

Partial preview of the text

Download COP3330 Midterm 2 Exam Solutions: C++ Programming and more Exams Advanced Education in PDF only on Docsity!

COP 333 0 MIDTERM 2 EXAM WITH COMPLETE SOLUTIONS

1 00% VERIFIED!!

Which of the following declares proper storage for a dynamic array of dynamic c-style strings? a. string myString; b. char* myString; c. string* myString; d. char** myString; - ANSWER>>d. char** myString;

________ is the flag that tells g++ to give a specified name to an executable after linking. a. -g b. -c c. -o d. -e - ANSWER>>c. -o

Which of the following is NOT an Automatic in C++? a. Default Constructor b. Destructor c. Deep Copy Constructor d. Shallow Copy Assignment - ANSWER>>c. Deep Copy Constructor

T/F When construction of an aggregated data member is not specified, the default constructor of that datamember will be used for construction. - ANSWER>>T

T/F MyClass** contents, refers to a dynamically allocated array of one or more MyClass objects. - ANSWER>>F

Section 2: Code Reading (20 points - 5 points each)class Inventory {friend ostream& operator<<(ostream& o, const Inventory& i);private:Weapon* content;int capacity, size;public:~Inventory(); //Question number 38Inventory(const Inventory& i); //Question number 39Inventory();Inventory& operator=(const Inventory& i);void Add(const Weapon& w);Weapon Remove(int i);Weapon& Get(int i);};Inventory::Inventory() {capacity = 3;size = 0;content = new Weapon[capacity];}void Inventory::Add(const Weapon& w) {if(capacity == size) {//sad pathcapacity += 7;Weapon* temp = new Weapon[capacity];for(int i = 0; i < size; i++) {temp[i] = content[i];}delete [] content;content = temp;}//happy pathcontent[size] = w;size++;} How many weapons can be added to the array called "content" when an Inventory object is first created? - ANSWER>>

Section 2: Reading Code (20 points - 5 points a piece)class Inventory {friend ostream&

operator<<(ostream& o, const Inventory& i);private:Weapon* content;int capacity, size;public:~Inventory(); //Question number 38Inventory(const Inventory& i); //Question number 39Inventory();Inventory& operator=(const Inventory& i);void Add(const Weapon& w);Weapon Remove(int i);Weapon& Get(int i);};Inventory::Inventory() {capacity = 3;size = 0;content = new Weapon[capacity];}void Inventory::Add(const Weapon& w) {if(capacity == size) {//sad pathcapacity += 7;Weapon* temp = new Weapon[capacity];for(int i = 0; i < size; i++) {temp[i] = content[i];}delete [] content;content = temp;}//happy pathcontent[size] = w;size++;} After the maximum number of Weapon objects in content is reached, how many more are added to the contentarray? - ANSWER>>

Section 2: Reading Code (20 points - 5 points a piece)class Inventory {friend ostream& operator<<(ostream& o, const Inventory& i);private:Weapon* content;int capacity, size;public:~Inventory(); //Question number 38Inventory(const Inventory& i); //Question number 39Inventory();Inventory& operator=(const Inventory& i);void Add(const Weapon& w);Weapon Remove(int i);Weapon& Get(int i);};Inventory::Inventory() {capacity = 3;size = 0;content = new Weapon[capacity];}void Inventory::Add(const Weapon& w) {if(capacity == size) {//sad pathcapacity += 7;Weapon* temp = new Weapon[capacity];for(int i = 0; i < size; i++) {temp[i] = content[i];}delete [] content;content = temp;}//happy pathcontent[size] = w;size++;} What is the first declaration in the public section of the Inventory class declaration? - ANSWER>> The inventory destructor

Section 2: Reading Code (20 points - 5 points a piece)class Inventory {friend ostream& operator<<(ostream& o, const Inventory& i);private:Weapon* content;int capacity, size;public:~Inventory(); //Question number 38Inventory(const Inventory& i); //Question number 39Inventory();Inventory& operator=(const Inventory& i);void Add(const Weapon& w);Weapon Remove(int i);Weapon& Get(int i);};Inventory::Inventory() {capacity = 3;size = 0;content = new Weapon[capacity];}void Inventory::Add(const Weapon& w) {if(capacity == size) {//sad pathcapacity += 7;Weapon* temp = new Weapon[capacity];for(int i = 0; i < size; i++) {temp[i] = content[i];}delete [] content;content = temp;}//happy pathcontent[size] = w;size++;} What would the usage of the function marked (in comments) with question number 39 look like? - ANSWER>>Inventory inv1;

Inventory inv2 = inv1;

Section 2: Reading Code (20 points - 5 points a piece)class Inventory {friend ostream& operator<<(ostream& o, const Inventory& i);private:Weapon* content;int capacity, size;public:~Inventory(); //Question number 38Inventory(const Inventory& i); //Question number 39Inventory();Inventory& operator=(const Inventory& i);void Add(const

size--;

Return returnW;

}

Section 2: Reading Code (20 points - 5 points a piece)class Inventory {friend ostream& operator<<(ostream& o, const Inventory& i);private:Weapon* content;int capacity, size;public:~Inventory(); //Question number 38Inventory(const Inventory& i); //Question number 39Inventory();Inventory& operator=(const Inventory& i);void Add(const Weapon& w);Weapon Remove(int i);Weapon& Get(int i);};Inventory::Inventory() {capacity = 3;size = 0;content = new Weapon[capacity];}void Inventory::Add(const Weapon& w) {if(capacity == size) {//sad pathcapacity += 7;Weapon* temp = new Weapon[capacity];for(int i = 0; i < size; i++) {temp[i] = content[i];}delete [] content;content = temp;}//happy pathcontent[size] = w;size++;} Implement the insertion operator overload for Inventory that prints all weapons in the inventorypreceded by the index of that weapon. For example, the 2nd weapon should be printed as: "1. where<wea - ANSWER>>ostream& operator<< (ostream& o, const Inventory& inv) {

for(int i = 0; i < inv.size; i ++) {

o << i << ". " << inv.contents[i] << endl;

}

return o;

}

Section 2: Reading Code (20 points - 5 points a piece)class Inventory {friend ostream& operator<<(ostream& o, const Inventory& i);private:Weapon* content;int capacity, size;public:~Inventory(); //Question number 38Inventory(const Inventory& i); //Question number 39Inventory();Inventory& operator=(const Inventory& i);void Add(const Weapon& w);Weapon Remove(int i);Weapon& Get(int i);};Inventory::Inventory() {capacity = 3;size = 0;content = new Weapon[capacity];}void Inventory::Add(const Weapon& w) {if(capacity == size) {//sad pathcapacity += 7;Weapon* temp = new Weapon[capacity];for(int i = 0; i < size; i++) {temp[i] = content[i];}delete [] content;content = temp;}//happy pathcontent[size] = w;size++;} Give the declaration of a

  • operator overload that returns the result of concatenating the contents of one Inventory with the contents of another Inventory. - ANSWER>>Inventory Inventory::operator+(const Inventory& inv) {

Inventory retInv;

retInv.size = 0;

retInv.capacity = capacity + inv.capacity;

//note that you only have to do this delete if we assume that the default constructor

// allocates default space

delete [] retInv.content;

retInv.content = new Weapon[retInv.capacity];

for(int i = 0; i < size; i++) {

retInv.content[i] = content[i];retInv.size++;

}

for(int i = 0; i < inv.size; i++) {

retInv.content[retInv.size + i] = inv.content[i];

retInv.size++;

}

return retInv;

}

Bonus (5 points): Given a dynamic array of Shapes called myShapes of size 10 provide the algorithm for resizing the array to size 25. - ANSWER>>Shape* myShapes = new Shape[10];Shape* temp = new Shape[15];for(int i = 0; i < 10; i++) {

temp[i] = myShapes[i];

}

delete [] myShapes;

myShapes = temp;

_________ is an object-oriented programming that depicts a "has a" relationship between two classes. a. Interface b. Inheritance c. Aggregation d. CRUD - ANSWER>>c. Aggregation

To prevent the creation of a copy, an object may be passed into a function by ______ a. Value b. Reference c. Name d. Constructor - ANSWER>>b. Reference

n C++, ______ is the second phase of compilation and takes object code files to generate an executable. a. Linking b. Compilation c. Declaration d. Overriding - ANSWER>>a. Linking

T/F A copy constructor should be provided for a class when DMA is used. - ANSWER>>T

T/F Multiple class definitions can be put in a single header file. - ANSWER>>T

T/F A copy constructor should take two parameters by value. - ANSWER>>F

T/F The "this" keyword can be used in a friend function. - ANSWER>>F

T/F C++ classes can only have data members that are built-in types. - ANSWER>>F

T/F To be a resizable array in C++ it has to be located on the stack. - ANSWER>>F

T/F A copy constructor should always take a parameter by reference. - ANSWER>>T

T/F When returning an object by reference, the value is copied before returning from the function. - ANSWER>>F

T/F All pointers point to heap space. - ANSWER>>F

T/F The delete keyword can be used on an unallocated address in heap space. - ANSWER>>F

T/F Only arrays may be dynamically allocated. - ANSWER>>F

T/F The ampersand (&) operator returns a memory address to stack space. - ANSWER>>F

T/F The new keyword returns a pointer to safe heap space. - ANSWER>>T

T/F Statically allocated variables must be deleted with the "delete" keyword before they go out of scope. - ANSWER>>F

T/F Constructors and destructors are needed to declare for all classes - ANSWER>>F

T/F The expression *myPtr gives the address myPtr is stored at. - ANSWER>>F

T/F The expression delete [] myArray will deallocate the pointers stored in myArray, and the pointer myArray itself. - ANSWER>>F

T/F Statically allocated variables assigned to a dynamically allocated list will not fall out of scope until thearray is deallocated. - ANSWER>>F