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 8.13 Multiple Choice Exercises
1.
Which of the following are compound values?
struct Student {
string firstName, lastName;
int year;
double gpa;
};
struct Professor {
string firstName, lastName;
string department;
int class;
};
int main() {
Student x = { "John", "Doe", 2, 3.46 };
Student y = { "Jane", "Doe", 3, 3.68 };
Professor z = { "Richard", "Roe", "Computer Science", 101 };
string college = "University of College";
int studentPop = 3400;
double avgGPA = 3.2;
}
x
is a Student
which is a struct
.
y
is a Student
which is a struct
.
z
is a Professor
which is a struct
.
college
is a string
which is made up of characters.
An int
is not a compound value.
A double
is not a compound value.
2.
What is wrong with the following
struct
definition?
struct Chicken {
string name;
int numLegs;
int eggs;
}
The word βstructβ needs to be capitalized.
βstructβ shouldnβt be capitalized in a struct
definition.
There needs to be a semicolon after the end curly brace.
It is a common error to forgot the semicolon at the end of struct
definitions.
A
struct
cannot have two instance variables of both
string
and
int
Instance variables of different types in a single struct is fine.
There is nothing wrong with the
struct
definition.
There is an error with the definition. Can you find it?
3.
How do we assign the value of 4 to the instance variable
numLegs
of the
Dog
object?
struct Dog {
string name;
int numLegs;
bool isPanting;
};
int main() {
Dog doug = { "Doug", 0, true };
}
The Dog
object is doug
. We can use the dot notation on an object.
Check the name of the instance variable in the struct
definition.
We can assign values to the instance variables of a struct
using dot notation.
Using dot notation on doug
, we can set the value of numLegs
to 4.
4.
What is the output of the code below?
struct Cube {
int edgeLength;
int volume;
int mass;
};
int main() {
Cube c;
c.edgeLength = 4;
c.volume = 64;
c.mass = 128;
cout << c.edgeLength << ", " << c.mass << ", " << c.volume << ", ";
int density = c.mass / c.volume;
cout << density;
}
Check the ordering of the output statements.
Take a closer look at the output statements.
The code outputs all instance variables and the density in the proper order.
edgeLength, volume, mass, density
Dot notation accesses the values of the instance variables, not the names.
5.
What is the output of the code below?
struct Cube {
int edgeLength;
int volume;
int mass;
};
int calculateDensity(Cube c) {
return c.mass / c.volume;
}
int main() {
Cube c;
c = (Cube) { 2, 8, 4 };
int density = calculateDensity(c);
cout << density;
}
Because of integer division, density
is 0 and thus the output is 0.
Density is mass divided by volume.
Take a closer look at what kind of division we are doing.
Integer division truncates the extra digits.
6.
What is the value of
s.coffeeCupFull
when the code is done running?
struct Student {
string name;
bool isSleepy;
bool coffeeCupFull;
};
void pourCoffee(Student s) {
s.coffeeCupFull = true;
}
int main() {
Student s = { "Thor Odinson", true, false };
if (s.isSleepy) {
pourCoffee (s);
}
}
C++ outputs boolean values as either a 0 or 1.
C++ outputs boolean values as either a 0 or 1.
Take a closer look at the function definition of pourCoffee
.
Since we pass a Student
object by value to pourCoffee
, the function makes a copy of the object and does not modify the original. If you wanted the original value to change, pass it by reference!
7.
What is the value of
r.batteryLevelPercentage
when the code is done running?
struct Robot {
string name;
int batteryLevelPercentage;
bool isFullyCharged;
};
void chargeRobot(Robot& r) {
if (r.batteryLevelPercentage + 50 > 100) {
r.batteryLevelPercentage = 100;
r.isFullyCharged = true;
}
else {
r.batteryLevelPercentage = r.batteryLevelPercentage + 50;
}
}
int main() {
Robot r = { "Rob", 60, false };
chargeRobot(r);
}
The Robot
object is passed by reference to chargeRobot
, which caps the batteryLevelPercentage
at 100.
Take a closer look at the chargeRobot
function.
Is the Robot
object passed by value or by reference to chargeRobot
?
That is the final value of r.isFullyCharged
.
8.
What is the output of the code below?
void foo (int& x, int y) {
x = x + 4;
y = 2 * x + 3 * y;
}
void bar(int x, int y) {
y = 2 * x;
x = x - 1;
foo (x, x);
}
void func(int &x, int& y) {
x = x + 3;
bar (y, x);
}
int main() {
int x = 4;
int y = 7;
func (y, x);
cout << x << ", " << y;
}
Take a closer look at func
and its parameters. Are they passed by value, passed by reference, or both?
Since bar
doesnβt pass either parameter by reference, neither bar
nor foo
affect the values of x
and y
.
Check the order of the arguments passed into func
.
Take a closer look at the three functions. Are they all passed by reference?
9.
If the user inputted the string βR2-D2β, what is the output of the code below?
int main() {
string name;
cin >> name;
cout << "Hello, " << name << "!";
}
Take another look at the cout
statement.
name
is not in quotes so the value stored in name
will be printed.
βR2-D2β is stored in name
and is then outputted in the cout
statement.
cin
reads input from the user.
10.
If the user inputted the string βC-3POβ, what is the output of the code below?
int main() {
char name;
cin >> name;
cout << "Hello, " << name << "!";
}
cin
reads the first char
in from user input.
Since βCβ is the first char
in the input, this is the correct output. The program will ignore everything that comes after the first char
.
Check the data type of name
.
Error, we cannot read a character from user input.
We can read characters from user input.
11.
If the user inputted the string βDarth Vaderβ, what is the output of the code below?
int main() {
string quote;
getline(cin, quote);
cout << quote << " is the epitome of Star Wars!";
}
quote is the epitome of Star Wars!
quote
is not in quotes so the value stored in quote
will be printed.
Darth Vader is the epitome of Star Wars!
getline reads the entire line until the user hits Return or Enter.
Darth is the epitome of Star Wars!
Check the manner in which the user input is acquired.
D is the epitome of Star Wars!
Try Again! Pay attention to the way in which user input is recieved.
You have attempted
of
activities on this page.