C++ Chapter 4

Ace your homework & exams now with Quizwiz!

After the following code executes, what is the value of my_value if the user enters 0? cin >> my_value; if (my_value > 5) my_value = my_value + 5; else if (my_value > 2) my_value = my_value + 10; else my_value = my_value + 15; a. 15 b. 10 c. 25 d. 0 e. 5

ANS: A

The __________ is an equality (or comparison) operator. a. == b. >= c. != d. = e. None of these

ANS: A

After the following code executes, what is the output if user enters 0? int x = -1; cout << "Enter a 0 or 1: "; cin >> x; if (c) -cout << "true" << endl; else -cout << "false" << endl; a.nothing will be displayed b. false c. x d. true e. 0

ANS: B

If you place a semicolon after the statement: if (x < y) a. the code will not compile b. the compiler will interpret the semicolon as a null statement c. the if statement will always evaluate to false d. All of these are true e. None of these

ANS: B

What is the output of the following code segment if the user enters 23? int number; cout << "Enter a number: "; cin >> number; if (number > 0) cout << "Hi, there!" << endl; else cout << "Good-bye." << endl; a. Hi, there!Good-bye. b. Hi, there! c. Good-bye. d. "Hi, there!" e. nothing will output

ANS: B

What is the output of the following code segment if the user enters 90 for the score? cout << "Enter your test score: "; cin >> test_score; if (test_score < 60) cout << "You failed the test." << endl; if (test_score > 60) cout << "You passed the test." else cout << "You need to study harder next time." << endl; a. Youfailedthetest. b. You passed the test. c. You need to study harder next time. d. You failed the test. You need to study harder next time. e. You passed the test. You need to study harder next time.

ANS: B

When a relational expression is false, it has the value a.1 b. 0 c. 0,1,or-1 d. -1 e. None of these

ANS: B

Relational operators allow you to __________ numbers. a. add b. multiply c. compare d. average e. None of these

ANS: C

Whereas < is called a relational operator, x < y is called a(n) a. arithmetic operator b. relative operator c. relational expression d. arithmetic expression e. None of these

ANS: C

What is assigned to the variable result given the statement below with the following assumptions: x = 10, y = 7, and x, result, and y are all int variables. result = x >= y; a. 10 b. 7 c. x >= y d. 1 e. 0

ANS: D

What is the output of the following code segment? -int x = 5; -if (x = 2) ---cout << "This is true!" << endl; -else ---cout << "This is false!" << endl; ---cout << "That's all, folks!" << endl; a. Thisistrue! b. This is false! c. This is false! That's all, folks d. This is true! That's all folks e. Thisistrue! This is false! That's all, folks!

ANS: D

The conditional operator takes two operands.

ANS: F

The default section is required in a switch statement.

ANS: F

The following code correctly determines whether x contains a value in the range of 0 through 100, inclusive. if (x > 0 && <= 100)

ANS: F

The value of result in the following expression will be 0 if x has the value of 12. result = x > 100 ? 0 : 1;

ANS: F

An expression that has any value other than 0 is considered true by an if statement.

ANS: T

As a rule of style, when writing an if statement you should indent the conditionally-executed statements.

ANS: T

Both of the following if statements perform the same operation. 1. if (sales > 10000) commissionRate = 0.15; 2. if (sales > 10000) commissionRate = 0.15;

ANS: T

If the expression on the left side of the following is false, the expression on the right side will not be checked. (a > = b) && (c == d)

ANS: T

If the expression on the left side of the following is true, the expression on the right side will not be checked. (a > = b) || (c == d)

ANS: T

You should be careful when using the equality operator to compare floating point values because of potential round-off errors.

ANS: T


Related study sets

NCEA Level 1 Science Acids & Bases

View Set

Chapter 8: Disorders of Fluid, Electrolyte, and Acid-Base Balance - ML4

View Set