Hogan Chapter 4
Question 34 0.2 / 0.2 pts What is the value of donuts after the following code executes? int donuts = 10; if (donuts = 1) donuts = 0; else donuts += 2; 12 10 0 2
0
After the following code is executed what is assigned to a? int a, x = 10, y = 7; a = x >= y; 10 7 The string "x >= y" 1 0
1
What is the value of donuts after the following code executes? int donuts = 10; if (donuts != 10) donuts = 0; else donuts += 2; 12 10 0 2
12
Given the following code segment, what is the output after "result = "? int x = 1, y = 1, z = 1; y = y + z; x = x + y; cout << "result = " << (x < y ? y : x) << endl; 0 1 2 3
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 } ___________________________________________________ 6 8 10 9
8
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"; 100 10 99 0 All of the above
99
Input values should always be checked for Appropriate range Reasonableness Division by zero, if division is taking place All of these
All of these
The following code correctly determines whether x contains a value in the range of 0 through 100. if (x >= 0 && <= 100) True False
False
Whereas < is called a relational operator, x < y is called a(n)________________ Arithmetic operator Relative operator Relational expression Largeness test
Relational expression
If you place a semicolon after this if statement: if (x < y); The code will not compile The compiler will interpret the semicolon as a null statement. The if statement will always evaluate to false. All of the above
The compiler will interpret the semicolon as a null statement.
As a rule of style, when writing an if statement you should indent the conditionally-executed statements. 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; 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 checked. True False
True
If the sub-expression on the left side of the || operator is true, the expression on the right side will not be checked. True False
True
You should be careful when using the equality operator to compare floating point values because of potential round-off errors. True False
True
In C++ the = operator is used for equality assignment subtraction negation
assignment
What is the value of the following expression? true && false true false -1 +1
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; Nothing will be displayed false x true
false
This is a variable (usually of type boolean or integer) that is used to signal when a condition exists. relational operator arithmetic operator flag float
flag
When a program lets the user know that an invalid choice has been made, this is known as: input validation output correction compiler criticism output validation
input validation
These operators connect two or more relational expressions into one, or reverse the logic of an expression. relational logical irrational negation
logical
When an if statement is placed within the conditionally executed code of another if statement, this is known as: complexity overloading nesting validation
nesting
The ________ of a variable is limited to the block in which it is declared. precedence associativity scope branching ability
scope
This statement lets the value of a variable or expression determine where the program will branch to. switch select associative scope
switch
The default section of a switch statement performs a similar task as the _______ portion of an if/else if statement. conditional break trailing else otherwise
trailing else
What is the value of the following expression? false || true true false -1 +1
true
What is the value of the following expression? true && true true false -1 +1
true
What is the value of the following expression? true || true true false -1 +1
true
Which of the following expressions will determine whether x is less than or equal to y? x > y x =< y x <= y x >= y
x <= y
This operator takes an operand and reverses its truth or falsehood. || relational arithmetic !
!
Question 14 0.2 / 0.2 pts Assuming x is 5, y is 6, and z is 8, which of the following is false? x == 5; 7 <= (x + 2); z <= 4; (1 + x) != y; z >= 8; x >= 0; x <= (y * 2) 3, 4, 6, 7 are False Only 5 is False 3 and 4 are False All are False
3 and 4 are False
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 << "Soccer"; cout << " is "; cout << "fun" << endl; ________________________________________________ C++ is fun Soccer is fun C++ C++fun Soccerfun
C++ is fun
What will the following segment of code output? 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!"; A. You failed the test! B. You passed the test! C. You did poorly on the test! D. Both A and B
D. Both A and B
The default section is required in a switch statement. True False
False
When a relational expression is false, it has the value _____. One Zero zero, one, minus one less than zero
Zero
Question 29 0.2 / 0.2 pts 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? a. if code is equal to C cout << "This is a check\n"; b. if (code = "C") cout << "This is a check" << endl; c. if (code == 'C') cout << "This is a check\n"; d. if (code == C) cout << "This is a check" << endl;
c
Relational operators allow you to ____________ numbers. add multiply compare average
compare
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; } 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 1
0 1 1 0
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; default: total = total + 4; } cout << total << endl; 0 3 13 28
13
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 10 25 0 5
15
If you intend to place a block of statements within an if statement, you must place these around the block. Parentheses ( ) Square Brackets [ ] Double Quote Marks " " Curly Braces { }
Curly Braces { }
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; A. That's a high score! B. Congratulations! C. This is a test question! D. Both B and A E. Both A and C
E. Both A and C
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 120 20 This code will not compile
20
An expression that has any value other than 0 is considered true by an if statement. True False
True
Given that x = 2, y = 1, and z = 0, what will the following cout statement display? cout << "answer = " << (x || !y && z) << endl; answer = 0 answer = 1 answer = 2 answer = 3
answer = 1
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; 7 15 0 0 10 10 1 1
1 1
This operator is used in C++ to compare whether two values are equal. = >< !! ==
==
This operator is known as the logical OR operator. -- // # ||
||
This operator performs a logical NOT operation. -- ! <> ><
!
This operator represents logical AND. ++ || && @
&&