CS FINAL

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/401

encourage image

There's no tags or description

Looks like no tags are added yet.

402 Terms

1
New cards

If the expression on the left side of the following is true, the expression on the right side will not be checked. (a >= b) || (c == d)

True

2
New cards

If the expression on the left side of the following is false, the expression on the right side will not be checked. (a >= b) && (c == d)

True

3
New cards

The default section is required in a switch statement

False

4
New cards

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


True

5
New cards

Both of the following if statements perform the same operation. 1. if (sales > 10000) commissionRate = 0.15; 2. if (sales > 10000) commissionRate = 0.15;

True

6
New cards

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

True

7
New cards

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

True

8
New cards

The following code correctly determines whether x contains a value in the range of 0 through 100, inclusive. if (x > 0 && <= 100)

False

9
New cards

The value of result in the following expression will be 0 if x has the value of 12. result = x > 100 ? 0 : 1;

False

10
New cards

The conditional operator takes two operands

False

11
New cards

Relational operators allow you to __________ numbers.

compare

12
New cards

After the following code executes, what is the value of my_value if the user enters 0? cin >> my_value;

if (my_value > 5)

my_value = my_value + 5;

else if (my_value > 2)

my_value = my_value + 10;

else

my_value = my_value + 15;

15

13
New cards

After the following code executes, what is the output if user enters 0?

int x = -1; // Note: assuming the typo "-1;" was meant to be -1

cout << "Enter a 0 or 1: ";

cin >> x;

if (x) // Note: assuming the typo 'c' was meant to be 'x'

cout << "true" << endl;

else

cout << "false" << endl;


false

14
New cards

What is assigned to the variable result given the statement below with the following assumptions: x = 10, y = 7, and x, result, and y are all int variables.

result = x >= y;

1

15
New cards

If you place a semicolon after the statement

if (x < y)

the compiler will interpret the semicolon as a null statement

16
New cards

When a relational expression is false, it has the value

0

17
New cards

What is the output of the following code segment?

int x = 5;

if (x = 2)

cout << "This is true!" << endl;

else

cout << "This is false!" << endl;

cout << "That's all, folks!" << endl;


This is true! That's all folks

18
New cards

What is the output of the following code segment if the user enters 90 for the score?

cout << "Enter your test score: ";

cin >> test_score;

if (test_score < 60)

cout << "You failed the test." << endl;

if (test_score > 60)

cout << "You passed the test."

else

cout << "You need to study harder next time." << endl;


You passed the test.

19
New cards

Whereas < is called a relational operator, x < y is called a(n)

relational expression

20
New cards

The __________ is an equality (or comparison) operator

==

21
New cards

What is the output of the following code segment if the user enters 23?

int number;

cout << "Enter a number: ";

cin >> number;

if (number > 0)

cout << "Hi, there!" << endl;

else

cout << "Good-bye." << endl;


Hi, there!

22
New cards

What will be displayed after the following statements execute?

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

23
New cards

What is the value of donuts after the following statement executes?

int donuts = 10;

if (donuts != 10)

donuts = 0;

else

donuts += 2;

12

24
New cards

What is the value of donuts after the following statement executes?

int donuts = 10;

if (donuts = 1)

donuts = 0;

else

donuts += 2;

0

25
New cards

Which of the following expressions will determine whether x is less than or equal to y?

x <= y

26
New cards

What is the value of result after the following code executes?

int a = 60;

int b = 15;

int result = 10;

if (a = b)

result *= 2;

20

27
New cards

When an if statement is placed within the conditionally-executed code of another if statement, this is known as

nesting

28
New cards

If you intend to place a block of statements within an if statement, you must place __________ around the block:

curly braces { }

29
New cards

A variable, usually a bool or an int, that signals when a condition exists is known as a(n)

flag

30
New cards

This operator represents the logical AND:

&&

31
New cards

This operator represents the logical OR:

||

32
New cards

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

!

33
New cards

What is the value of the following expression?

true && false

False

34
New cards

What is the value of the following expression?

true && true

true

35
New cards

What is the value of the following expression?

true || false

true

36
New cards

What is the value of the following expression?

true && !false

true

37
New cards

These operators connect two or more relational expressions into one, or reverse the logic of an expression.

logical

38
New cards

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

39
New cards

Which of the following is evaluated first, given the expression: A && B || C && !D

!D

40
New cards

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;

}


1

41
New cards

Which line in the following program will cause a compiler error?

1 #include <iostream>

2 using namespace std;

3 int main()

4 {

5 int number = 5;

6 if (number >= 0 && <= 100)

7 cout << "passed.\n";

8 else

9 cout << "failed.\n";

10 return 0;

11 }


line 6

42
New cards

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

cout << "answer = " << (x || !y && z) << endl;

answer = 1

43
New cards

What is the output of the following segment of code if the value 4 is input by the user?

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

44
New cards

This statement uses the value of a variable or expression to determine where the program will branch to.

switch

45
New cards

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

46
New cards

The default section of a switch statement performs a similar task similar to the __________ portion of an if/else if statement

trailing else

47
New cards

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?

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

48
New cards

Given the following code segment, what is the output?

int x = 1, y = 1, z = 1;

y = y + z;

x = x + y;

cout << "result = " << (x < y ? y : x) << endl;

result = 3

49
New cards

Given the if/else statement, Which of the following performs the same operation?

if (a < 5)

b = 12;

else

d = 30;

a < 5 ? b = 12 : d = 30;

50
New cards

When a program lets the user know that an invalid choice has been made, this is known as:

input validation

51
New cards

Input values should always be checked for

All of these

52
New cards

The increment and decrement operators can be used in mathematical expressions; however, they cannot be used in relational expressions.

False

53
New cards

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

True

54
New cards

A while loop is somewhat limited because the counter can only be incremented by one each time through the loop.

False

55
New cards

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

True

56
New cards

The scope of a variable declared in a for loop's initialization expression always extends beyond the body of the loop.

False

57
New cards

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

True

58
New cards

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

False

59
New cards

The condition that is tested by a while loop must be enclosed in parentheses and terminated with a semicolon

False

60
New cards

The update expression of a for loop can contain more than one statement, for example: for(i = 5; i <= 10; i++, total+= sales)

True

61
New cards

Multiple relational expressions cannot be placed into the test condition of a for loop.

False

62
New cards

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

False

63
New cards

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

False

64
New cards

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

True

65
New cards

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

True

66
New cards

In C++ 11 you can pass a string object as an argument to a file stream object's open member function.

True

67
New cards

string objects have a member function named c_str that returns the contents of the object formatted as a null-terminated C-string.

True

68
New cards

These are operators that add and subtract one from their operands.

++ and --

69
New cards

This operator increments the value of its operand and then uses the value in context

prefix increment

70
New cards

This is a control structure that causes a statement or group of statements to repeat

loop

71
New cards

The two important parts of a while loop are the expression that is tested for a true or false value and

a statement or block that is repeated as long as the expression is true

72
New cards

Something within a while loop must eventually cause the condition to become false or a(n) __________ results.

infinite loop

73
New cards

The while loop is a __________ loop

pre-test

74
New cards

If you place a semicolon after the test expression in a while loop, it is assumed to be a(n)

null-statement

75
New cards

The statements in the body of a while loop may never be executed while the statements in the body of a do-while loop will be executed

at least once

76
New cards

When the increment operator precedes its operand, as ++num, the expression is in __________ mode.

prefix

77
New cards

This means to increase a value:

increment

78
New cards

A statement that may be used to stop a loop's current iteration and begin the next one is

continue

79
New cards

A statement that causes a loop to terminate early is

break

80
New cards

A variable that is regularly incremented or decremented each time a loop iterates is a

counter

81
New cards

A special value that marks the end of a list of values is a

sentinel

82
New cards

What is the output of the following code segment?

n = 1;

while (n <= 5)

cout << n << ' ';

n++;

1 1โ€ฆ. and on forever

83
New cards

In the following statement, which operator is used first?

while (x++ < 10)

<

84
New cards

while (x++ < 10)

int number = 6;

number++;

cout << number << endl;

7

85
New cards

What will the following code display?

int number = 6;

++number;

cout << number << endl;

7

86
New cards

What will the following code display?

int number = 6;

cout << number++ << endl;

6

87
New cards

What will the following code display?

int number = 6;

cout << ++number << endl;

7

88
New cards

What will the following code display?

int number = 6;

int x = 0;

x = number--;

cout << x << endl;

6

89
New cards

What will the following code display?

int number = 6;

int x = 0;

x = --number;

cout << x << endl;

5

90
New cards

A for statement contains three expressions: initialization, test, and

update

91
New cards

In a for statement, this expression is executed only once

initialization

92
New cards

You may define a __________ in the initialization expression of a for loop

variable

93
New cards

The do-while loop is considered

a post-test loop

94
New cards

The __________ loop is ideal in situations where you want the loop to iterate at least once.

do-while

95
New cards

The __________ loop is a good choice when you know how many times you want the loop to iterate in advance of entering the loop.

for

96
New cards

The __________ loop is a good choice when you do not want the loop to iterate if the condition is false in the beginning.

while

97
New cards

If you want a user to enter exactly 20 values, which loop would be the best to use?

for

98
New cards

What will the following code display?

int x = 0;

while (x < 5) {

cout << x << " ";

x++;

}

0 1 2 3 4

99
New cards

What will the following code display?

int x = 0;

for (int count = 0; count < 3; count++)

x += count;

cout << x << endl;

3

100
New cards

How many times will the following loop display "Hello world!"?

for (int i = 0; i < 20; i++)

cout << "Hello world!" << endl;

20