#include <iostream> #include \"Tree.h\" using namespace std; int main() { { Tree
ID: 3706549 • Letter: #
Question
#include <iostream>
#include "Tree.h"
using namespace std;
int main()
{
{
Tree A;
Tree B
}
Tree C;
Tree D:
}
What is the order in which the objects are destroyed?
_ is destroyed first.
_ is destroyed second.
_ is destroyed third.
_ is destroyed fourth.
D-)
1.. C
2.. D
3.. A
4.. B
A-)
1.. A
2.. B
3.. C
4.. D
C-)
1.. D
2.. C
3.. B
4.. A
B-)
1.. B
2.. A
3.. D
4.. C
Flag this Question
Question 21 pts
Programming assignment 8 involved a class named integerSet. One of the functions in this class was unionOfSets. Suppose the following code is in an application program that uses class integerSet:
const int SIZE1 = 5;
const int SIZE2 = 3;
int arr1[SIZE1] = {33, 55, 77, 99, 22};
int arr2[SIZE2] = {77, 26, 51};
IntegerSet a(arr1, SIZE1);
IntegerSet b(arr2, SIZE2);
IntegerSet c;
c = a.unionOfSets(b);
a.printSet();
b.printSet();
c.printSet();
And the output of this program is:
22 33 55 77 99
26 51 77
22 26 33 51 55 77 99
NOTE: set is the name of the vector member variable in the IntegerSet class. It is a vector of type bool (each index in the vector stores either true or false).
Which of the options below shows the correct implementation for the unionOfSets member function in the IntegerSet class?
IntegerSet& IntegerSet::unionOfSets( const IntegerSet &r ) const
{
for ( int i = 0; i < setSize; ++i )
set[ i ] = set[ i ] || r.set[ i ];
return *this;
}
IntegerSet IntegerSet::unionOfSets( const IntegerSet &r ) const
{
IntegerSet temp;
for ( int i = 0; i < setSize; ++i )
temp.set[ i ] = set[ i ] || r.set[ i ];
return temp;
}
IntegerSet& IntegerSet::unionOfSets( const IntegerSet &r ) const
{
IntegerSet temp;
for ( int i = 0; i < setSize; ++i )
temp.set[ i ] = set[ i ] || r.set[ i ];
return temp;
}
IntegerSet* IntegerSet::unionOfSets( const IntegerSet &r ) const
{
for ( int i = 0; i < setSize; ++i )
set[ i ] = set[ i ] || r.set[ i ];
return this;
}
Flag this Question
Question 31 pts
In programming assignment 7 one of the classes is DeckOfCards. One of the member variables in the DeckOfCards class is a vector named deck, which is of type Card:
class DeckOfCards
{
private:
vector<Card> deck;
}
The idea is that each index in the deck vector is storing an entire Card object. Each Card object in-turn stores two integers: one to represent the face of the card (the first integer), and one to represent the suit of the card (the second integer). For example, suppose the deck vector is initialized as follows:
deck[0] = Card(0, 0)
deck[1] = Card(0, 1)
deck[2] = Card(0, 2)
deck[3] = Card(0, 3)
....
deck[51] = Card(12, 3)
One of the member functions in the DeckOfCards class is the shuffle index. The shuffle function loops through each index of the deck vector, and on each iteration of the loop a number between 0 and 51 is randomly chosen. The Card object stored at index i of the deck vector is then swapped with the card object at the randomly chosen index.
For example, suppose the current index is 0 (i.e., i=0), and the randomly chosen index is 51. Then the Card objects stored at deck[0] and deck[51] will be swapped. The updated state of the deck vector is:
deck[0] = Card(12, 3)
deck[1] = Card(0, 1)
deck[2] = Card(0, 2)
deck[3] = Card(0, 3)
....
deck[51] = Card(0, 0)
Which of the options below is the correct implementation of the shuffle member function in the DeckOfCards class?
void DeckOfCards::shuffle()
{
Card temp(0, 0);
int random_index;
for (int i = 0; i < deck.size(); i++)
{
random_index = rand() % 52;
temp = deck[i];
deck[i] = deck[random_index];
deck[random_index] = temp;
}
}
void DeckOfCards::shuffle()
{
Card* tempptr = nullptr;
int random_index;
for (int i = 0; i < deck.size(); i++)
{
random_index = rand() % 52;
tempptr = &deck[i];
deck[i] = deck[random_index];
deck[random_index] = *temp;
}
}
void DeckOfCards::shuffle(DeckOfCards &d)
{
Card* tempptr = nullptr;
int random_index;
for (int i = 0; i < d.deck.size(); i++)
{
random_index = rand() % 52;
tempptr = &(d.deck[i]);
d.deck[i] = d.deck[random_index];
d.deck[random_index] = *temp;
}
}
void DeckOfCards::shuffle(DeckOfCards &d)
{
Card temp(0, 0);
int random_index;
for (int i = 0; i < d.deck.size(); i++)
{
random_index = rand() % 52;
temp = d.deck[i];
d.deck[i] = d.deck[random_index];
d.deck[random_index] = temp;
}
}
Flag this Question
Question 41 pts
Suppose there are two classes, Bedroom and House. Which of the options below shows the correct relationship that exists between these two classes?
class House
{
private:
vector<Bedroom*> bedroom;
}
class Bedroom
{
private:
vector<House*> house;
}
class Bedroom
{
private:
vector<House> house;
}
class House
{
private:
vector<Bedroom> bedroom;
}
Flag this Question
Question 51 pts
Suppose there is a class named Gas_Pedal, which represents a gas pedal in a car. The Gas_Pedal class has a member function named apply_gas_pedal, which takes in an integer (0 - 100) as an argument. The prototype of this member function is:
void apply_gas_pedal(int);
The integer passed to the function (0 - 100) represents the degree of force applied to the gas pedal (i.e., how hard the gas pedal is being pressed down). The larger the number, the more force is applied to the gas pedal. For example, apply_gas_pedal(0) represents the case were the gas pedal is not being pressed down on at all (i.e., the car is not moving). apply_gas_pedal(100) represents the case were the gas pedal is being pressed down all the way (i.e., the car is traveling at it's maximum speed).
Suppose there is also a class named Car. The Car class has a member function named increase_speed, which takes in an integer (0 - 100) as an argument. The prototype of this member function is:
void increase_speed(int);
The integer passed to the function (0 - 100) represents the degree to which the car's speed is increased. The larger the number, the more the car's speed is increased. For example, increase_speed(0) would not increase the car's speed at all (i.e., the car would continue traveling at it's current speed). increase_speed(100) would bring the car to it's maximum speed.
Which of the options below shows the correct relationship between the apply_gas_pedal and increase_speed functions from the Gas_Pedal and Car classes, respectively?
void Car::increase_speed(int amount)
{
gas_pedal->apply_gas_pedal(amount);
}
void Gas_Pedal::apply_gas_pedal(int amount)
{
car.increase_speed(amount);
}
void Car::increase_speed(int amount)
{
gas_pedal.apply_gas_pedal(amount);
}
void Gas_Pedal::apply_gas_pedal(int amount)
{
car->increase_speed(amount);
}
Flag this Question
Question 61 pts
One of the two classes in programming assignment 7 was the Card class. This class consisted of:
Data members face and suit of type int. These are index numbers for the faces and suits arrays described in step c below.
A constructor that receives two ints representing the face and suit and uses them to initialize the data members.
Two static arrays of strings representing the faces and suits. The faces array stores the strings “Ace”, “Deuce”, “Three”, “Four”, “Five”, “Six”, “Seven”, “Eight”, “Nine”, “Ten”, “Jack”, “Queen”, and “King”. The suits array stores the strings “Hearts”, “Diamonds”, “Clubs”, and “Spades”.
A toString function that returns the Card as a string in the form “face of suit”. For example, “Ace of Hearts”. You can use the + operator to concatenate strings.
So the class has the following private member variables: face (integer), suit (integer), faceNames (static array of type string), and suitNames (static array of type string). The static faceNames and suitNames arrays have the following values:
const string Card::faceNames[ FACES ] =
{ "Ace", "Deuce", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
const string Card::suitNames[ SUITS ] =
{ "Hearts", "Diamonds", "Clubs", "Spades" };
The idea is that each card object stores two numbers -- the face member variable is used as an index for the faceNames array, and the suit member variable is used as an index for the suitNames array. This is so that using all four member variables (face, suit, faceNames, and suitNames), the toString function can print out each card in the form of "face of suit". For example, "Ace of Hearts".
For example suppose we have the following card object:
Card c(1, 2); // first integer is the face, second integer is the suit
For this card object, face is 1 and suit is 2. Index 1 of the faceNames array is "Deuce", and index 2 of the suitNames array is "Clubs". Therefore, if we called the toString member function using this Card object, it would return the string "Deuce of Clubs".
Which of the options below is the correct implementation of the toString member function in the Card class?
string Card::toString(Card &c, int face, int suit) const
{
return c.faceNames[face] + " of " + c.suitNames[suit];
}
string Card::toString(Card &c) const
{
return c.faceNames[face] + " of " + c.suitNames[suit];
}
string Card::toString() const
{
return faceNames[face] + " of " + suitNames[suit];
}
string Card::toString(int face, int suit) const
{
return faceNames[face] + " of " + suitNames[suit];
}
Flag this Question
Question 71 pts
A non-const member function can call a const member function.
A const member function can call a non-const member function.
D-)
1. False
2. False
A-)
1. True
2. False
C-)
1. True
2. True
B-)
1. False
2. True
Flag this Question
Question 81 pts
Suppose a program has a Car class:
class Car
{
private:
string model;
};
Which of the options below is considered a dangerous address return?
D-) string* set_model(string);
C-) string& set_model(string);
A-) Car& set_model(string);
B-) Car* set_model(string);
Flag this Question
Question 91 pts
1. Static member variables in a class can be accessed in non-static const member functions.
2. Non-static member variables in a class can be accessed in static member functions.
1. False
2. False
1. False
2. True
1. True
2. False
1. True
2. True
Flag this Question
Question 101 pts
Suppose a program has the following Date class:
class Date
{
private:
int day;
int month;
int year;
};
Suppose we want to be able to use member functions set_day, set_month, and set_year in cascaded member function calls. In particular, in the application program it should be possible to do:
d.set_day(1)->set_month_(12)->set_year(1970);
Where d is a Date object.
Which of the options below contains the correct prototype for member function set_day?
C-) Date* set_day(int);
B-) int& set_day(int);
D-) Date& set_day(int);
A-) int* set_day(int);
D-)
1.. C
2.. D
3.. A
4.. B
Explanation / Answer
Answer: Firsr block will be destroyed and then the main
A-)
1.. A
2.. B
3.. C
4.. D
21)
ntegerSet& IntegerSet::unionOfSets( const IntegerSet &r ) const
{
IntegerSet temp;
for ( int i = 0; i < setSize; ++i )
temp.set[ i ] = set[ i ] || r.set[ i ];
return temp;
}
40)
void DeckOfCards::shuffle(DeckOfCards &d)
{
Card temp(0, 0);
int random_index;
for (int i = 0; i < d.deck.size(); i++)
{
random_index = rand() % 52;
temp = d.deck[i];
d.deck[i] = d.deck[random_index];
d.deck[random_index] = temp;
}
}
70)
string Card::toString() const
{
return faceNames[face] + " of " + suitNames[suit];
}
81)
D-) string* set_model(string);
D-) Date& set_day(int);
D-) string* set_model(string);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.