Chapter 4
This operator takes an operand and reverses its truth or falsehood
!
Given the expression: A && B || C && !D Which of the following is evaluated first ?
!D
This operator represents the logical AND
&&
What is the value of donuts after the following statement executes?int donuts = 10; if (donuts = 1) donuts = 0; else donuts += 2;
0
When a relational expression is false, is has the value
0
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 values? result = x >= y;
1
What is the output of the following code? int w = 98; int x = 99; int y = 0; int z = 1; if (x >= 99) { if ( x < 99 ) cout << y << endl; else cout << z << endl; } else { if ( x == 99) cout << x << endl; else cout << w << endl; }
1
What will be displayed after the following statements execute? 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;
1 1
What is the value of donuts after the following statement executes?int donuts = 10; if (donuts != 10) donuts = 0; else donuts += 2;
12
What is the output of the following segment of code if the value 4 is input by the user? 44) int num; int total = 0; cout << "Enter a number from 1 to 10: "; cin >> num; switch (num) { case 1: case 2: case 3: case 4: case 8: default: total = total + 4; total = 5; total = 10; total = total + 3; total = total + 6; } cout << total << endl;
13
What is the value of the result after the following code executes? int a = 60; int b = 15; int result = 10; if (a = b) result *= 2;
20
Which value can be entered to cause the following cause that following code segments to display the message "That number is acceptable"? int number; cin >> number; if (number > 10 && number < 100) cout << "That number is not acceptable. \n"; else cout << "That number is not acceptable. \n";
99
The ______ is an equality (or comparison) operator.
==
Relational operators allow you to ________ numbers.
Compare
The conditional operator takes two operands.
False
The default section is required in a switch statement.
False
The following code correctly determines whether x contains a value in the range of 0 through 100, inclusive. if (x > 0 && <= 100)
False
The value of result in the following expression will be 0 if x has the value of 12. result = x > 100? 0 : 1;
False
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;
Hi, there!
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;
Nothing will be displayed
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;
That is true! That's all folks
An expression that has any value other than 0 is considered true by an if statement.
True
As a rule of style, when writing an if statement you should indent the conditionally- executed statements.
True
Both of the following if statements perform the same operations. 1. if (sales > 10000) commissionRate = 0.15; 2. if (sales >10000) commisionRate = 0.15;
True
If an if statement has an initialization clause, the initialization clause executes before the conditional expression is evaluated.
True
If the expression on the left side of the following is false, the expression on the right side will note be checked. (a >=b) && (c==d)
True
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)
True
You should be careful when using the equality operator to compare floating point values because of potential round-off errors.
True
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;
You passed the test.
Given the if/else statement if (a < 5 ) b = 12; else d = 30; which of the following performs the same operations?
a < 5 ? b = 12 : d = 30;
Input values should always be checked for
all of these
Given that x = 2, y = 1, z = 0, what will the following cout statement display?
answer = 1
If a switch statement has an initialization clause, when does the initialization clause execute?
before the conditional integer expression is evaluated
Without this statement appearing in a switch construct, the program "falls through" all of the statements below the one with the matching case expression.
break
If you intend to place a block of statements within an if statement, you must place ________ around the block.
curly braces { }
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;
false
What is the value of the following expression? true && false
false
A variable, usually a bool or an int, that signals when a condition exist is known as a(n)
flag
Which statements allow you to properly check the char variable code to determine whether it is equal to a C and then output This is a check?
if (code == 'C') cout << "This is a check\n";
When a program lets the user know that an invalid choice has been made, this is known as
input validation
Which line in the following program will cause a compiler error? 1 #include <iostream> 2 using namespace std; 3 int main() 4{ 5 int number = 5; 6 if (number >= 0 && <= 100) 7 cout << "passed.\n"; 8 else 9 cout << "failed.\n"; 10 return 0; 11 }
line 6
These operators connect two or more relational expressions into one, or reverse the logic of an expression.
logical
When an if statement is places within the conditionally- executed code of another if statement, this is known as
nesting
Whereas < is called a relational operator, x < y is called a(n)
relational expression
Given the following code segment, what is the output? int x = 1 , y = 1 , z 1; y = y + z ; x = x + y; cout << "result" = " << (x < y ? y :x ) <<endl;
result = 3
This statement uses the value of a variable or expression to determine where the program will branch to.
switch
If you place a semicolon after the statement: if (x<y)
the compiler will interpret the semicolon as a null statement
The default section of a switch statement performs a similar task similar to the _______ portion of an if/else if statement.
trailing else
What is the value of the following expression? true && !false
true
What is the value of the following expression? true && true
true
What is the value of the following expression? true || false
true
Which of the following expressions will determine whether x is less than or equal to y?
x <= y
THis operator represents the logical OR
| |