C++ Final Review
What is the value of number after the following statements execute? int number = 10; number += 5; number -= 2; number *= 3;
39
What will the value of x be after the following statements execute? int x; x = 18/4;
4
What is the value of average after the following code executes? double average; average = 1.0 + 2.0 + 3.0 / 3.0;
4.0
What will the following code print? num = 8; cout << --num << " "; cout << num++ << " "; cout << num;
7 7 8
True/False: A while loop may have a semicolon after the test expression and before the body of the loop, but it is not required
False
True/False: If you want to know the length of the string that is stored in a string object, you can call the object's sizeof member function
False
True/False: Once a value has been stored in a variable it cannot be changed
False
True/False: Relational operators connect two or more relational expressions into one, or reverse the logic of an expression
False
True/False: The following C++ test checks if the variable child is in the range 3 to 12 if (child >= 3 || child <= 12)
False
True/False: The following statement will output $5.00 to the screen: cout << setprecision(5) << dollars << endl;
False
True/False: The following statements will not print anything x = 5; if (x < 5) cout << "Hello "; cout << "world \n";
False
True/False: The scope of a variable is the program it is defined in
False
True/False: To check if a variable has a particular value, use the = relational operator, as in the statement if (s=3) cout << "S has the value 3";
False
True/False: To decrement a number means to increase its value
False
True/False: You can nest a for loop inside another for loop, but cannot nest a while loop inside another while loop or a do-while loop inside another do-while loop
False
Which is true about the following statement? cout << setw(4) << num4 << " ";
It allows four spaces for the value in the variable num4
True/False: If the sub-expression on the left side of an || operator is true, the expression on the right side will not be checked
True
True/False: Most of the lines in a program contain something meaningful; however, some of the lines may contain nothing at all
True
True/False: Relational expressions and logical expressions are both Boolean, which means they evaluate to true or false
True
True/False: Syntax involves rules that must be followed when writing a program
True
True/False: The rule for matching an else with an if is that an else goes with the last if statement before it that doesn't have its own else
True
True/False: The statement pass = (score >= 7) ? true : false; does exactly the same thing as the if/else statement below: if ( score >=7) pass = true; else pass = false;
True
Which of the following correctly consolidates the following declaration statements into one statement? int x = 7; int y = 16; int z = 28;
int x = 7, y = 16, z = 28;
In any program that uses the cin object, you must include the ________
iostream header file
The ++ operator
is a unary operator adds one to the value of its operand can operate in prefix or postfix mode
The -- operator
is a unary operator subtracts one from the value of its operand must have an lvalue, such as a variable, as its operand can be used in either prefix or postfix mode
Words with a special meaning that may be used only for their intended purpose are known as
keywords
Assume that a program has the following variable definition: char letter; Which of the following statements correctly assigns the character Z to the variable?
letter = 'Z';
Mistakes that allow a program to run, but cause it to produce erroneous results are called
logic errors
A sentinel is a special value that
marks the end of a list of values
You may define a(n) ________ in the initialization expression of a for loop
variable
Which statement is equivalent to the following? x = x * 2;
x *= 2;
True/False: If a new value is stored in a variable, it replaces whatever value was previously there
True
Three primary activities of a typical program are
input, processing, and output
What will the following expression evaluate to? ! ( 6 > 7 || 3 == 4)
true
What is the modulus operator?
%
Character constants in C++ are always enclosed in ________
'single quotation marks'
When this operator is used with string operands it concatenates them, or joins them together
+
________ are C++ operators that change their operands by one
++ and --
Assume that x is an int variable. What value is assigned to x after the following assignment statement is executed? x = -3 + 4 % 6 / 5;
-3
In C++ when a relational expression is false, it has the value
0
What is the value stored at x, given the statements: int x; x = 3 / static_cast<int>(4.5 + 6.4);
0
What will the following code display? int x=0, y=1, z=2; cout << x << y << z << endl;
012
What is the output of the following statement? cout << 4* (15/(1+3)) <<endl;
12
What will the value of x be after the following statements execute? int x = 0; int y = 5; int z = 4; x = y + z * 2;
13
Look at the following program and answer the question that follows it. 1 //This program displays my gross wages. 2 //I worked 40 hours and I make $20.00 per hour 3 #include <iostream> 4 using namespace std; 5 6 int main() 7 { 8 int hours; 9 double payRate, grossPay; 10 11 hours=40; 12 payRate=20.0; 13 grossPay=hours*payRate; 14 cout<<"My gross pay is $" <<grossPay<<endl; 15 return 0; 16 } Which line(s) in this program cause output to be displayed on the screen?
14
What will the value of result be after the following statement executes? result = 6 - 3 * 2 +7 - 10 / 2 ;
2
In the following C++ statement, what will be executed first according to the order of precedence? result = 6 - 3 * 2 + 7 - 10 / 2 ;
3 * 2
What will the following statement do if x equals 17 and answer = 20? answer = x > 100 ? 0 : 1;
Assign 1 to answer
High-level programming languages include
C++ and Java C++ and JavaScript C++ and Visual Basic
Of the following, which is a valid C++ identifier?
June1997 _employee_number ___department myExtraLongVariableName
These are data items whose values do not change while the program is running
Literals
What will the following code display? cout<< "Monday"; cout<< "Tuesday"; cout<< "Wednesday";
MondayTuesdayWednesday
If s1 and s2 are string objects, s1 == s2 is true when
None of these because in each case one or more characters in the strings have different ASCII codes
When converting some algebraic expressions to C++, you may need to insert ________ that do not appear in the algebraic expression
Parentheses
In programming terms, a group of characters inside a set of quotation marks is called a(n):
String literal
In a C++ program, two slash marks ( // ) indicate:
The beginning of a comment
When the final value of an expression is assigned to a variable, it will be converted to:
The data type of the variable
True/False: Before a computer can execute a program written in a high level language, such as C++, it must be translated into object code
True
What will the following code display? int number=7; cout << "The number is" << "number" << endl;
The number is number
True/False: A pair of characters or a pair of string objects can be compared with any of the relational operators
True
An integrated development environment (IDE) normally includes
a text editor a compiler a debugger
A variable that keeps a running total of data values is called a(n)
accumulator
A set of well-defined steps for performing a task or solving a problem is known as
an algorithm
The statements in the body of a do-while loop are executed
at least once
A variable whose value can be either true or false is of this data type
bool
If a switch statement has no ________ statements, the program "falls through" all of the statements below the one with the matching case expression
break
The ________ statement causes a loop to terminate early
break
The function, pow(x, 5.0), requires this header file:
cmath
The ________ is/are used to display information on the computer's screen
cout object
The ideal type of loop to use for repeating a menu is a(n) ________ loop
do-while
The ideal type of loop to use if you want a user to enter exactly 20 values is a(n) ________ loop
for
To use files in a C++ program you must include the ________ header file
fstream
________ reads a line of input, including leading and embedded spaces, and stores it in a string object
getline
C++ is an example of a ________ programming language
high-level
The ________ statement causes other program statements to execute only under certain conditions
if
The ________ statement executes one statement, or block of statements, if a condition is true and skips it, doing nothing, if the condition is false
if
Assume that a program has the following string object definition: string name; Which of the following statements correctly assigns a string literal to the string object?
name = "Jane";
When an if statement is placed within the conditionally-executed code of another if statement, this is known as a(n)
nested if
The programmer usually enters source code into a computer using
none of these
Which statement is equivalent to the following? number +=1;
number = number + 1;
When a variable is assigned a number that is too large for its data type, it:
overflows
The do-while loop is a(n) ________ loop, whereas the while loop is a(n) ________ loop
post test, pretest
The while loop is a(n) ________ loop, whereas the do-while loop is a(n) ________ loop
pretest, post test
A(n) ________ is a set of instructions that tells the computer how to solve a problem
program
The expression x < y is called a(n) ________ expression
relational
A variable's ________ is the part of the program that has access to the variable
scope
A(n) ________ is a special value that marks the end of a list of values
sentinel
This manipulator is used to establish a field width for the value immediately following it
setw
A character literal is enclosed in ________ quotation marks, whereas a string literal is enclosed in ________ quotation marks
single, double
The statements written by a programmer are called
source code
A ________ is a complete instruction that causes the computer to perform some action
statement
The first step in using the string class is to #include the ________ header file
string
The programs that control and manage the basic operations of a computer are generally referred to as
system software
Internally, the central processing unit (CPU) consists of two parts:
the arithmetic and logic unit (ALU) and the control unit
The CPU includes
the arithmetic and logic unit (ALU) and the control unit
In order for a C++ program to read data from a file,
the file must already exist the program must define a file stream object that can "point to" (i.e., reference) the file the program must open the file
If a while loop has no braces around the body of the loop
the loop body contains just one statement
The term hardware refers to
the physical components that make up a computer
The default section of a switch statement performs a similar task as the ________ portion of an if/else if statement
trailing else