Skip to main content
Contents
Dark Mode Prev Up Next Scratch ActiveCode Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Exercises 12.12 Multiple Choice Exercises
1.
Select all of the true statements.
You can have a vector that stores a vector of objects.
C++ allows for a variety of different compositions.
In order to check to see if two
Card
s are equal, we can use the
==
operator.
We have to write a function that compares two Card
s.
There is no faster way to search through an unsorted vector than using a linear search.
If the vector
were sorted, then there are faster search methods.
There is no such thing as an empty object.
All variables are given default values unless otherwise specified by the user.
2.
What is the correct way to declare a
vector
of
vector
s of
int
s called
vec
?
This declares a vector
of int
s.
This is not the proper way to declare vec
.
Close! Look closely at the answer choices again.
vector<vector<int> > vec;
This is the proper way to declare a vector
of vector
s of int
s.
3.
What is the value of
card
?
struct Card {
int suit, rank;
Card();
Card(int s, int r);
};
Card::Card() {
suit = 0; rank = 0;
}
Card::Card(int s, int r) {
suit = s; rank = r;
}
int main() {
Card card (2, 8);
}
How did we define our mapping earlier in the chapter?
card
has a suit
value of 2 corresponding to Hearts, and a rank
value of 8.
How did we define our mapping earlier in the chapter?
card
does not have a value.
We initialized card
with a suit
value of 2 and a rank
value of 8.
4.
There is an error with the code below. Can you find it?
struct Card {
int suit, rank;
Card();
Card(int s, int r);
void print() const;
};
int main() {
Card card (1,3);
print (card);
}
card
is not a valid
Card
.
A suit
of 1 and a rank
of 3 maps to the 3 of Diamonds.
There shouldnβt be a semicolon after the
struct
definition.
A struct
definition always ends with a semicolon.
print
is a member function.
Since print
is a member function, we need to use the dot operator.
There is nothing wrong with the code.
There is an error with the code. Can you find it?
5.
What is the output of the code below?
struct Card {
int suit, rank;
Card();
Card(int s, int r);
void print() const;
bool isGreater(const Card& c2) const;
};
int main() {
Card card1 (2,12);
Card card2 (2,2);
cout << card1.isGreater (card2) << endl;
}
The output of a bool
is either a 0 or 1.
The output of a bool
is either a 0 or 1.
Is card1
greater than card2
?
The Queen of Hearts is greater than the 2 of Hearts.
6.
What is the output of the code below?
struct Card {
int suit, rank;
Card();
Card(int s, int r);
void print() const;
bool isGreater(const Card& c2) const;
};
vector<Card> buildDeck();
bool equals(const Card& c1, const Card& c2) {
return (c1.rank == c2.rank && c1.suit == c2.suit);
}
void printDeck(const vector<Card>& deck);
int find(const Card& card, const vector<Card>& deck);
int main() {
vector<Card> deck = buildDeck();
Card card (3, 13);
cout << find(card, deck);
}
The card
is the King of Spades, which is located at the end of the deck.
Since the vector
is size 52, it cannot have an index of 52.
What is the value of card
?
What is the value of card
?
7.
struct Card {
int suit, rank;
Card();
Card(int s, int r);
void print() const;
bool isGreater(const Card& c2) const;
};
vector<Card> createDeck() {
vector<Card> deck(12);
int i = 0;
for (int suit = 0; suit <= 3; suit++) {
for (int rank = 1; rank < 4; rank++) {
deck[i].suit = suit;
deck[i].rank = rank;
i++;
}
}
return deck;
}
int main() {
vector<Card> deck = createDeck();
}
createDeck
returns a vector
of size 12, corresponding to 12 Card
s.
The rank
goes up to but does not include 4.
There are no spades in the deck.
The suit
goes up to and include the suit
value 3 which corresponds to spades.
The
deck
has 3 cards in each suit.
Each suit has an Ace, 2, and 3.
8.
How many times does
findBisect
need to call itself in order to find the King of Diamonds?
struct Card {
int suit, rank;
Card();
Card(int s, int r);
void print() const;
bool isGreater(const Card& c2) const;
};
vector<Card> buildDeck();
bool equals(const Card& c1, const Card& c2);
void printDeck(const vector<Card>& deck);
int find(const Card& card, const vector<Card>& deck);
int findBisect(const Card& card, const vector<Card>& deck, int low, int high);
int main() {
vector<Card> deck = buildDeck();
Card card (1, 13);
cout << findBisect(card, deck, 0, 51);
}
The King of Diamonds is right in the middle of the deck, so it doesnβt need to call itself.
Where is the King of Diamonds located relative to the sorted deck?
Where is the King of Diamonds located relative to the sorted deck?
Where is the King of Diamonds located relative to the sorted deck?
9.
We want to write the function
findAllQueens
, which searches through a deck and prints out the location of all 4 queens in the
deck
. What should go in the blanks?
struct Card {
int suit, rank;
Card();
Card(int s, int r);
void print() const;
bool isGreater(const Card& c2) const;
};
vector<Card> buildDeck();
bool equals(const Card& c1, const Card& c2);
void printDeck(const vector<Card>& deck);
void findAllQueens(const vector<Card>& deck) {
for (size_t i = 0; i < deck.____; ++i) {
if (deck[i].____ == 12) {
cout << ____ << " ";
}
}
}
int main() {
vector<Card> deck = buildDeck();
findAllQueens (deck);
}
What value should i
go up to?
These are the correct variables and functions.
We want to print the index, not the card.
What value should i
go up to?
10.
What is the process of modeling a complex system with a simplified description in order to suppress unnecessary details while capturing relevant behavior?
Generalization means to take something specific and make it more general.
Encapsulation means taking a piece of code and wrapping it up in a function.
Using this process, we can remove unnecessary details to focus on the more important aspects.
Implementation is the process of taking an idea and making it real.
You have attempted
of
activities on this page.