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 3.13 Multiple Choice Exercises
Answer the following
Multiple Choice questions to assess what you have learned in this chapter.
1.
You want to spice up your resume before the career fair, so you decide to update your GPA using the program below. What is the GPA that you will have on display for future employers?
#include <iostream>
using namespace std;
int main() {
double GPA = 3.52;
int updatedGPA = int(GPA);
cout << "GPA: " << updatedGPA;
}
Its correct to think that your GPA will be rounded down, but what else happens when you convert from int
to double
?
Converting to an int
always rounds down to the nearest integer, so I do not recommend using type conversions to build your resumeβ¦ especially if youβre close to 4.0
.
Converting to an int will round your GPA, but not in the direction that you were hoping for⦠what else happens when you convert from int
to double
?
Converting to an int will round yor GPA, but not in the direction that you were hoping for.
No errors here! Type conversions are perfectly legal in C++!
2.
What is the value of x after the program executes?
#include <iostream>
using namespace std;
int main() {
int x = acos(-1);
}
If x
were a double, C++ would automatically round the value of pi to 15 decimal places.
If x
were a double, C++ would automatically round the value of pi to 15 decimal places.
Automatic type conversion will round the value of pi down to the nearest integer, but what else happens when we convert a double
to an int
?
The value of x should be 3, since automatic type conversion will round the value of pi down to the nearest integer. Are you sure this program compiles?
Whenever we use math functions, we must include the <cmath>
header file.
3.
Multiple Response Select all variables that have a
non-zero value after the decimal place. (3.1 has a
non-zero value, while 3.0 does not)
#include <iostream>
using namespace std;
int main() {
int a = 1.5;
double b = a + 1.5;
double c = 2.4;
double d = 1/5;
int e = c * c;
double f = int(c);
}
C++ performs automatic type conversion to round 1.5 down to the nearest integer.
Since a = 1
, we know that b = 2.5
, which is a non-zero decimal.
c
is a double
and has a non-zero decimal.
C++ performs integer division to round 1/5
down to the nearest integer. The value will be stored as 0
, not 0.2
.
c
squared may have a non-zero decimal, but automatic type conversion will round it down to the nearest integer before storing the value in e
.
int(c)
rounds c
down to the nearest integer before storing the value in f
.
4.
Multiple Response Which of the following would work as a function header (first line of a function).
This function header is missing a type.
string palindrome(word) {
The functionβs parameter is missing a type.
Correct! The function header has a type, empty parentheses, and a squiggly bracket.
char shiftThree(char letter)
This function header is missing a squiggly bracket {
.
Correct! The function header has a type, empty parentheses, and a squiggly bracket.
string friend(string name) {
friend
is a reserved keyword in C++.
5.
What is printed when the following code runs? Are there any errors?
#include <iostream>
using namespace std;
void giveCompliment() {
cout << "You are awesome!";
}
void giveInsult() {
insult = "You suck!";
}
int main() {
giveInsult();
}
The giveCompliment
function is not called in main
.
The giveInsult
function doesnβt cout
anything.
insult
data type is not defined.
Correct! insult
data type is not defined.
6.
Rachel and Monica are best friends. They write a function called
bestFriends
so that they announce this fact to the rest of their friends. What is printed when they run the code below? Are there any errors?
#include <iostream>
using namespace std;
void bestFriends(string a, string b) {
cout << a << " is best friends with " << b;
}
int main() {
string a = "Rachel";
string b = "Monica";
bestFriends(b, a);
}
"Monica is best friends with Rachel"
Correct! Although the function definition has a << " is best friends with " << b
, we call the function with variable b
as argument a
and variable a
as argument b
.
"Rachel is best friends with Monica"
You seem to be confusing your arguments and parameters!
The function couts
the values of the variables, not their names!
The function couts
the values of the variables, not their names!
There are no errors with this program!
7.
What is printed when the following code runs? Are there any errors?
#include <iostream>
using namespace std;
void greeting(string name) {
cout << "hello, " << name << "!";
}
void goodbye(string name) {
greeting(name);
cout << "!!";
}
int main() {
string hannah = "Hannah";
string anna = "Anna";
string louise = hannah;
hannah = anna;
anna = louise;
goodbye(anna);
}
Correct! The string βHannahβ is assigned to the variable louise
, then the value of louise
is assigned to the variable anna
. When goodbye(anna)
runs, anna
has the value βHannahβ.
The function couts
the value of the variable anna
not the variable name!
Is "Anna"
still the value of anna
?
The goodbye
function adds extra exclamation points.
We assigned the value of louise
to anna
. Is "Louise"
the value of louise
?
There are no errors with this program!
8.
Multiple Response Which of the following are legal function calls of
orderFood
?
#include <iostream>
using namespace std;
void orderFood(string food, int quantity) {
cout << "I'll have " << quantity << " " << food;
}
int main() {
string a = "wings";
string b = "sliders";
int c = 3;
double d = 8.64;
char e = 'p';
}
Correct! a
is a string and c
is an int.
Correct! Automatic type conversion will convert d to an int
.
e
has a character value, and this function takes a string .
Correct! Automatic type conversion will convert d to an int
.
You have to input your arguments in the correct order.
9.
What is printed when the following code runs? Are there any errors?
#include <iostream>
using namespace std;
void printWord(string w) {
cout << w << w;
}
int main() {
char a = 'a' + 5;
printWord (a);
}
'a'
is no longer the value of a
, and the function would print it more than once. Hint: think about the type of a
.
'f'
is the value of a, but the function would print it more than once. Hint: think about the type of a
.
'a'
is no longer the value of a
. Hint: think about the type of a
.
Hint: think about the type of a
.
printWord
takes a string, not a character, as an argument.
10.
How many local variables and parameters does
mult
have?
void mult(int a, int b, int c) {
int d = 7;
cout << a * b * c * d;
}
1 parameter, 3 local variables
Remember, the parameters are declared in the function definition, and the local variables are declared inside of the function.
2 parameters, 4 local variables
You can declare multiple variables at once! Also, remember that local variables are declared inside of the function.
2 parameters, 1 local variables
You can declare multiple variables at once!
3 parameters, 1 local variable
a
, b
, and c
are parameters declared in the function definition. d
is a local variable declared inside of the function.
3 parameters, 4 local variables
Remember that local variables are declared inside of the function.
11.
How many calls are made to
party
during the entire program?
#include <iostream>
using namespace std;
void party(int day_of_month, string address) {
cout << "party on "<<day_of_month<<" at "
<< address << endl;
}
void weekend(bool available) {
if(available==true) {
party(21,"Big house");
party(22,"CCTC");
} else {
cout << "sorry I have to study for ENGR101!" << endl;
}
}
int main() {
bool im_free=false;
party(25, "North campus");
weekend(im_free);
im_free=true;
party(25, "Central campus");
weekend(im_free);
return 0;
}
Take into account that weekend
only calls party
if a conditional is true!
weekend
can also call the function party
Correct! two calls by main
and two calls by weekend
One invocation of weekend
calls party
twice.
You have attempted
of
activities on this page.