Chapter 4 Quiz - Making Decisions

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

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;

1 1

This operator is known as the logical OR operator

||

This operator takes an operand and reverses its truth or falsehood.

!

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

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 }

8 (should be if (number >= 0 && *number* <= 100))

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

99 (any number from 11 to 99)

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

This is true! This is all folks! Since x = 2 uses the assignment operator (=) instead of the equals operator (==), it returns the integer 2, and when testing a numeric expression, a nonzero numeric value is considered true, and the value 0 is considered false. Because the else statement doesn't use brackets, only the first statement following is part of it, so "This is all folks!" will be output either way.

In C++ the = operator indicates:

assignment

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


Set pelajaran terkait

introduction to psychology, James W. Kalat Chapter 8

View Set

Dosage Calculation 3.0 Pediatric Medications Test

View Set

RE: Types of Loans, Terms, and Issues

View Set

Nature of Insurance, Risk, Perils, and Hazards

View Set