Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Chapter 04 Starting out with C++ Making Decisions


 

Download  file with the answers

Not a member!
Create a FREE account here to get access and download this file with answers


 

Chapter Four

MULTIPLE CHOICE

1. Relational operators allow you to ____________ numbers.
a. add
b. multiply
c. compare
d. average
e. None of these

 

2. 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;

a. 15
b. 10
c. 25
d. 0
e. 5

 

3. 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; a. Nothing will be displayed b. false c. x d. true ANS: B 4. What is assigned to the variable a given the statement below with the following assumptions: x = 10, y = 7, and z, a, and b are all int variables. a = x >= y;

a. 10
b. 7
c. The string “x >= y”
d. 1
e. 0

 

5. If you place a semicolon after the statement if (x < y)
a. The code will not compile.
b. The compiler will interpret the semicolon as a null statement.
c. The if statement will always evaluate to false.
d. All of the above.
e. None of these.

 

6. When a relational expression is false, it has the value _____.
a. one
b. zero
c. zero, one, or minus one
d. less than zero
e. None of these

 

7. This is a variable, usually a boolean or an integer, that signals when a condition exists.
a. relational operator
b. arithmetic operator
c. flag
d. float
e. None of these

 

8. 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;

a. This is true!

b. This is false!

c. This is true!
This is false!

d. This is true!
This is all folks!

e. None of these

 

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

b. You passed the test!

c. You failed the test!
You passed the test!

d. You failed the test!
You did poorly on the test!

e. None of the above

 

10. When an if statement is placed within the conditionally-executed code of another if statement, this is known as:
a. complexity
b. overloading
c. nesting
d. validation
e. None of these

 

11. 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; a. 0 b. 3 c. 13 d. 28 e. None of these ANS: C 12. 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. This is a test question!

b. Congratulations!
That’s a high score!
This is a test question!

c. That’s a high score!
This is a test question!

d. Congratulations!
That’s a high score!

e. None of these

 

13. This operator represents the logical AND.
a. ++
b. ||
c. &&
d. @
e. None of these

 

14. This operator takes an operand and reverses its truth or falsehood.
a. ||
b. relational
c. arithmetic
d. !
e. None of these

 

15. Assuming x is 5, y is 6, and z is 8, which of the following is false?

1. x == 5;
2. 7 <= (x + 2);
3. z < = 4; 4. (1 + x) != y; 5. z >= 8;
6. x >= 0;
7. x <= (y * 2)

a. 3, 4, 6, 7 are False
b. Only 5 is False
c. 3 and 4 are False
d. All are False
e. None of these

 

16. Input values should always be checked for
a. Appropriate range
b. Reasonableness
c. Division by zero, if division is taking place
d. All of these
e. None of these

 

17. This statement lets the value of a variable or expression determine where the program will branch to.
a. switch
b. select
c. associative
d. scope
e. None of these

 

18. 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. break
b. exit
c. switch
d. scope
e. None of these

 

19. Whereas < is called a relational operator, x < y is called a(n)________________ a. Arithmetic operator b. Relative operator c. Relational expression d. Largeness test e. None of these ANS: C 20. This operator is used in C++ to represent equality. a. = b. >< c. !! d. == e. None of these ANS: D 21. In C++ the = operator indicates a. equality b. assignment c. subtraction d. negation e. None of these ANS: B 22. If you intend to place a block of statements within an if statement, you must place these around the block. a. parentheses ( ) b. square brackets [ ] c. quotation marks ? ? d. curly braces { } e. None of these ANS: D 23. 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;

a. C++ is fun

b. Soccer is fun

c. C++

d. C++fun

e. Soccerfun

 

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

 

25. When a program lets the user know that an invalid choice has been made, this is known as:
a. input validation
b. output correction
c. compiler criticism
d. output validation
e. None of these

 

26. These operators connect two or more relational expressions into one, or reverse the logic of an expression.
a. relational
b. logical
c. irrational
d. negation
e. None of these

 

27. 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;
}

a. 0 1 1 0
b. 0 0 1 0
c. 1 1 0 1
d. 1 0 0 1
e. None of these

 

28. This operator is known as the logical OR operator.
a. —
b. //
c. #
d. ||
e. None of these

 

29. This operator performs a logical NOT operation.
a. —
b. !
c. <>
d. ><
e. None of these

 

30. 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;

a. 0
b. 1
c. 2
d. 3
e. None of these

 

31. 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;

 

32. The ________ of a variable is limited to the block in which it is declared.
a. precedence
b. associativity
c. scope
d. branching ability
e. None of these

 

33. Given that x = 2, y = 1, and z = 0, what will the following cout statement display?

cout << “answer = ” << (x || !y && z) << endl; a. answer = 0 b. answer = 1 c. answer = 2 d. None of these ANS: B 34. The default section of a switch statement performs a similar task as the _______ portion of an if/else if statement. a. conditional b. break c. trailing else d. All of these e. None of these ANS: C 35. What is the value of donuts after the following code executes? int donuts = 10; if (donuts != 10) donuts = 0; else donuts += 2; a. 12 c. 0 b. 10 d. 2 ANS: A 36. What is the value of donuts after the following code executes? int donuts = 10; if (donuts = 1) donuts = 0; else donuts += 2; a. 12 c. 0 b. 10 d. 1 ANS: C 37. What is the value of the following expression? true && false a. true c. -1 b. false d. +1 ANS: B 38. What is the value of the following expression? true && true a. true c. -1 b. false d. +1 ANS: A 39. What is the value of the following expression? true || true a. true c. -1 b. false d. +1 ANS: A 40. What is the value of the following expression? false || true a. true c. -1 b. false d. +1 ANS: A 41. What is the output of the following code? int w = 98; int x = 99; int y = 0; int z = 1; if (x >= 99)
{
if (x < 99)
cout << y << endl;
else
cout << z << endl;
}
else
{
if (x == 99)
cout << x << endl;
else
cout << w << endl; } a. 98 c. 0 b. 99 d. 1 ANS: D 42. 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. 100 d. 0
b. 10 e. All of these
c. 99

 

43. 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 } a. 6 c. 10 b. 8 d. 9 ANS: B 44. 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

 

45. 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;

a. 10
b. 120
c. 20
d. This code will not compile

 

TRUE/FALSE

1. If the sub-expression on the left side of the || operator is true, the expression on the right side will not be checked.

2. The default section is required in a switch statement.

3. Both of the following if statements perform the same operation.

if (sales > 10000)
commissionRate = 0.15;

if (sales > 10000) commissionRate = 0.15;

4. You should be careful when using the equality operator to compare floating point values because of potential round-off errors.

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

6. If the sub-expression on the left side of an && operator is false, the expression on the right side will not be checked.

7. The following code correctly determines whether x contains a value in the range of 0 through 100.

if (x >= 0 && <= 100)

8. As a rule of style, when writing an if statement you should indent the conditionally-executed statements.

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!