3.15. Multiple Choice ExercisesΒΆ
Answer the following Multiple Choice questions to assess what you have learned in this chapter.
Q1
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>
int main() {
double gpa = 3.52;
int updated_gpa = int(gpa);
std::cout << "GPA: " << updated_gpa;
}
Q2
What is the value of x after the program executes?
#include <iostream>
int main() {
int x = acos(-1);
}
Q3
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)
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);
}
Q4
Multiple Response Which of the following would work as the first line of a function definition?
Q5
What is printed when the following code runs? Are there any errors?
#include <iostream>
using namespace std;
void give_compliment () {
cout << "You are awesome!";
}
void give_insult () {
insult = "You suck!";
}
int main () {
give_insult ();
}
Q6
Rachel and Monica are best friends. They write a function
called best_friends 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>
#include <string>
void best_friends (string a, string b) {
std::cout << a << " is best friends with " << b;
}
int main () {
std::string a = "Rachel";
std::string b = "Monica";
best_friends(b, a);
}
Q7
What is printed when the following code runs? Are there any errors?
#include <iostream>
#include <string>
void greeting (string name) {
std::cout << "hello, " << name << '!';
}
void goodbye (string name) {
greeting (name);
std::cout << "!!";
}
int main () {
std::string hannah = "Hannah";
std::string anna = "Anna";
std::string louise = hannah;
hannah = anna;
anna = louise;
goodbye (anna);
}
Q8
Multiple Response Which of the following are legal function
calls of order_food?
#include <iostream>
#include <string>
using namespace std;
void order_food (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';
}
Q9
What is printed when the following code runs? Are there any errors?
#include <iostream>
#include <string>
using namespace std;
void print (string w) {
cout << w << w;
}
int main () {
char a = 'a' + 5;
print (a);
}
Q10
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;
}
Q11
How many calls are made to party during the entire program?
void party (int day_of_month, string address) {
cout <<"party on "<<day_of_month<<" at "<<address<<'\n';
}
void weekend(bool available){
if(available) {
party(21,"Big house"); party(22,"CCTC");
} else {
cout<<"sorry I have to study for ENGR101!\n";
}
}
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;
}