CPE211 Midterm (updated 10/03)

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan 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

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

&&

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);

'\n' ^^.get() grabs whitespace

Assuming all variables are of type float, the C++ expression for (a + b) * c / d + e is:

(a + b) * c / d + e

Write a C++ logical expression that is false if either x or y is equal to 5: ____________________

(x!=5 || y != 5)

What is the output of the following program fragment? (x is a float variable.) x = 25.6284; cout << "**" << setw(6) << setprecision(1) << x << endl;

** 25.6 ^^ my output looked different but logically this should be right

Among the C++ operators +, -, *, /, and %, which ones have the lowest precedence?

+ and -

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; if you're reading in an int, then the reading marker will stop immediately at a non-int number, so the char will be a period

The C++ function _________________ allows you to access character data by their position on the string.

.at()

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

If the int variables int1 and int2 contain the values 4 and 5, respectively, then the value of the expression float(int1 / int2) is:

0

The value of the C++ expression 3 / 4 * 5 is:

0

Given that x is a float variable and num is an int variable containing the value 38, what will x contain after execution of the following statement: x = num / 4 + 3.0;

12

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 30 90

What is the output of the following program fragment? (alpha and beta are int variables.) alpha = 2463; beta = 72; cout << "123456789" << endl << setw(5) << alpha << endl << setw(5) << beta << endl;

123456789 2463 72

The value of the C++ expression 11 + 22 % 4 is:

13

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? n >> angle; if (angle > 5) angle = angle + 5; else if (angle > 2) angle = angle + 10;

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; if (angle > 2) angle = angle + 10;

25

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 9 and 3, respectively, before execution of the above statement. After execution of the statement, what value will x contain?

27

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?

27

If plant is a string variable and the statement plant = "Dandelion"; is executed, then the value of the expression plant.find('d') is

3

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

Given that x is a float variable and num is an int variable containing the value 5, what will x contain after execution of the following statement: x = num + 2;

7

Given that x is a float variable containing the value 84.7 and num is an int variable, what will num contain after execution of the following statement: num = x + 2;

86

What is the output of the following program fragment? age = 29; cout << "Are you" << age << "years old?" << endl;

Are you29years old?

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

What is the output of the following program fragment? cout << "Barrel" << endl; cout << ' '; cout << "of"; cout << "Laughs" << endl;

Barrel ofLaughs

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

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 ^^getline moves the reading marker to the next line so that .get() doesn't grab the '\n'

Which of the following statements about functional decomposition is false?

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

If we want a program to use file I/O for reading or writing, the following four things must be done EXCEPT:

Ensure that each individual file to be read or written contains a strictly limited amount of data

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

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

False

A prewritten function that is supplied by the C++ system and available for any programmer to use is called a(n) ____________________.

Header file

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

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

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 IO?

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

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";

LarryCurly

If name is a string variable, what is the output of the following code? name = "Marian"; cout << "Name: " << name.substr(1, 3) + "sta";

Name: arista

Manipulators endl, showpoint, fixed, and scientific have _______________ argument type; while, scientific, setw(n), and setprecision(n) manipulators have a(n)____________________ argument type.

None; int

Assuming alpha and beta are int variables, what is the output of the following code (which is indented poorly)? alpha = 3; beta = 2; if (alpha < 2) if (beta == 3) cout << "Hello"; else cout << "There";

Nothing is output

____________________ are the rules by which C++ operators are ordered.

Precedence

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

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

Formatting a program in a consistent, readable style is valuable to

a and b above ^^compiler doesn't care

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

a and b above (cout<<endl; cout<<'\n';)

A value can be stored into a variable by execution of:

a and c above (an input and an assignment statement)

Which of the following correctly inputs the person's initials into the char variables firstInit and lastInit?

a and c above (cin >> firstInit >> lastInit; cin >> lastInit;) ?

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

a, b, and c above

If testScore is an int variable containing the value 78, which output statement will produce the following output: 1234567890 Score: 78

a, b, and c above

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

an ARITHMETIC expression followed by a LOGICAL operator followed by an arithmetic expression

In the statement y = SomeFunc(3); the number 3 is known as the function's ____________________.

argument

A(n) ____________________ is a mechanism by which functions communicate with each other.

argument list

Which C++ logical expression correctly determines whether the value of beta lies between 0 and 100?

b and c above

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

b and c above (FACTOR = 24; cin >> FACTOR;)

Which expression does not correctly compute the mathematical average of the int variables int1, int2, and int3?

b and d above (i1 + i2 + i3) / 3.0; float(i1 + i2 + i3) / 3;

Which of the following is a valid input statement?

cin >> studentAge;

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; ^^.get grabs chars, including whitespace

Two approaches to program testing are ____________________ coverage and data coverage.

code

After the code is written in the implementation phase, you should go over it line by line. This process is known as a(n) ____________________.

code walk-through

Any statement used to alter the normally sequential flow of control is called a(n) ____________________.

control structure

If x is a float variable containing a positive value, which of the following statements outputs the value of x, rounded to the nearest integer?

cout << int(x + 0.5);

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 IO?

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

Taking some actual values and hand-calculating what the output of a program should be is called a(n) ____________________.

execution trace

The order in which the computer executes statements in a program is called the ____________________.

flow of control

The mechanism that transfers control to a function is called a(n) ____________________.

function call

When an expression contains several operators with the same precedence, the operands are combined according to the ____________________ of the operators.

grouping order (or associativity)

The C++ standard library defines a data type named ____________________ that represents a stream of characters coming from an input file.

ifstream

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 4 otherwise.

If DoSomething is a void function that takes an int expression as an argument, which of the following is an incorrect statement? (Assume all variables are int variables.)

length = DoSomething(width);

The operators &&, ||, and ! are known as ____________________ operators.

logical

A(n) ____________________ expression is an expression composed of logical (Boolean) values and operations.

logical (Boolean)

A(n) ____________________ expression is an arithmetic expression that contains operands of different data types.

mixed type

An If-Then-Else-If structure represents a(n) ____________________-way branch.

multi

When placing an If statement within an If statement, a computer programmer is creating a ___________.

nested control structure

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

none of the above

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

p && !p

A(n) ____________________ is an assertion that should be true after a module has finished executing.

postcondition

A(n) ____________________ is an assertion that must be true before a module begins executing.

precondition

The operators <, ==, >=, and != are examples of ____________________ operators. (Do not answer "binary.")

relational

The ______________ manipulator forces output values to be written scientific notation in a floating-point output.

scientific

An If statement is an example of a(n) ____________________ control structure.

selection

In programming languages that use ____________________ evaluation of a logical expression, evaluation proceeds in left-to-right order and stops as soon as the final truth value can be determined.

short-circuit

In programming languages that use ____________________ evaluation of a logical expression, all subexpressions are evaluated before applying any logical operators.

short-circuit?

The . ______________ manipulator forces display of a decimal point in a floating-point output.

showpoint

One of the following statements does not show a proper use of the sqrt library function. Which one is the wrong statement? (Assume all variables are float variables.)

sqrt(25.0 *x);

The C++ ____________________ is a collection of prewritten functions and declarations that are available for use by any C++ programmer.

standard library

Write a C++ logical expression that is true if the variable testScore is greater than or equal to 90 and less than or equal to 100: __________________

testScore >= 90 && testScore <= 100

The act of using a C++ stream object in a logical expression as if it were a Boolean variable is called ____________________ of the stream

testing the state

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

true

The explicit conversion of a value from one data type to another is called ____________________.

type casting

The expression int(someFloat) is an example of a(n) ____________________ operation.

type casting

The implicit (automatic) conversion of a value from one data type to another is called ____________________.

type coercion

A(n) ____________________ is a function that returns a single value to its caller and is invoked from within an expression.

value-returning function

A(n) ____________________ is a function that does not return a function value to its caller and is invoked as a complete, stand-alone statement.

void function

A(n) ____________________ is the process of going through the steps of an algorithm to confirm that they produce the required postcondition, given the stated precondition.

walk-through

Blanks and newline characters in an input stream are examples of ____________________ characters.

whitespace


Set pelajaran terkait

Building Blocks of the Nervous System

View Set

Types and Characteristics of Fixed Income Securities

View Set

Anatomy Exam 1- Chapter 4: The Integumentary System New

View Set

Chapter 11 APUSH American Pageant

View Set