C++ Ch 4 Quiz

¡Supera tus tareas y exámenes ahora con Quizwiz!

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

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

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

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

True

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

True

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

True

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

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

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

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

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

a. 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

a. True

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

b. 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

b. False

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

b. curly braces { }

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

b. x <= y

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

c. 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. 1 1 0 1 b. None of these c. 0 1 1 0 d. 0 0 1 0 e. 1 0 0 1

c. 0 1 1 0

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

c. 1 1

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. 10 b. 9 c. 8 d. 6

c. 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"; Select one: a. 100 b. 10 c. 99 d. 0 e. All of these

c. 99

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

e. ERROR

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.

e. There is no way to predict the integer value of an expression in which the result is not false.

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

e. break

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.

if ( flag ) hours = 10;

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).

if ( grade < 0 || grade > 100 ) cout << "The grade is invalid!" << endl;

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.

if ( grade >= 0 && grade <= 100 ) cout << "The grade is valid!" << endl;

What is the value of the following expression? false || true

true

What is the value of the following expression? true || true

true

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

b. C++ is fun

Relational operators allow you to ________ operands.

compare

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.

if ( y == 0 ) x = 0; else x = 1;

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.

if ( y == 0 ) { x = 100; }

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 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

b. False

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\n"; c. if (code == "C") cout << "This is a check" << endl; d. if (code == C) cout << "This is a check" << endl;

b. if (code == 'C') cout << "This is a check\n";

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

c. assignment

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

Type the operator that performs the logical NOT operation.

!

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

!=

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

0

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

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

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

What is the value of the following expression? true && false 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;

False

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

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 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!

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.

a. That's a high score! This is a test 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

a. True

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

a. True

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

d. All of these

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

d. int e. char

The _____ of a variable is limited to the block in which it is declared.

scope

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

true

Given two bool variables x and y, are these two expressions equivalent? !( x && y ) and ( !x || !y )

true

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

True

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

True

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

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

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

a. You failed the test! You passed the test!

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

a. input validation


Conjuntos de estudio relacionados

Psych 1 Ch. 13: (Defining Psychological Disorders)

View Set

Athletic Training - Chapter Nine

View Set

Mental Health Test 3 Chapter 27 - Community Mental Health Nursing

View Set

Overview of Animal Reproduction and Development

View Set

Ch. 30: Classical Dichotomy and Monetary Neutrality

View Set