Exam 2 Notes

Ace your homework & exams now with Quizwiz!

What is the missing If condition in the following code fragment? The program is supposed to halt if the input file does not exist. ifstream inFile; inFile.open("myfile.dat"); if ( ) { cout << "Cannot open input file." << endl; return 1; }

!inFile

Given the two lines of input data ABC DEF what value is read into ch by the following code? (str is of type string, and ch is of type char.) getline(cin, str); cin.get(ch);

'D'

Given the two lines of input data ABC DEF what value is read into ch by the following code? (str is of type string, and ch is of type char.) cin >> str; cin.get(ch);

'D' **

What is the output of the following code fragment? (Be careful here.) n = 1; while (n <= 5) cout << n << ' '; n++;

1 1 1 forever

After execution of the following code, what will be the value of angle if the input value is 10? cin >> angle; if (angle > 5) angle = angle + 5; if (angle > 2) angle = angle + 10;

25

If a quadratic-time algorithm requires 3 minutes to process 100 data values on a particular computer, approximately how long would it take for the algorithm to process 10,000 data values?

30,000 minutes

Given the input data 25 10 6 -1 what is the output of the following code fragment? (All variables are of type int.) sum = 0; cin >> number; while (number != -1) { cin >> number; sum = sum + number; } cout << sum << endl;

41 (not) 40 (not)

After execution of the following code, what is the value of length? (count and length are of type int.) length = 5; count = 4; while (count <= 6) { if (length >= 100) length = length - 2; else length = count * length; count++; }

98

P Q P op Q TTT TFF FTF FFF

AND

Which of the following statements about functional decomposition is false?

Each level of a solution tree is more abstract (less detailed) than the level above it.

Which of the following is not one of the things a programmer must do in order to use files in a C++ program?

Erase the contents of each output file before running the program.

True or False? An infinite loop is one in which the While expression always has the value false.

False

True or False? Boolean variables cannot store the result of a comparison of two variables.

False

True or False? If P and Q are logical expressions, the expression P AND Q is TRUE if either P or Q is TRUE or both are TRUE.

False

True or False? If a While loop's termination condition becomes true in the middle of the loop body, the loop is exited immediately.

False

True or False? Implementing a test plan guarantees that a program is completely correct.

False

True or False? Testing is performed only in the implementation phase of a program's life cycle.

False

True or False? The expression !(n < 5) is equivalent to the expression n > 5.

False

True or False? The input statement cin >> someInt; could also be written as someInt << cin;

False

True or False? The statement if (grade == 'A' || grade == 'B' || grade == 'C') cout << "Fail"; else cout << "Pass"; prints Pass if grade is 'A', 'B', or 'C' and prints Fail otherwise.

False

True or False? Using the >> operator, a floating-point data value may be read into an int variable.

False

True or False? When a While expression evaluates to false, the loop terminates and control goes back to the statement immediately before the While statement

False

True or False? When a While expression evaluates to false, the loop terminates and control goes back to the statement immediately before the While statement.

False

What is the output of the following code fragment if the input value is 20? (Be careful here.) cin >> someInt; if (someInt > 30) cout << "Moe "; cout << "Larry "; cout << "Curly";

Larry Curly

What happens when a C++ input stream enters the fail state?

The system does not display an error message, the program continues running, and further input operations with that stream are ignored.

Given the following code: string name1; string name2; name1 = "Mark"; name2 = "Mary"; what is the value of the relational expression name1 < name2 ?

True

True or False? An example of a logical (Boolean) expression is an arithmetic expression followed by a relational operator followed by an arithmetic expression.

True

True or False? Functional decomposition focuses on actions and algorithms, whereas object-oriented design focuses on entities (objects) and their associated operations.

True

True or False? Given the module description PRINT AVERAGE (Level 1) Print a heading Print sumOfValues / countOfValues the following is an appropriate module precondition: "sumOfValues is assigned AND countOfValues does not equal 0."

True

True or False? If a C++ If statement begins with if (age = 30) the If condition is an assignment expression, not a relational expression.

True

True or False? If ch1 contains the value 'C' and ch2 contains the value 'K', the value of the C++ expression ch1 <= ch2 is true.

True

True or False? If the reading marker is in the middle of an input line of 25 characters, execution of the statement cin.ignore(500, '\n'); leaves the reading marker at the character following the next newline character

True

True or False? In C++, an infinite loop results from using the assignment operator in the following way: while (gamma = 2) { ... }

True

True or False? It is possible for the body of a While statement never to be executed.

True

True or False? When working at the keyboard, the user generates a newline character by pressing the Enter or Return key.

True

Given a Boolean variable isEmpty, which of the following is a valid C++ assignment statement?

a, b, c isEmpty = true isEmpty = !isEmpty isEmpty = m>n

Which of the following does not constitute a logical (Boolean) expression?

an arithmetic expression followed by a logical operator followed by an arithmetic expression

What s the termination condition for the following While loop? while (beta > 0 && beta < 10) { cout << beta << endl; cin >> beta; }

beta <=0 || beta >=10

Indicate where (if at all) the following loop needs a priming read. sum = 0; // Line 1 while (inFile) // Line 2 { // Line 3 sum = sum + number; // Line 4 inFile >> number; // Line 5 } // Line 6

between lines 1 and 2

An input line consists of a person's first and last initials, separated by a blank: M H Which of the following correctly inputs the person's initials into the char variables firstInit and lastInit?

cin >> firstInit >> lastInit; cin >> firstInit; cin.ignore(1 ' '); cin >> lastInit;

Which of the following is a valid input statement?

cin >> studentAge;

Which of the following statements sends a newline character to the standard output device?

cout << endl; cout << '\n';

If p is a Boolean variable, which of the following logical expressions always has the value false?

p && !p

If the int variables i, j, and k contain the values 10, 3, and 20, respectively, what is the value of the following logical expression: j < 4 || j == 5 && i <= k

true

True or False? Assuming no input errors, an execution of the >> operator leaves the reading marker at the character immediately following the last data item read.

true

True or False? To test whether someInt equals 25 or 30, the C++ expression someInt == 25 || 30 has the correct semantics but produces a syntax (compile-time) error.

True

True or False? Using an editor program to edit a file requires interactive I/O.

True

Which type of loop would be most appropriate for solving the problem 'Input an integer value, then print 'Happy Birthday' that many times'?

a count controlled loop

True or False? The logical order of statements in a program may be different from their physical order.

True

Which of the following is not a C++ relational operator?

&&

Given the line of input data 123.456 A 789 what value is read into inputChar by the following code? (alpha and beta are of type int, and inputChar is of type char.) cin >> alpha >> inputChar >> beta;

'.' (period)

Given the two lines of input data A B CDE what value is read into ch3 by the following code? (All variables are of type char.) cin.get(ch1); cin.get(ch2); cin.get(ch3);

'B'

Given the two lines of input data A B CDE what value is read into ch3 by the following code? (All variables are of type char.) cin >> ch1 >> ch2 >> ch3;

'C'

If DeMorgan's Law is used to negate the expression (i < j) && (k == l) then the result is:

(i >= j) || (k != l)

After execution of the following code, what will be the value of angle if the input value is 0? cin >> angle; if (angle > 5) angle = angle + 5; else if (angle > 2) angle = angle + 10;

0

What is the output of the following code fragment? n = 1; while (n < 5) { cout << n << ' '; n++; }

1 2 3 4

What is the output of the following code fragment? n = 1; while (n <= 5) { cout << n << ' '; n++; }

1 2 3 4 5

What is the output of the following code fragment? (All variables are of type int.) sum = 0; outerCount = 1; while (outerCount <= 3) { innerCount = 1; while (innerCount <= outerCount) { sum = sum + innerCount; innerCount++; } outerCount++; } cout << sum << endl;

10

What is the output of the following C++ code fragment? (Be careful here.) int1 = 120; cin >> int2; // Assume user types 30 if ((int1 > 100) && (int2 = 50)) int3 = int1 + int2; else int3 = int1 - int2; cout << int1 << ' ' << int2 << ' ' << int3;

120 50 70

What is the value of loopCount after control exits the following loop? loopCount = 1; while (loopCount <= 145) { alpha = alpha + 7; loopCount++; }

146

After execution of the following code, what will be the value of angle if the input value is 0? cin >> angle; if (angle > 5) angle = angle + 5; else if (angle > 2) angle = angle + 10; else angle = angle + 15;

15

After execution of the following code, what will be the value of angle if the input value is 10? cin >> angle; if (angle > 5) angle = angle + 5; else if (angle > 2) angle = angle + 10;

15

What is the output of the following code fragment? n = 1; while (n <= 5) { n++; cout << n << ' '; }

2 3 4 5 6

After execution of the following code, what is the value of length? (count and length are of type int.) length = 5; count = 4; while (count <= 6) { if (length >= 100) length = length - 2; else length = count * length; count++; }

20

Given the three lines of input data 111 222 333 444 555 666 777 888 999 what value is read into gamma by the following code? (All variables are of type int.) cin >> alpha; cin.ignore(500, '\n'); cin >> beta >> gamma;

555

What is the output of the following code fragment? (finished is a bool variable, and firstInt and secondInt are of type int.) finished = false; firstInt = 3; secondInt = 20; while (firstInt <= secondInt && !finished) if (secondInt / firstInt <= 2) // Reminder: integer division finished = true; else firstInt++; cout << firstInt << endl;

7 no, 5 no, 3 no, 8 or 9??

True or False? The number of steps performed by an O(N) algorithm is directly proportional to the number of data values that are processed.

True

True or False? The problem-solving phase of programming includes both analysis and design

True

True or False? The single statement cin >> alpha >> beta; may be used in place of the two statements cin >> alpha; cin >> beta;

True

Given the constant declaration const int FACTOR = 95; which of the following is not a valid use of FACTOR?

FACTOR = 24 cin >> FACTOR

Given the following code: string name1; string name2; name1 = "Maryanne"; name2 = "Mary"; what is the value of the relational expression name1 <= name2 ?

False

True or False? According to DeMorgan's Law, the expression !(x <= y || s > t) is equivalent to x <= y && s > t

False

True or False? In testing a loop, one tries to devise data sets that would never cause the variables to go out of range.

False

True or False? Most C++ programs are written for noninteractive I/O (input/Output) use.

False

True or False? Reading input from a file is considered interactive I/O because the CPU is required to interact with a disk drive or other device.

False

True or False? Syntactically, the only expressions that can be assigned to Boolean variables are the literal values true and false.

False

True or False? The term "complexity" is a measure of the effort expended by the computer in performing a computation.

True

In order to test the boundaries of the following condition, what data values would you use for the variable alpha? (alpha is of type int.) alpha >= 1

INT_MIN, 0,1, and INT_MAX

Which of the following statements about object-oriented design (OOD) is false?

In OOD, data plays a secondary role in support of actions to be performed

Which of the following is not a reason for using batch I/O?

Input prompts can be tailored to the experience level of the user.

This question is about short-circuit evaluation of logical expressions. Consider the following expression in some imaginary programming language (not C++): (N > 5) AND (K / N < 12) If N equals 0 when this expression is evaluated, which of the following statements about the expression is true?

It causes a divide-by-zero ERROR ONLY if the language DOES NOT USE short-circuit evaluation.

When used with an input file stream, which of the following statements about the open function is false?

It creates a new, empty file if the file does not already exist.

What does the following statement print? (All variables are of type int.) if (j < k) if (k < j) cout << 1; else cout << 2; else if (j < k) cout << 3; else cout << 4;

It prints 2 if j < k and 1 if k <= j.** not right It prints 2 if j<k and 4 otherwise ??

True or False? A hierarchical implementation of a functional decomposition is one in which some or all of the modules are implemented as separate C++ functions.

True

True or False? A program is said to be robust if it can recover from erroneous input and keep running.

True

True or False? In a functional decomposition, a concrete step is one in which some of the implementation details remain unspecified.

True

True or False? In testing a loop, one tries to devise data sets that leave the files in improper states that violate either the loop postcondition (an assertion that must be true immediately after the loop exit) or the postcondition of the module containing the loop.

True

True or False? In the design of a flag-controlled loop, the loop condition is initialized by giving the flag variable an initial value of true or false, whichever is appropriate.

True

True or False? In the function call cin.get(XXXXX); the XXXXX must be a variable name, not a constant or arbitrary expression.

True

True or False? Nested loops themselves can contain nest loops, which also can contain nested loops and so on.

True

True or False? Reading input from a keyboard is considered interactive I/O because the user is communicating directly with the computer.

True

True or False? The >> operator skips leading whitespace characters when looking for the next data value in the input stream.

True

True or False? The code segment if (speed <= 40) cout << "Too slow"; if (speed > 40 && speed <= 55) cout << "Good speed"; if (speed > 55) cout << "Too fast"; could be written equivalently as if (speed <= 40) cout << "Too slow"; else if (speed <= 55) cout << "Good speed"; else cout << "Too fast";

True

True or False? The following represents a list showing the correct order of precedence (from highest to lowest) for the arithmetic, relational, and logical operators with the assignment operator included as well. ! Unary + Unary - ∗ / % + - < <= > >= == != && || =

True

Which type of loop would be most appropriate for solving the problem 'Print every other input character until the character '@' is encountered'?

a sentinel-controlled loop or flag controlled loop

Which of the following would be a poor choice for a sentinel value?

a value of 1 for testScore a value of 75 for carSpeed

In the following code fragment, a semicolon appears at the end of the line containing the While condition. cout << 'A'; loopCount = 1; while (loopCount <= 3); { cout << 'B'; loopCount++; } cout << 'C';

an infinite loop

In the following code fragment, a semicolon appears at the end of the line containing the While condition. cout << 'A'; loopCount = 1; while (loopCount <= 3); { cout << 'B'; loopCount++; } cout << 'C'; The result will be:

an infinite loop

Which of the following code segments could be used to skip the first two characters of an input line (they may or may not be whitespace characters) and input the integer value that comes next? (Variable dummy is of type char, and inputInt is of type int.)

cin.get(dummy); cin.get(dummy); cin >> inputInt;

After execution of the following code, what is the value of length? (count and length are of type int.) length = 5; count = 4; while (count <= 6) { if (length >= 100) length = length - 2; else length = count * length; count++; }

none of the above

The phrase "minimum complete coverage" means that we test a program with

data that executes every branch at least once

Which of the following is considered interactive I/O?

entering sales records into a file in response to prompts from the computer program

A value can be stored into a variable by execution of

input statement and assignment statement

Indicate where (if at all) the following loop needs a priming read. count = 1; // Line 1 while (count <= 10) // Line 2 { // Line 3 cin >> number; // Line 4 cout << number * 2; // Line 5 count++; // Line 6 } // Line 7

no priming read is necessary

Consider the following If statement, which is syntactically correct but uses poor style and indentation: if (x >= y) if (y > 0) x = x * y; else if (y < 4) x = x - y; Assume that x and y are int variables containing the values 3 and 9, respectively, before execution of the above statement. After execution of the statement, what value will x contain?

none of the above

When used with an output file stream, which of the following statements about the open function is false?

none of the above

With respect to the loop in the following main function, what is missing? int main() { int loopCount; while (loopCount <= 8) { cout << "Hi"; loopCount++; } return 0; }

the initialization of the loop control variable


Related study sets

Intro to Health- Health Care Careers Quizzes

View Set

Level 1 Module 5 Introduction to the National Electrical Code

View Set