C++ From Control Structures through Objects Ch. 4

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Type the operator that performs the logical NOT operation.

!

Type the operator that performs the "is not equal to" comparison.

!=

Type the operator that performs the logical AND operation.

&&

Type the operator that performs the "greater than or equal to" comparison.

>=

The = and == operators perform the same operation when used within a Boolean expression. Select one: True False

False

The default section is required in a switch statement. Select one: True False

False

What is the outcome of the following expression? Your choices are true, false, 0, 1, or ERROR. !( 5 % 3 == 2 ) Select one: a. True b. False c. 0 d. 1 e. ERROR

False

What is the outcome of the following expression? Your choices are true, false, 0, 1, or ERROR. ( 6 < 5 ) && ( 2 * 3 ) Select one: a. True b. False c. 0 d. 1 e. ERROR

False

What is the outcome of the following expression? Your choices are true, false, 0, 1, or ERROR. -5 + -1 != -(2 * 3) Select one: a. True b. False c. 0 d. 1 e. ERROR

False

What is the outcome of the following expression? Your choices are true, false, 0, 1, or ERROR. 1 < 12 Select one: a. True b. False c. 0 d. 1 e. ERROR

False

What is the value of the following expression? true && false Select one: True False

False

!(y < x) is the same as x >= y Select one: True False

True

Consider the following code: int x = 5; int y = 6; int z = 8; Is the following expression true or false? 2 != y && z != 4 Select one: True False

True

Given two bool variables x and y, are these two expressions equivalent? !( x && y ) and ( !x || !y ) Select one: True False

True

What is the outcome of the following expression? Your choices are true, false, 0, 1, or ERROR. ( 6 > 5 ) || ( 2 / 0 ) Select one: a. True b. False c. 0 d. 1 e. ERROR

True

What is the outcome of the following expression? Your choices are true, false, 0, 1, or ERROR. -5 + -1 == -(2 * 3) Select one: a. True b. False c. 0 d. 1 e. ERROR

True

What is the outcome of the following expression? Your choices are true, false, 0, 1, or ERROR. 12 > 1 Select one: a. True b. False c. 0 d. 1 e. ERROR

True

You should not use the equality operator == to compare floating point values. Select one: True False

True

x != y is the same as (x > y || x < y) Select one: True False

True

If the sub-expression on the left side of an && operator is false, the expression on the right side will not be evaluated. Select one: True False

True If the outcome of a logical expression can be determined by the left-hand side, the right-hand side is not evaluated. This is called "short circuit evaluation" and most programming languages support this feature.

If the sub-expression on the left side of the "||" operator is true, the expression on the right side will not be evaluated. Select one: True False

True If the outcome of a logical expression can be determined by the left-hand side, the right-hand side is not evaluated. This is called "short circuit evaluation" and most programming languages support this feature.

Which statement allows you to properly check the char variable code to determine whether it is equal to a "C" and then output "This is a check" and then advance to a new line? Select one: a. if (code == "C") cout << "This is a check" << endl; b. if (code == C) cout << "This is a check" << endl; c. if (code == 'C') cout << "This is a check\n"; d. if code is equal to C cout << "This is a check\n";

if (code == 'C') cout << "This is a check\n"; The char variable code is denoted by single quotes 'C'.

What is the outcome of the following expression? Your choices are true, false, 0, 1, or ERROR. ( 5 % 3 == 2 )? 0 : 1 Select one: a. True b. False c. 0 d. 1 e. ERROR

0

What is the value of donuts after the following code executes? int donuts = 10; if (donuts = 1) donuts = 0; else donuts += 2;

0

What will the following program display? # include <iostream> using namespace std; int main( ) { int a = 0, b = 2, x = 4, y = 0; cout << (a == b) << " "; cout << (a != b) << " "; cout << (b <=x) << " "; cout << (y > a) << endl; return 0; } Select one: a. 0 1 1 0 b. 1 1 0 1 c. None of these d. 0 0 1 0 e. 1 0 0 1

0 1 1 0 C++ displays Boolean values as the integers 0 and 1. Newer languages are more refined and display the words false and true.

What will the following program segment display? int funny = 7, serious = 15; funny = serious % 2; if (funny != 1) { funny = 0; serious = 0; } else if (funny == 2) { funny = 10; serious = 10; } else { funny = 1; serious = 1; } cout << funny << " " << serious << endl; Select one: a. 10 10 b. None of these c. 1 1 d. 7 15 e. 0 0

1 1

What will be the value of result after the following code has been executed? int a = 60; int b = 15; int result = 10; if (a == b) result *= 2;

10

What is the value of donuts after the following code executes? int donuts = 10; if (donuts != 10) donuts = 0; else donuts += 2;

12

After execution of the following code, what will be the value of input_value if the value 0 is entered at the keyboard at run time? cin >> input_value; if (input_value > 5) input_value = input_value + 5; else if (input_value > 2) input_value = input_value + 10; else input_value = input_value + 15;

15

What will be the value of result after the following code has been executed? int a = 60; int b = 15; int result = 10; if (a = b) result *= 2;

20

Given the following code segment, what is output after "result = "? int x = 1, y = 1, z = 1; y = y + z; x = x + y; cout << "result = " << (x < y ? y : x) << endl;

3

Which line in the following program will cause a compiler error? 1 #include <iostream> 2 using namespace std; 3 4 int main( ) 5 { 6 int number = 5; 7 8 if (number >= 0 && <= 100) 9 cout << "passed.\n"; 10 else 11 cout << "failed.\n"; 12 return 0; 13 } Select one: a. 6 b. 9 c. 10 d. 8

8

What is the output of the following segment of code if 4 is input by the user when asked to enter a number? int num; int total = 0; cout << "Enter a number from 1 to 10: "; cin >> num; switch (num) { case 1: case 2: total = 5; case 3: total = 10; case 4: total = total + 3; case 8: total = total + 6; } cout << total << endl;

9

Which value can be entered to cause the following code segment to display the message "That number is acceptable." int number; cin >> number; if (number > 10 && number < 100) cout << "That number is acceptable.\n"; else cout << "That number is not acceptable.\n"; Select one: a. All of these b. 99 c. 10 d. 100 e. 0

99

Type the operator that performs the "less than" comparison.

<

Type the operator that performs the "less than or equal to" comparison.

<=

Type the operator that performs the assignment operation.

=

Type the operator that performs the is-equal-to or equality comparison.

==

Type the operator that performs the "greater than" comparison.

>

Which value can be entered to cause the following code segment to display the message "That number is acceptable." int number; cin >> number; if (number > 10 || number < 100) cout << "That number is acceptable.\n"; else cout << "That number is not acceptable.\n"; Select one: a. 100 b. 0 c. All of these d. 10 e. 99

All of these

What will the following segment of code output if 11 is entered at the keyboard? int number; cin >> number; if (number > 0) cout << "C++"; else cout << "C++"; cout << " is "; cout << "fun!" << endl; Select one: a. C++ is fun! b. C++isfun! c. C++ is fun!

C++ is fun!

What is the outcome of the following expression? Your choices are true, false, 0, 1, or ERROR. !( 5 % 3 = 2 ) Select one: a. True b. False c. 0 d. 1 e. ERROR

ERROR

Consider the following code: int x = 5; int y = 6; int z = 8; Is the following expression true or false? 7 <= x && z > 4 Select one: True False

False

Consider the following code: int x = 5; int y = 6; int z = 8; Is the following expression true or false? x >= 0 && x <= y Select one: True False

False

What will be the output of the following code segment after the user enters 0 at the keyboard? int x = -1; cout << "Enter a 0 or a 1 from the keyboard: "; cin >> x; if (x) cout << "true" << endl; else cout << "false" << endl; Select one: True False

False

x >= y is the same as x > y && x = y Select one: True False

False

The following code correctly determines whether x contains a value in the range [0, 100] (or zero through 100, inclusive). if (x >= 0 && <= 100) Select one: True False

False The code contains a subtle error. The "x" is missing from the right side of the "&&" operator.

What will the following segment of code output? score = 40; if (score > 95) cout << "Congratulations!\n"; cout << "That's a high score!\n"; cout << "This is a test question!" << endl; Select one: a. That's a high score! This is a test question! b. Congratulations! That's a high score! This is a test question! c. This is a test question! d. Congratulations! That's a high score! e. No output will be displayed.

That's a high score! This is a test question!

What will following segment of code output? int x = 5; if (x == 2) cout << "This is true!" << endl; else cout << "This is false!" << endl;

This is false!

What will following segment of code output? int x = 5; if (x = 2) cout << "This is true!" << endl; else cout << "This is false!" << endl;

This is true!

An expression that has any value other than 0 is considered true by an if statement. Select one: True False

True

Consider the following code: int x = 5; int y = 6; int z = 8; Is the following expression true or false? x == 5 || y > 3 Select one: True False

True

Given two bool variables x and y, are these two expressions equivalent? !( x || y ) and ( !x && !y ) Select one: True False

True

The following code correctly determines whether x contains a value in the range [0, 100] (or zero through 100, inclusive). if (x >= 0 && x <= 100) Select one: True False

True

What is the value of the following expression? false || true Select one: True False

True

What is the value of the following expression? true && ( false || true ) Select one: True False

True

What is the value of the following expression? true && false || true Select one: True False

True

What is the value of the following expression? true && true Select one: True False

True

What is the value of the following expression? true || true Select one: True False

True

Both of the following if statements perform the same operation. if (sales > 10000) commissionRate = 0.15; if (sales > 10000) commissionRate = 0.15; Select one: True False

True But it would be better still to enclose the body in curly braces and write: if (sales > 10000){ commissionRate = 0.15; }

As a rule of style, when writing an if statement you should indent the conditionally-executed statements, and enclose those statement in curly braces. Select one: True False

True You should always enclose the bodies of control structures in curly braces, even if the body is only one line of code.

What will the following segment of code output? You can assume the user enters a grade of 90 from the keyboard. cout << "Enter a test score: "; cin >> test_score; if (test_score < 60); cout << "You failed the test!" << endl; if (test_score > 60) cout << "You passed the test!" << endl; else cout << "You need to study for the next test!"; Select one: a. You failed the test! You passed the test! b. You failed the test! c. You passed the test! d. You failed the test! You did poorly on the test! e. None of these

You failed the test! You passed the test! This is one reason to use inline bracing, rather than the "open" style used in the textbook.

In C++ the = operator indicates: Select one: a. negation b. subtraction c. assignment d. equality e. None of these

assignment In assignment statements, the value or expression to the right is evaluated and stored in the location on the left.

Without this statement appearing in a switch construct, the program "falls through" all of the statements below the one with the matching case expression. Select one: a. exit b. case c. scope d. break e. switch

break The default behavior of switch statements is "fall thru" so we must use break to exit where desired.

The expression following a case statement must have a __ or __ value. Choose *two* from the list below. Select one or more: a. string b. float c. char d. double e. int

char & int You can also use bool!

Relational operators allow you to ________ operands. Select one: a. multiply b. compare c. add d. average e. None of these

compare The six relational operators compare two operands and return true or false based on that comparison.

If you intend to place a block of statements within an if statement, you must place these around the block. Select one: a. parentheses ( ) b. curly braces { } c. square brackets [ ] d. quotation marks " " e. None of these

curly braces { }

When a program lets the user know that an invalid choice has been made, this is known as: Select one: a. input validation b. output validation c. compiler criticism d. output correction e. None of these

input validation We'll do a lot of this when we get to the next chapter, when we can not only validate the input but use a loop to make the user try again if necessary.

Which of the following expressions will determine whether x is less than or equal to y? Select one: a. x > y b. x =< y c. x <= y d. x >= y

x <= y

Type the operator that performs the logical OR operation.

||


Kaugnay na mga set ng pag-aaral

Midterm - Microbiology and Parasitology

View Set

Human Anatomy & Physiology 1 / Exam 2 / Bones & Articulations

View Set