2.14. Multiple Choice ExercisesΒΆ

Answer the following Multiple Choice questions to assess what you have learned in this chapter.

Q1

Take a look at the following program. How many lines of output will be produced?

int main() {
  int hour = 7;
  int min = 50;
  cout << "The current time is: " << '\n';
  cout << hour;
  cout << ':';  cout << minute;
  cout << '\n';
  cout << "I'm going to be late for my 8am!";
}

Q2

What is the type of x?

x = "3";

Q3

What is the value of x?

x = 2 + (13 - 5) * 2 + 6 /3;

Q4

What is printed when the following code is run?

int main() {
  int x;
  int y = 2;
  int z = 4;
  x = z;
  z = 6;
  y = x + z;
  cout << y + z;
}

Q5

Multiple Response What could be changed so that the output of the following program is 34?

1int main() {
2  char c;
3  int d;
4  c = "3";
5  d = 4;
6  cout << c; cout << d;
7}

Q6

What is the output of the following code block?

int main() {
  string apples;
  apples = "bananas";
  string pineapple;
  string mango = "mango";
  pineapple = apples;
  cout << "My favorite fruit is ";
  cout << pineapple << '\n';
  pineapple = mango;
}

Q7

What line does the first error occur in the following program? If there is no error, what is the output?

1int main() {
2  string tom = "Tom";
3  string friend = "Jerry";
4  cout << tom;
5  cout << "is friends with"; cout << friend;
6}

Q8

Your math teacher just gave an exam that had all of the students panicking. Four students decide to share their scores to see who did the best. At the end of the program's execution, who has the highest score on the exam?

int main() {
  int Regina = 6 * (3 + 2) / 100;
  int Gretchen = (3 + 5) * 6 / 100;
  int Karen =  6 * 3 + 2 / 100;
  int Cady = (3 * 5) * 6 / 100;
}

Q9

What line does the first error occur in the following program? If there is no error, what is the output?

1int main() {
2  char r = 'r';
3  int x = 3;
4  r = r + x;
5  cout << r;
6}

Q10

Say you run the following code. What is the value of mod?

int x = 4;
int y = 7;
int mod = y % x;

Q11

Suppose you want to find the volume of a cone. For reference, the formula is \(V = \frac{1}{3}\pi \cdot r^2 \cdot h\). For the sake of this question, we will use \(\pi = 3.14\). What is wrong with the following code?

double r = 4.5;
double h = 12;
double volume = 1/3 * 3.14 * r * r * h;