C++ Ch 4 Quiz

25 July 2022
4.7 (114 reviews)
69 test answers

Unlock all answers in this set

Unlock answers (65)
question
The _____ of a variable is limited to the block in which it is declared.
answer
scope
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;
answer
This is false!
question
Relational operators allow you to ________ operands.
answer
compare
question
What is the outcome of the following expression? Your choices are true, false, 0, 1, or ERROR. ( 6 > 5 ) || ( 2 / 0 ) a. True b. False c. 0 d. 1 e. ERROR
answer
a. True
question
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 checkn"; b. if (code == 'C') cout << "This is a checkn"; c. if (code == "C") cout << "This is a check" << endl; d. if (code == C) cout << "This is a check" << endl;
answer
b. if (code == 'C') cout << "This is a checkn";
question
Given two bool variables x and y, are these two expressions equivalent? !( x && y ) and ( !x || !y )
answer
true
question
When a program lets the user know that an invalid choice has been made, this is known as: a. input validation b. output validation c. compiler criticism d. output correction e. None of these
answer
a. input validation
question
Without this statement appearing in a switch construct, the program "falls through" all of the statements below the one with the matching case expression. a. switch b. scope c. case d. exit e. break
answer
e. break
question
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;
answer
9
question
What is the value of the following expression? false || true
answer
true
question
An expression that has any value other than 0 is considered true by an if statement. True False
answer
True
question
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) True False
answer
True
question
What is the outcome of the following expression? Your choices are true, false, 0, 1, or ERROR. -5 + -1 == -(2 * 3) a. True b. False c. 0 d. 1 e. ERROR
answer
a. True
question
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; a. None of these b. 10 10 c. 1 1 d. 0 0 e. 7 15
answer
c. 1 1
question
As a rule of style, when writing an if statement you should indent the conditionally-executed statements, and enclose those statement in curly braces. True False
answer
True
question
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;
answer
False
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;
answer
This is true!
question
Type the operator that performs the "is not equal to" comparison.
answer
!=
question
What is the outcome of the following expression? Your choices are true, false, 0, 1, or ERROR. !( 5 % 3 = 2 ) a. True b. False c. 0 d. 1 e. ERROR
answer
e. ERROR
question
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;
answer
10
question
In C++ the = operator indicates: a. negation b. subtraction c. assignment d. equality e. None of these
answer
c. assignment
question
Both of the following if statements perform the same operation. if (sales > 10000) commissionRate = 0.15; if (sales > 10000) commissionRate = 0.15; True False
answer
True
question
Write a single-line if statement that prints the message "The grade is valid!" followed by an endl if the variable grade is in the range [0, 100] (that is, from zero to one hundred inclusive). Pro tip - put spaces around operators and parenthesis so that the Moodle grader will score your work correctly. The "single line" provision is because the Moodle grader won't accept a multi-line answer.
answer
if ( grade >= 0 && grade <= 100 ) cout << "The grade is valid!" << endl;
question
What is the value of donuts after the following code executes? int donuts = 10; if (donuts != 10) donuts = 0; else donuts += 2;
answer
12
question
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"; a. 99 b. 100 c. 0 d. All of these e. 10
answer
d. All of these
question
!(y < x) is the same as x >= y True False
answer
True
question
Consider the following code: int x = 5; int y = 6; int z = 8; Is the following expression true or false? x >= 0 && x <= y
answer
True
question
Which of the following expressions will determine whether x is less than or equal to y? a. x > y b. x <= y c. x =< y d. x >= y
answer
b. x <= y
question
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!"; 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
answer
a. You failed the test! You passed the test!
question
If the sub-expression on the left side of an && operator is false, the expression on the right side will not be evaluated. True False
answer
True
question
Write a single-line if statement that sets the variable hours equal to 10 if the bool variable flag is true. Pro tip - put spaces around operators and parenthesis so that the Moodle grader will score your work correctly. The "single line" provision is because the Moodle grader won't accept a multi-line answer.
answer
if ( flag ) hours = 10;
question
Write a single-line if / else statement that assigns 0 to x when y is equal to 0; otherwise it should assign 1 to x. Pro tip - put spaces around operators and parenthesis so that the Moodle grader will score your work correctly. The "single line" provision is because the Moodle grader won't accept a multi-line answer.
answer
if ( y == 0 ) x = 0; else x = 1;
question
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;
answer
15
question
Consider the following code: int x = 5; int y = 6; int z = 8; Is the following expression true or false? 2 != y && z != 4
answer
true
question
What is the value of the following expression? true || true
answer
true
question
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
answer
b. curly braces { }
question
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
answer
b. False
question
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
answer
b. False
question
The = and == operators perform the same operation when used within a Boolean expression. Select one: True False
answer
False
question
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.
answer
a. That's a high score! This is a test question!
question
What is the value of the following expression? false || true Select one: True False
answer
True
question
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
answer
True
question
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
answer
c. 0
question
Write a single-line if statement that assigns 100 to x when y is equal to 0. Pro tip - put spaces around operators and parenthesis so that the Moodle grader will score your work correctly. The "single line" provision is because the Moodle grader won't accept a multi-line answer.
answer
if ( y == 0 ) { x = 100; }
question
Write a single-line if statement that prints the message "The grade is invalid!" followed by an endl if the variable grade is outside the range [0, 100] (that is, not in the range from zero to one hundred inclusive).
answer
if ( grade < 0 || grade > 100 ) cout << "The grade is invalid!" << endl;
question
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
answer
b. False
question
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
answer
a. True
question
x >= y is the same as x > y && x = y Select one: True False
answer
False
question
What will the following program display? # include 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. 1 1 0 1 b. None of these c. 0 1 1 0 d. 0 0 1 0 e. 1 0 0 1
answer
c. 0 1 1 0
question
What is the value of the following expression? true && ( false || true ) Select one: True False
answer
True
question
x != y is the same as ( x > y || x < y ) Select one: True False
answer
True
question
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
answer
False
question
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;
answer
20
question
What is the value of the following expression? true && false Select one: True False
answer
False
question
Which line in the following program will cause a compiler error? 1 #include 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. 10 b. 9 c. 8 d. 6
answer
c. 8
question
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
answer
False
question
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; Select one: a. C++fun b. C++ is fun c. Soccer is fun d. C++ e. Soccerfun
answer
b. C++ is fun
question
What is the value of the following expression? true && false || true Select one: True False
answer
True
question
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. 10 c. 99 d. 0 e. All of these
answer
c. 99
question
What is the value of the following expression? true && true Select one: True False
answer
True
question
The expression following a case statement must have a __ or __ value. Choose two from the list below. Select one or more: a. float b. double c. string d. int e. char
answer
d. int e. char
question
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
answer
a. True
question
Consider the following code: int x = 5; int y = 6; int z = 8; Is the following expression true or false? x == 5 || y > 3
answer
True
question
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;
answer
3
question
You should not use the equality operator == to compare floating point values. Select one: True False
answer
True
question
When a relational expression is true, it has the value ________. Select one: a. one b. zero c. zero, one, or minus one d. less than zero e. There is no way to predict the integer value of an expression in which the result is not false.
answer
e. There is no way to predict the integer value of an expression in which the result is not false.
question
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
answer
True
question
The default section is required in a switch statement. Select one: True False
answer
False
question
Type the operator that performs the logical NOT operation.
answer
!