C++ Exam 2

Ace your homework & exams now with Quizwiz!

Something within a while loop must eventually cause the condition to become false or a blank result

Infinite loop

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

Prefix increment

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

Relational expression

If an OPEN file command fails in a program, what should you code?

Send an error message to the user and end or exit the program

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

True

When the increment operator precedes its operand, as in ++num1, the expression is in this mode.

prefix

What would you do to correct the following selection structure: switch (counter); { case 1 x=1 case=2 x=2 case=3 x=3 default: Cout << "invalid input"; }

switch (counter) { case 1 x=1; case=2 x=2; case=3 x=3; default: cout << "invalid input"; }

Write a program using a "while" statement with a number that starts at 3001 and repeatedly subtract 3 from that number until the number is negative. then print the number.

#include <iostream> using namespace std; int main () { int num = 3001; while( num > 0 ) { num = num - 3; } if (num < 0) { cout << num; } return 0; }

This operator represents the logical AND.

&&

These are operators that add and subtract 1 from their operands

++ and --

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

1

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

Look a the following statement while (x++ < 10) which operator is used first?

<

This operator is used in C++ to represent equality.

==

The while loop has two important parts: an expression that is tested for a true or false value and blank

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

Input values should always be checked for:

Appropriate range Reasonableness Division by zero, if division is taking place ---All of these---

What 3 functions does an Operating System perform when an OPEN command for a file is issued for a program?

Assigns the file to your program Sets a pointer to the first record Allocates I/O buffer

Relational operators allow you to blank numbers

Compare

This is a variable that is regularly incremented or decremented every time a loop iterates

Counter

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

False

If the subexpression on the left side of the || operator is false, the expression on the right side will be false

False

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

False

The default section is required in a switch statement

False

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

False

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

False

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

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

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

This is a variable, usually a bool or an int, that signals when a condition exists.

Flag

This means to increase a value by one

Increment

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;

That's a high score! This is a test question!

If you place a semicolon after the statement if (x < y)

The compiler will interpret the semicolon as a null statement.

What will the 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! This is all folks!

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

True

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

True

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

True

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

True

If the subexpression on the left side of an && operator is false, the expression on the right side will be false

True

The update expression of a for loop can contain more than one statement

True

You should be careful when using the quality operator to compare floating point values because of the potential roundoff errors

True

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

Variable

When does the EOF condition arise when reading or accessing a file?

When you attempt to repass the last record

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

You failed the test! You passed the test!

When a relational expression is false, it has a value

Zero

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:

at least ounce

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

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

continue

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:

is true

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

loop

A loop that is inside another loop is called:

nested loop

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

null statement

The do while loop is a ______ loop that is ideal in situations where you always want the loop to iterate at least once

post test

The while loop is this type of loop

pre-test

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

update

Write a program for problem #5 using a "for" loop.

#include <iostream> using namespace std; int main() { for(int num=3001; num>0; num=num-3) { if(num>0) { num = num - 3; cout << num << endl; } } return 0; }

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

!

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

1 2 3 4 5

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

True

Write a nested conditional statement to execute the following logic: We receive a number from a user at the terminal. Check the number to make sure it is not zero. If it is zero, send an error message to the user and end the instruction. If the number is not zero, check to make sure the number is not negative. Send the user an error message if it is negative but otherwise divide this number into the second number entered by the user and print the answer back to the user.

#include <iostream> #include <iomanip> using namespace std; int main() { double number1, number2, newnum; cout << "Enter a number: "; cin >> number1; cout << "Enter a second number: "; cin >> number2; if (number1 == 0) { cout << "Error, you entered 0."; return 0; } if (number1 < 0) { cout << "Error, you entered a negative number."; return 0; } if (number1 >0) { newnum = number2 / number1; cout << fixed << setprecision(2) << newnum; } }

Is the following nested loop structure correct? If not, how would you correct it? int i, count; while (count < 100) { for (i=1;<100;i++) { cout << "\n I is now " << in << endl; cout << "Inner loop execution " << endl; } cout "Outer loop execution " << endl; }

#include <iostream> using namespace std; int main() { int i, count; count=0; while (count < 100) { for (int i=1; 100>=i; i++) { cout << "\n I is now " << i << endl; cout << "Inner loop execution " << endl; cout << "Outer loop execution " << endl; count=count-1; } } }

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

Assuming x is 5, y is 6, and z is 8, which of the following is false? 1. x == 5; 2. 7 <= (x + 2); 3. z <= 4; 4. (1 + x) != y; 5. z >= 8; 6. x >= 0; 7. x <= (y * 2)

3 and 4 are false.

In a for statement, this expression is executed only once

Initialization

Name and describe the 4 components of a loop

Initialize the LCV Test the LCV Loop body Update/Modify LCV

Will the following instruction execute correctly? If not, how would you correct it? cin >> type; cin temp; if(type = 'C') celsius = (5.0/9.0) * (temp-32.0); cout<<"\n Temeperature in Celcius is" << celsius << end; else farhen= (9.0/5.0)*temp+32.0; cout<<"\n Temeperature in Fahrenheit is"<<fahren<<end;

No, missing iostream and iostring header missing several { } brackets. Also, instead of "=" you need "==" cin >> type; cin temp; if(type == 'C') celsius = (5.0/9.0) * (temp-32.0); cout<<"\n Temeperature in Celcius is" << celsius << end; else farhen= (9.0/5.0)*temp+32.0; cout<<"\n Temeperature in Fahrenheit is <<fahren<<end;

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

Switch

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

True

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

nesting


Related study sets

Structure and Function of Plasma Membranes

View Set

Chapter 6: When Governments Intervene in Markets

View Set

Kinns Chapter 17 Banking Services and Procedures

View Set

Chapter 1: The Earliest Human Societies

View Set