COSC 1436 Exam 2 Study Guide

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

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

True

An expression that has any value other than 0 is considered true by and if statement. (T/F)

True

An initialization expression may be omitted from the for loop if no initialization is required. (T/F)

True

An output file is a file that data is written to. (T/F)

True

Both of the following if statements perform the same operation. (T/F) if (sales > 10000) commissionRate = 0.15; if (sales > 10000) commissionRate = 0.15;

True

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

True

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

True

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

True

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

True

It is possible to define a file stream object and open a file in one statement. (T/F)

True

Multiple relational expressions cannot be placed into the test condition of a for loop. (T/F)

True

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

True

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

True

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

True

This operator represents the logical OR: A. || B. None of these C. && D. -- E. #

A

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 (Missing variable "number" on the right side of the AND operator)

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

false

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

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

12

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 (Fall through adds 3, then 6, then 4)

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

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; A. There will be no output B. result = 3 C. result = 0 D. result = 1 E. result = 2

3

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

6 (postfix increments the value after it is used in an expression)

A ________ is a value that signals when the end of a list of values has been reached. A. sentinel B. terminal C. token

A

A loop that repeats a specific number of times is known as a(s) Select one: A. count-controlled B. pretest C. infinite D. conditional

A

A statement that causes a loop to terminate early is A. break B. continue C. terminate D. None of these E. re-iterate

A

After the following code executes, what is the output if user enters 0? int x = -1; cout << "enter a 0 or 1: "; cin >> x; if (c) cout << "true" << endl; else cout << "false" << endl; A. false B. x C. 0 D. true E. nothing will be displayed

A

The do-while loop is a(n) ________ loop that is ideal in situations where you always want the loop to iterate at least once. A) post-test B) pre-test C) infinite D) null-terminated E) None of these

A

The statements in the body of a while loop may never be executed, whereas the statements in the body of a do-while loop will be executed: A) at least once B) at least twice C) as many times as the user wishes D) never E) None of these

A

The two important parts of a while loop are the expression that is tested for a true or false value and A. a statement or block that is repeated as long as the expression is true B. a statement or block that is repeated once, if the expression it true C. one line of code that is repeated once, if the expression is true D. a statement or block that is repeated only if the expression is false

A

This operator increments the value of its operand, then uses the value in context. A) prefix increment B) postfix increment C) prefix decrement D) postfix decrement E) None of these

A

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; A. Hi, there! B. Good-bye. C. "Hi, there!" D. nothing will happen E. Hi, there! Good-bye.

A

What is the output of the following code? int w = 98; int x = 99; int y = 9; 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. 1 B. None of these C. 0 D. 98 E. 99

A

What will the following code display? int number = 6; int x = 0; x = --number; cout << x << endl; A. 5 B. 6 C. 7 D. 0

A

Whereas < is called a relational operator, x < y is called a(n) ________. A. relational expression B. arithmetic expression C. arithmetic operator D. relative operator E. None of these

A

What is the output of the following code segment? n = 1; while (n <= 5) cout << n << ' '; n++; A. 1 1 ... and on forever B. 1 2 3 4 C. 1 2 3 4 5 6 D. 1 2 3 4 5 E. 2 3 4 5

A (while loop executes only the line following it forever, since there are no braces)

Select all that apply. Given: x = 5, y = 6, z = 8. Which of the following are FALSE? 1. x == 5; 2. x < (y + 2); 3. z <= 4; 4. y > (z - x); 5. z > (y + x); 6. y <= 6; A. 5 B. 4 C. 1 D. 6 E. 3 F. 2

A and E

When an if statement is placed within the conditionally-executed code of another if statement, this is known as: A. nesting B. complexity C. None of these D. validation E. overloading

A. nesting

A ________ loop will always be executed at least once A. pretest B. post-test C. user-controlled D. conditional

B

If you intend to place a block of statements within an if statement, you must place ________ around the block. A. parentheses ( ) B. curly braces { } C. None of these D. square brackets [ ] E. angle brackets < >

B

Input values should always be checked for: A. None of these B. All of these C. division by zero, if division is taking place D. reasonableness E. an appropriate range

B

Look at the following statement. while (x++ < 10) Which operator is used first? A) ++ B) < C) Neither. The expression is invalid.

B

Something within a while loop must eventually cause the condition to become false, or a(n) ________ results. A) null value B) infinite loop C) unexpected exit D) compiler error E) None of these

B

The ________ is an equality (or comparison) operator. A. = B. == C. None of these D. >= E. !=

B

The do-while loop is considered a(n) ________ loop. A) pre-test B) post-test C) infinite D) limited E) None of these

B

The while loop contains an expression that is tested for a true or false value, and a statement or block that is repeated as long as the expression: A) is false B) is true C) does not evaluate to true or false D) evaluates to true or false E) None of these

B

The while loop is a ________ loop. A. infinite B. pre-test C. None of these D. post-test E. limited

B

The while loop is this type of loop. A. post-test B. pre-test C. infinite D. limited E. None of these

B

These are operators that add and subtract one from their operands. A) plus and minus B) ++ and -- C) binary and unary D) conditional and relational E) None of these

B

This is a variable that is regularly incremented or decremented each time a loop iterates. A) constant B) counter C) control statement D) null terminator E) None of these

B

This means to increase a value: A. parse B. increment C. modulus D. None of these E. decrement

B

When the increment operator precedes its operand, as in ++num1, the expression is in this mode. A) postfix B) prefix C) preliminary D) binary E) None of these

B

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. None of these D. answer = 2

B. answer = 1

A for statement contains three expressions: initialization, test, and A. null B. validation C. update D. reversal E. None of these

C

How many times will the following loop display "Looping!"? for (int i = 20; i > 0; i--) cout << "Looping!" << endl; A. an infinite number of times B. 19 C. 20 D. 21

C

If you place a semicolon after the test expression in a while loop, it is assumed to be a(n): A) pre-test loop B) post-test loop C) null statement D) infinite loop E) None of these

C

In a for statement, this expression is executed only once. A) test B) null C) initialization D) validation E) None of these

C

This is a control structure that causes a statement or group of statements to repeat. A) decision statement B) constant C) loop D) cout object E) None of these

C

This operator represents the logical AND. A. ++ B. @ C. && D. None of these E. ||

C

This statement uses the value of a variable or expression to determine where the program will branch to. A. None of these B. select C. switch D. scope E. association

C

To write read data from a file, you define an object of the ________ data type. A. ofstream B. inputFile C. ifstream D. fstream

C

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. exit B. None of these C. break D. switch E. scope

C

You may define a ________ in the initialization expression of a for loop. A) constant B) function C) variable D) new data type E) None of these

C

A loop that is inside another loop is called: A) an infinite loop B) a pre-test loop C) a post-test loop D) a nested loop E) None of these

D

Assuming dataFile is a file stream object, the following statement: dataFile.close(); A. is illegal in C++ B. needs a filename argument to execute correctly C. None of these D. closes a file E. is legal but risks losing valuable data

D

This is a special value that marks the end of a list of values. A) constant B) variable C) loop D) sentinel E) None of these

D

This statement may be used to stop a loop's current iteration and begin the next one. A) break B) terminate C) re-iterate D) continue E) None of these

D

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." << endl; else cout << "You need to study harder next time." << endl; A. You need to study harder next time. B. You passed the test. You need to study harder next time. C. You failed the test. You need to study harder next time. D. You passed the test. E. You failed the test.

D

What is the value of the following expression? true && true A. -1 B. None of these C. false D. true E. +1

D

What is the value of the following expression? true || false A. None of these B. +1 C. false D. true E. -1

D

What will the following code display? int number = 6; ++number; cout << number << endl; A. 6 B. 5 C. 0 D. 7

D

What will the following code display? int x = 0; while (x < 5) { cout << x << " "; x++; } A. 0 1 2 3 4 5 B. 0 1 2 3 4 C. This is an infinite loop D. 0 1 2 3 4

D

When the increment operator precedes its operand, as ++num, the expression is in ________ mode. A. binary B. postfix C. None of these D. prefix E. preliminary

D

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

D

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. 0 B. all of these C. 100 D. 99 E. 10

D

A variable that is regularly incremented or decremented each time a loop iterates is a A. None of these B. null terminator C. constant D. control E. counter

E

Given the if/else statement: if (a < 5) b = 12; else d = 30; Which of the following performs the same operation? A. a >= 5 ? d = 30 : b = 12; B. None of these C. d = 30 ? b = 12 : a = 5; D. b < 5 ? b = 12 : d = 30; E. a < 5 ? b = 12 : d = 30;

E

If you place a semicolon after the statement: (x < y) A. All of these are true B. the code will not compile C. the if statement will always evaluate to false D. None of these E. the compiler will interpret the semicolon as a null statement

E

If you place a semicolon after the test expression in a while loop, it is assumed to be a(n) A. None of these B. pre-test loop C. infinite loop D. post-test loop E. null statement

E

In a for statement, this expression is executed only once: A. the test B. validation C. None of these D. nill E. initialization

E

Select all that apply. Which of the following steps is normally performed by a for loop? A. update the control variable during each iteration B. terminate when the control variable reaches its maximum or minimum value C. test the control variable by comparing it to a maximum or minimum value D. initialize the control variable to a starting value E. All of the above

E

The ________ loop is ideal in situations where you want the loop to iterate at least once. A. pre-test B. for C. while D. switch E. do-while

E

The default section of a switch statement performs a similar task to the ________ portion of an if/else if statement. A. None of these B. conditional C. break D. All of these E. trailing else

E

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; A. code will not execute B. 10 C. 120 D. 12 E. 20

E

What is the value of the following expression? true && !false A. None of these B. +1 C. -1 D. false E. true

E

A while loop is somewhat limited because the counter can only be incremented by one each time through the loop. (T/F)

False

The condition that is tested by a while loop must be enclosed in parentheses and terminated with a semicolon. (T/F)

False

The conditional operator takes two operands. (T/F)

False

The default section is required in a switch statement. (T/F)

False

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

False

The increment and decrement operators can be used in mathematical expressions; however, they cannot be used in relational expressions. (T/F)

False

The scope of a variable declared in a for loop's initialization expression always extends beyond the body of the loop. (T/F)

False

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

False

You may nest while and do-while loops but you may not nest for loops. (T/F)

False

You may not use both break and continue statements within the same set of nested loops. (T/F)

False

You may not use the break statement in a nested loop. (T/F)

False

Relational operators allow you to ________ numbers.

compare

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

input validation

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

logical


संबंधित स्टडी सेट्स

Evolution of European Thought Take-home Test

View Set

OOP + Python3 Basics coding exercises

View Set

Human resource management chapter for homework

View Set

Night (Section 5 & 6) test 3/13/18

View Set

Chemistry/Physics: Chemical Kinetics (Rate Laws) and Equilibrium

View Set