Programming C++ Chapter 4 Quiz
Input values should always be checked for:
Appropriate range, reasonableness, and division by zero, if division is taking place.
If you place a semicolon after the statement: if (x < y)
The compiler will interpret the semicolon as a null statement.
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
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
What will the following segment of code output if the value 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
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!";
You failed the test! You passed the test!
Relational operators allow you to ________ numbers.
compare
What is the value of the following expression? 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
This is a variable, usually a bool or an int, that signals when a condition exists.
flag
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
What is the output of the following segment of code if the value 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;
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
What is the value of the following expression? true || true
true
When a relational expression is false, it has the value ________.
zero