Programming Fundamentals 1: Exam 2 (Ch 4-6)
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; -0 0 -10 10 -None of these -7 15 -1 1
1 1
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/False
True
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/False
True
If you want to stop a loop before it goes through all of its iterations, the break statement may be used. -True/False
True
It is possible to define a file stream object and open a file in one statement. -True/False
True
Multiple relational expressions cannot be placed into the test condition of a for loop. -True/False
True
The update expression of a for loop can contain more than one statement, for example: for(i = 5; i <= 10; i++, total+= sales) -True/False
True
You should be careful when using the equality operator to compare floating point values because of potential round-off errors. -True/False
True
This operator takes an operand and reverses its truth or falsehood: -|| -! -=! -!& -None of these
!
Which of the following is evaluated first, given the expression: A && B || C && !D -C && !D -A && B -B || C -!D
!D
To allow file access in a program, you must use ________ in the header file. -#include fstream -#include file -#include fileaccess -#include cfile
#include fstream
This operator represents the logical AND: -&& -++ -None of these -@ -||
&&
This operator represents the logical AND: -|| -None of these -++ -&& -@
&&
These are operators that add and subtract one from their operands. -conditional and relational -binary and ternary -binary and unary -++ and -- -plus and minus
++ and --
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; -6. -4. -5. -1. -3. -2.
-5. -3.
When a relational expression is FALSE, it has the value --1 -None of these -1 -0, 1, or -1 -0
0
What will the following code display? int x = 0; while (x < 5) { cout << x << " "; x++; } -This is an infinite loop -0 1 2 3 4 -0 1 2 3 4 -0 1 2 3 4 5
0 1 2 3 4
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; -0 -1 -7 -10 -x >= y
1
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; } -None of these -1 -99 -0 -98
1
What is the output of the following code segment? n = 1; while (n <= 5) cout << n << " "; n++ -2 3 4 5 -1 2 3 4 5 6 -1 2 3 4 5 -1 2 3 4 -1 1 ... and on forever
1 1 ... and on forever
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; -23 -None of these -3 -0 -13
13
How many times will the following loop display "Looping!"? for (int i = 20; i > 0; i--) cout << "Looping!" << endl; -an infinite number of times -20 -19 -21
20
What will the following code display? int number = 6; int x = 0; x = --number; cout << x << endl; -5 -7 -0 -6
5
What will the following code display? int number = 6; int x = 0; x = --number; cout << x << endl; -6 -7 -5 -0
5
What will the following code display? int number = 6; int x = 0; x = number--; cout << x << endl; -6 -0 -7 -5
6
What will the following code display? int number = 6; ++number; cout << number << endl; -0 -6 -5 -7
7
What will the following code display? int number = 6; ++number; cout << number << endl; -7 -0 -6 -5
7
What will the following code display? int number = 6; cout << ++number << endl; -5 -0 -6 -7
7
What will the following code display? int number = 6; cout << ++number << endl; -5 -7 -6 -0
7
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"; -100 -10 -all of these -0 -99
99
In the following statement, which operator is used first? while (x++ < 10) cannot tell without the rest of the code -++ -neither; the expression is invalid -<
<
The ________ is an equality (or comparison) operator. -== -None of these ->= -= -!=
==
Input values should always be checked for -division by zero, if division is taking place -All of these -an appropriate range -None of these -reasonableness
All of these
A while loop is somewhat limited because the counter can only be incremented by one each time through the loop. True/False
False
The condition that is tested by a while loop must be enclosed in parentheses and terminated with a semicolon. -True/False
False
The conditional operator takes two operands. -True/False
False
The default section is required in a switch statement. -True/False
False
The following code correctly determines whether x contains a value in the range of 0 through 100, inclusive. if (x > 0 && <= 100) -True/False
False
A while loop's body can contain multiple statements, as long as they are enclosed in braces. -True/False
True
An expression that has any value other than 0 is considered true by an if statement. -True/False
True
An initialization expression may be omitted from the for loop if no initialization is required. -True/False
True
An output file is a file that data is written to. -True/False
True
As a rule of style, when writing an if statement you should indent the conditionally-executed statements. -True/False
True
In C++ 11 you can pass a string object as an argument to a file stream object's open member function. -True/False
True
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 -never -at least once -as many times as the user wishes -at least twice -None of these
at least once
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 -scope -switch -exit -None of these
break
Assuming dataFile is a file stream object, the following statement: dataFile.close(); -closes a file -is illegal in C++ -None of these -needs a filename argument to execute correctly -is legal but risks losing valuable data
closes a file
Relational operators allow you to ________ numbers. -add -multiply -None of these -compare -average
compare
A statement that may be used to stop a loop's current iteration and begin the next one is -break -None of these -continue -terminate -re-iterate
continue
A variable that is regularly incremented or decremented each time a loop iterates is a -None of these -null terminator -constant -counter -control
counter
The ________ loop is ideal in situations where you want the loop to iterate at least once. -do-while -pre-test -for -while -switch
do-while
After the following code executes, what is the output if user enters 0? int x = 1; cout << "Enter a 0 or 1: " endl; cin >> x; if (c) cout << "true" << endl; else cout << "false" << endl; -nothing will be displayed -false -0 -x -true
false
What is the value of the following expression? true && false -None of these -false --1 -+1 -true
false
A variable, usually a bool or an int, that signals when a condition exists is known as a(n) -logical operator -None of these -relational operator -arithmetic operator -flag
flag
If you want a user to enter exactly 20 values, which loop would be the best to use? -switch -None of these -for -do-while -while
for
In a for statement, this expression is executed only once: -validation -None of these -null -the test -initialization
initialization
These operators connect two or more relational expressions into one, or reverse the logic of an expression. -irrational -None of these -logical -negation -relational
logical
A loop that is inside another loop is called a(n) -nested loop -None of these -post-test loop -pre-test loop -infinite loop
nested loop
When an if statement is placed within the conditionally-executed code of another if statement, this is known as -complexity -None of these -overloading -nesting -validation
nesting
The while loop is a ________ loop. -post-test -pre-test -None of these -infinite -limited
pre-test
A special value that marks the end of a list of values is a -variable -counter -constant -None of these -sentinel
sentinel
To write information to a file, use the -stream insertion operator -cout object -output object -None of these -pen object
stream insertion operator
The default section of a switch statement performs a similar task similar to the ________ portion of an if/else if statement. -None of these -conditional -trailing else -break -All of these
trailing else
What is the value of the following expression? true && !false -None of these -false -+1 --1 -true
true
What is the value of the following expression? true && true --1 -false -+1 -None of these -true
true
What is the value of the following expression? true || false --1 -true -+1 -None of these -false
true
A for statement contains three expressions: initialization, test, and -null -update -validation -None of these -reversal
update
Which of the following expressions will determine whether x is less than or equal to y? -x == y and x < y -x > y -x <= y -x =< y -x >= y
x<=y
This operator represents the logical OR: -# -None of these --- -&& -||
||
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; -25 -5 -15 -0 -10
15
How many times will the following loop display "Hello world!"? for (int = 0; i < 20; i++) cout << "Hello world!" << endl; -20 -19 -21 -an infinite number of times
20
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; -120 -10 -20 -code will not execute -12
20
How many times will the following loop display "Looping again!"? for (int i = 0; i <= 20; i++) cout << "Looping again!" << endl; -20 -19 -21 -an infinite number of times
21
The increment and decrement operators can be used in mathematical expressions; however, they cannot be used in relational expressions. -True/False
False
The value of result in the following expression will be 0 if x has the value of 12. result = x > 100 ? 0 : 1; -True/False
False
You may nest while and do-while loops but you may not nest for loops. -True/False
False
You may not use both break and continue statements within the same set of nested loops. -True/False
False
You may not use the break statement in a nested loop. -True/False
False
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; -Good-bye. -Hi, there! Good-bye. -nothing will output -"Hi, there!" -Hi, there!
Hi, there!
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.\n"; if (test_score > 60) cout << "You passed the test.\n"; else cout << "You need to study harder next time.\n"; -You failed the test. -You failed the test. You need to study harder next time. -You passed the test. -You passed the test. You need to study harder next time. -You need to study harder next time.
You passed the test.
Given the if/else statement: if (a < 5) b = 12; else d = 30; Which of the following performs the same operation? -a < 5 ? b = 12 : d = 30; -b < 5 ? b = 12 : d = 30; -d = 30 ? b = 12 : a = 5; -a >= 5 ? d = 30 : b = 12; -None of these
a < 5 ? b = 12 : d = 30;
The two important parts of a while loop are the expression that is tested for a true or false value and -one line of code that is repeated once, if the expression is true -a statement or block that is repeated only if the expression is false -a statement or block that is repeated as long as the expression is true -a statement or block that is repeated once, if the expression is true
a statement or block that is repeated as long as the expression is true
If you intend to place a block of statements within an if statement, you must place ________ around the block. -parentheses( ) -angle brackets < > -None of these -square brackets [ ] -curly braces { }
curly braces { }
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 is equal to C cout << "This is a check\n"; -if (code == 'C') cout << "This is a check\n"; -if (code == C) cout << "This is a check\n"; -if (code == "C") cout << "This is a check\n";
if (code == 'C') cout << "This is a check\n";
To write read data from a file, you define an object of the ________ data type. -fstream -ofstream -ifstream -inputFile
ifstream
This means to increase a value: -None of these -decrement -parse -modulus -increment
increment
When a program lets the user know that an invalid choice has been made, this is known as: -output correction -output validation -input validation -compiler criticism -None of these
input validation
Which line in the following program will cause a compiler error? #include <iostream> using namespace std; int main() { int number = 5; if (number >= 0 && <= 100) cout << "passed.\n"; else cout << "failed.\n"; return 0; } -line 3 -None will cause an error -line 7 -line 9 -line 6
line 6
This is a control structure that causes a statement or group of statements to repeat. -selection structure -loop -decision statement -cout object -None of these
loop
If you place a semicolon after the test expression in a while loop, it is assumed to be a(n) -pre-test loop -infinite loop -post-test loop -None of these -null statement
null statement
To write data to a file, you define an object of the ________ data type. -ifstream -fstream -ofstream -outputFile
ofstream
A file must be ________ before data can be written to or read from it. -buffered -opened -closed -named -initialized
opened
Assuming outFile is a file stream object and number is a variable, which statement writes the contents of number to the file associated with outFile? -outFile >> number; -number >> outFile; -outFile << number; -write(outFile, number);
outFile << number;
When the increment operator precedes its operand, as ++num, the expression is in ________ mode. -binary -postfix -prefix -None of these -preliminary
prefix
This operator increments the value of its operand and then uses the value in context. -prefix increment -prefix decrement -postfix increment -None of these -postfix decrement
prefix increment
Whereas < is called a relational operator, x < y is called a(n) -None of these -arithmetic expression -relative operator -arithmetic operator -relational expression
relational expression
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 = 2 -result = 1 -result = 0 -there will be no output -result = 3
result = 3
This statement uses the value of a variable or expression to determine where the program will branch to. -None of these -association -scope -select -switch
switch
If you place a semicolon after the statement: if (x < y) -None of these -All of these are true -the code will not compile -the if statement will always evaluate to false -the compiler will interpret the semicolon as a null statement
the compiler will interpret the semicolon as a null statement
You may define a ________ in the initialization expression of a for loop. -new data type -constant -function -variable -None of these
variable