CS Unit 2 (CH.4&5)

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is the outcome of the following expression? Your choices are true, false, 0, 1, or ERROR. 1 < 12

True

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

True

What is the value of the following expression? true && ( false || true )

True

The following code correctly determines whether x contains a value in the range [0, 100] (or zero through 100, inclusive). if (x >= 0 && <= 100) True or False?

False

The do / while loop is an example of a / an Select one: a. post-test loop b. mid-test loop c. infinite loop d. pre-test loop

a. post-test loop

The do / while loop is considered to be a / an Select one: a. post-test loop b. mid-test loop c. infinite loop d. pre-test loop

a. post-test loop

Type the operator that performs the logical NOT operation.

!

How many times will the following loop display "Hello"? for (int i = 0; i < 20; i++); cout << "Hello!" << endl;

1

What will the following code display? int x = 0; for (int count = 0; count <= 5; count++){ x += count; } cout << x << endl;

15

How many times will the following loop display "Hello"? for (int i = 0; i < 20; i++) cout << "Hello!" << endl;

20

How many times will the following loop display "Hello"? for (int i = 0; i <= 20; i++) cout << "Hello!" << endl;

21

What will the following code display? int x = 0; for (int count = 1; count <= 10; count+=2){ x += count; } cout << x << endl;

25

What will the following code display? int x = 0; for (int count = 0; count < 3; count++){ x += count; } cout << x << endl;

3

What will the following code display? int number = 41; number++; cout << number << endl;

42

What will the following code display? int number = 42; cout << number++ << endl;

42

What will the following code display? int number = 40; number++; cout << ++number << endl;

42

What will the following code display? int number = 42; cout << ++number << endl;

43

What will the following code display? int x = 0, count = 0; for (count = 0; count < 5 ; count++); { x += count; } cout << x << endl;

5

The body of a do / while loop will be executed zero or more times.

Correct. Since do / while is a post-test loop, the body will always be executed at least once. The correct answer is 'False'.

The body of a for loop will be executed at least once.

Correct. for is a pre-test loop; if the loop condition is initially false the body is not executed. The correct answer is 'False'.

The body of a for loop will be executed at least once.

False

The body of a while loop will be executed at least once.

False

You may nest while and do-while loops, but you may not nest for loops.

False

You may not use the break and continue statements within the same set of nested loops.

False

You may not use the break statement in a nested loop.

False

Which of these are pretest loops? Choose all that apply. Select one or more: a. do / while b. for c. if / else d. while

The correct answers are: for, while

Which of these are pretest loops? Choose all that apply. Select one or more: a. if / else b. do / while c. while d. for

The correct answers are: for, while

Which of these are pretest loops? Choose all that apply. Select one or more: a. while b. for c. do / while d. if / else

The correct answers are: for, while

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!

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

True

A do / while loop's body can contain multiple statements, as long as they are enclosed in braces.

True

A for loop's body can contain multiple statements, as long as they are enclosed in braces.

True

A while loop's body can contain multiple statements, as long as they are enclosed in braces.

True

If you want to stop a loop before it goes through all its iterations, the break statement may be used.

True

It is possible to define a file stream object and open a file in one statement.

True

The body of a do / while loop will be executed one or more times.

True

The body of a for loop will be executed zero or more times.

True

The body of a while loop will be executed zero or more times.

True

The scope of a variable declared in a for loop's initialization expression is limited to the body of the loop.

True

The update expression of a for loop can contain more than one statement, e.g. counter++, total+= sales.

True

What will the following loop display? int x = 0; while (x < 5) { cout << x << endl; x++; } Select one: a. 0 1 2 3 4 b. The loop will display numbers starting at 0, for infinity. c. 0 1 2 3 4 5 d. 0 1 2 3 4

a. 0 1 2 3 4

What is the output of the following code segment? n = 1; while (n < 5){ cout << n << ' '; n++; } Select one: a. 1 2 3 4 b. 1 2 3 4 5 c. 1 d. 1 1 1 1 1 1 1 1 1 ...

a. 1 2 3 4

A loop in which the test expression is evaluated after each repetition is a / an Select one: a. post-test loop b. infinite loop c. mid-test loop d. pre-test loop

a. post-test loop

A loop in which the body is executed zero or more times is a / an Select one: a. pre-test loop b. infinite loop c. post-test loop d. mid-test loop

a. pre-test loop

When a continue statement is encountered in a do / while loop, control is transferred to Select one: a. the loop condition b. the statement following the end of the do / while loop c. the next statement in the loop body d. continue does not have any effect on do / while loops

a. the loop condition

When a continue statement is encountered in a while loop, control is transferred to Select one: a. the loop condition b. the statement following the end of the while loop c. the next statement in the loop body d. continue does not have any effect on while loops

a. the loop condition

When a continue statement is encountered in a for loop, control is transferred to Select one: a. the loop update block b. the statement following the end of the for loop c. the next statement in the loop body d. continue does not have any effect on for loops

a. the loop update block Correct. continue jumps to the next iteration of the loop. In a for loop, that means go to the update block, then to the loop condition.

What is the output of the following code segment? n = 1; while (n <= 5){ cout << n << ' '; n++; } Select one: a. 1 2 3 4 b. 1 2 3 4 5 c. 1 d. 1 1 1 1 1 1 1 1 1 ...

b. 1 2 3 4 5

What is the output of the following code segment? n = 1; while (n <= 5){ cout << n << ' '; n++;} Select one: a. 1 2 3 4 b. 1 2 3 4 5 c. 1 d. 1 1 1 1 1 1 1 1 1 ...

b. 1 2 3 4 5

Which of these are pretest loops? Choose all that apply. Select one or more: a. if / else b. for c. while d. do / while

b. for c. while

while ( true ){ . . . } is one way to deliberately write a / an Select one: a. pre-test loop b. infinite loop c. post-test loop d. mid-test loop

b. infinite loop

A loop in which the body is executed at least once is a / an Select one: a. mid-test loop b. post-test loop c. pre-test loop d. infinite loop

b. post-test loop

A loop in which the test expression is evaluated after each repetition is a / an Select one: a. mid-test loop b. post-test loop c. infinite loop d. pre-test loop

b. post-test loop

The for loop is an example of a / an Select one: a. mid-test loop b. pre-test loop c. post-test loop d. infinite loop

b. pre-test loop

The while loop is considered to be a / an Select one: a. infinite loop b. pre-test loop c. mid-test loop d. post-test loop

b. pre-test loop

The while loop is considered to be a / an Select one: a. mid-test loop b. pre-test loop c. infinite loop d. post-test loop

b. pre-test loop

When a break statement is encountered in a do / while loop, control is transferred to Select one: a. the loop condition b. the statement following the end of the do / while loop c. the next statement in the loop body d. break does not have any effect on do / while loops

b. the statement following the end of the do / while loop

A loop in which the text expression is evaluated before each repetition is a / an Select one: a. infinite loop b. mid-test loop c. post-test loop d. pre-test loop

d. pre-test loop

When a break statement is encountered in a for loop, control is transferred to Select one: a. the for loop update block b. the statement following the end of the for loop c. the next statement in the loop body d. break does not have any effect on for loops

b. the statement following the end of the for loop

When a break statement is encountered in a while loop, control is transferred to Select one: a. the loop condition b. the statement following the end of the while loop c. the next statement in the loop body d. break does not have any effect on while loops

b. the statement following the end of the while loop

A loop that has no way of stopping is a / an Select one: a. mid-test loop b. post-test loop c. infinite loop d. pre-test loop

c. infinite loop

A loop in which the body is executed at least once is a / an Select one: a. mid-test loop b. pre-test loop c. post-test loop d. infinite loop

c. post-test loop

The while loop is considered to be a / an Select one: a. mid-test loop b. post-test loop c. pre-test loop d. infinite loop

c. pre-test loop

A for statement contains three expressions: initialization, test, and: Select one: a. null b. reversal c. update d. validation

c. update

What is the output of the following code segment? n = 1; while (n != 5) cout << n << ' '; n++; Select one: a. 1 2 3 4 b. 1 2 3 4 5 c. 1 d. 1 1 1 1 1 1 1 1 1 ...

d. 1 1 1 1 1 1 1 1 1 ...

What is the output of the following code segment? n = 1; while (n < 5) cout << n << ' '; n++; Select one: a. 1 2 3 4 b. 1 2 3 4 5 c. 1 d. 1 1 1 1 1 1 1 1 1 ...

d. 1 1 1 1 1 1 1 1 1 ...

What is the output of the following code segment? n = 1; while (n <= 5) cout << n << ' '; n++; Select one: a. 1 2 3 4 b. 1 2 3 4 5 c. 1 d. 1 1 1 1 1 1 1 1 1 ...

d. 1 1 1 1 1 1 1 1 1 ...

What is the output of the following code segment? n = 1; while (n < 5) cout << n << ' '; n++; Select one: a. 1 2 3 4 b. 1 2 3 4 5 c. 1 d. 1 1 1 1 1 1 1 1 1 ...

d. 1 1 1 1 1 1 1 1 1 ... Correct. The programmer forgot to enclose the loop body in braces. The update statement is never reached, resulting in an infinite loop!

When a continue statement is encountered in an if statement, control is transferred to Select one: a. the true clause of the if statement b. the false (or else) clause of the if statement c. the statement that follows the close of the if statement d. continue does not have any effect on if statements

d. continue does not have any effect on if statements

A loop that has no way of stopping is a / an Select one: a. post-test loop b. pre-test loop c. mid-test loop d. infinite loop

d. infinite loop

A loop in which the body is executed zero or more times is a / an Select one: a. mid-test loop b. post-test loop c. infinite loop d. pre-test loop

d. pre-test loop

A loop in which the text expression is evaluated before each repetition is a / an Select one: a. post-test loop b. mid-test loop c. infinite loop d. pre-test loop

d. pre-test loop

The for loop is considered to be a / an Select one: a. mid-test loop b. infinite loop c. post-test loop d. pre-test loop

d. pre-test loop

The while loop is an example of a / an Select one: a. infinite loop b. mid-test loop c. post-test loop d. pre-test loop

d. pre-test loop

A loop that is inside another loop is called: Select one: a. a pre-test loop b. an infinite loop c. a post-test loop d. a loop-de-loop e. a nested loop

e. a nested loop

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

!=

Type the operator that performs the logical AND operation.

&&

------------------------------

-------------------------------

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

0

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

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

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

Type the operator that performs the "greater than or equal to" comparison.

>=

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

x >= y is the same as x > y && x = y True or False?

False

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

Given two bool variables x and y, are these two expressions equivalent? !( x || y ) and ( !x && !y ) True or 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 or False?

True

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

True

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

True

You should not use the equality operator == to compare floating point values. True or False?

You can never really know if two floating-point values are equal right down to the last bit. A typical work around is something like this: if ( abs ( x - y ) < e ) where abs is the absolute-value function and e is some very small value. The condition is true if the two floating-point values differ by less than e. The correct answer is '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.

You should always enclose the bodies of control structures in curly braces, even if the body is only one line of code. The correct answer is 'True'.

What is the output of the following code segment? n = 1; while (n < 5) { cout << n << ' '; n++; } Select one: a. 1 2 3 4 b. 1 2 3 4 5 c. 1 d. 1 1 1 1 1 1 1 1 1 ...

a. 1 2 3 4

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. 99 b. 0 c. All of these d. 100 e. 10

a. 99

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. 99 b. 10 c. 100 d. 0 e. All of these

a. 99

What is the outcome of the following expression? Your choices are true, false, 0, 1, or ERROR. ( 6 > 5 ) || ( 2 / 0 ) 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 + -1 == -(2 * 3) 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 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 << "C++"; cout << " is "; cout << "fun!" << endl; Select one: a. C++ is fun! b. C++ is fun! c. C++isfun!

b. C++ is fun!

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 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? Select one: 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";

Without this statement appearing in a switch construct, the program "falls through" all of the statements below the one with the matching case expression. Select one: a. exit b. switch c. scope d. break e. case

d. break The default behavior of switch statements is "fall thru" so we must use break to exit where desired.

The do / while loop is considered to be a / an Select one: a. infinite loop b. pre-test loop c. mid-test loop d. post-test loop

d. post-test loop

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

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; Select one: a. 7 15 b. 10 10 c. 1 1 d. 0 0 e. None of these

11

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

How many times will the following loop display "Hello"? for (int i = 1; i < 20; i++) cout << "Hello!" << endl;

19

How many times will the following loop display "Hello"? for (int i = 0; i < 20; i++) cout << "Hello!" << endl;

20

How many times will the following loop display "Hello"? for (int i = 20; i > 0; i--) cout << "Hello!" << endl;

20

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

What will the following code display? int x = 0, count = 0; for (count = 0; count < 5 ; count++); { x += count; } cout << x << endl;

5

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

8

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

Type the operator that performs the "less than" comparison.

<

Type the operator that performs the "less than or equal to" comparison.

<=

Type the operator that performs the assignment operation.

=

Type the operator that performs the is-equal-to or equality comparison.

==

Type the operator that performs the "greater than" comparison.

>

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

False

The = and == operators perform the same operation when used within a Boolean expression. True or False?

False

The default section is required in a switch statement. True or False?

False

What is the outcome of the following expression? Your choices are true, false, 0, 1, or ERROR. ( 6 < 5 ) && ( 2 * 3 )

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!

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

True

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

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

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

True

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

True

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

True

You should not use the equality operator == to compare floating point values.

You can never really know if two floating-point values are equal right down to the last bit. A typical work around is something like this: if ( abs ( x - y ) < e ) where abs is the absolute-value function and e is some very small value. The condition is true if the two floating-point values differ by less than e. The correct answer 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!

Relational operators allow you to ________ operands. Select one: a. multiply b. compare c. add d. average e. None of these

b. compare

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

c. 0 1 1 0

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

c. assignment

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

c. assignment Correct. In assignment statements, the value or expression to the right is evaluated and stored in the location on the left.

Without this statement appearing in a switch construct, the program "falls through" all of the statements below the one with the matching case expression. Select one: a. switch b. scope c. break d. exit e. case

c. break Correct. The default behaviour of switch statements is "fall thru" so we must use break to exit where desired.

Which of these are posttest loops? Select one or more: a. if / else b. for c. do / while d. while

c. do / while do / while is a posttest loop

Which of the following expressions will determine whether x is less than or equal to y? Select one: a. x >= y b. x =< y c. x <= y d. x > y

c. x <= y

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. 99 b. 100 c. 10 d. 0 e. All of these

e. All of these

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

e. ERROR

The expression following a case statement must have a __ or __ value. Choose two from the list below. a. string b. char c. double d. int e. float

int, char

Type the operator that performs the logical OR operation.

||

If the sub-expression on the left side of an && operator is false, the expression on the right side will not be evaluated. True or False?

If the outcome of a logical expression can be determined by the left-hand side, the right-hand side is not evaluated. This is called "short circuit evaluation" and most programming languages support this feature. The correct answer is 'True'.

If the sub-expression on the left side of the || operator is true, the expression on the right side will not be evaluated. True or False?

If the outcome of a logical expression can be determined by the left-hand side, the right-hand side is not evaluated. This is called "short circuit evaluation" and most programming languages support this feature. The correct answer is 'True'.

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!

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

True

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

True

An initialization expression may be omitted from the for loop if no initialization is required.

True

An output file is a file that data is written to.

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!"; Select one: 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: Select one: a. input validation b. output validation c. compiler criticism d. output correction e. None of these

a. input validation

The for loop is considered to be a / an Select one: a. pre-test loop b. mid-test loop c. post-test loop d. infinite loop

a. pre-test loop


Ensembles d'études connexes

Western Civilization II - Quiz 4

View Set

PEDs Chapt 21 Nursing Care of the Child with a Genitourinary Disorder

View Set

chapter 18:Health Problems of the Adolescent-exam 3 peds

View Set

2. LinuxKernelDevelopmentProcess

View Set

Florida Civic Literacy Exam Sample

View Set