CSCI 2001 Final exam
The endl stream manipulator
causes any output that follows to be displayed on the next line
Which of the following data types can you not use to store the value 500?
char
Which of the following statements defines and initializes a variable that stores a single character?
char customer_type = 'R';
Which of the following statements can you use to discard any data that remains in the buffer?
cin.ignore(numeric_limits<streamsize>::max, '\n');
Which of the following statements uses a compound assignment operator?
index += 1;
If a stream manipulator is sticky, it means that
it stays in effect until you change it or the program ends
Which of the following statements would you use to creat a vector of ints named ages with an initial size of 20 elements?
vector<int> ages(20);
Which of the following statements could you use to assign a value to the first element of a vector of doubles named wages?
wages[0] = 258.25;
Which of these names follows the naming conventions for constants presented in this chapter?
weeks_in_year
The int data type can be used to store
whole numbers only
Given two double variables named x and y, which of the following statements could you use to initialize both variables to a value of 0.0?
x = y = 0.0;
One difference between working with a file stream object and working with an input or output stream object is that
you must always specify whether a file is to be opened for input or output
Which of the following lets you use the code in the C++ standard library that provides for performing mathematical operations?
#include <cmath>
Which of the following operators do you use to concatenate strings and chars?
+
Which of the following is a unary operator?
++
When you use a range-based for loop with a vector, you
can avoid out of bounds access
If the value of the int variable named quantity is 5, what is the value of total after this statement is executed? int total = quantity++;
5
If the value of the int variable named quantity is 5, what is its value after this statement is executed? int total = ++quantity;
6
Given an integer variable named miles with a value of 122 and an integer variable named gallons with a value of 5, what will the value of mpg be after you execute this statement?double mpg = miles / gallons;
. 24
If the int variables a = 5, b = 2, and c = 10, what is the value of c after the following statement is executed? c = c + a * b - 5;
15
Given a double variable named total with a value of 62.5 and an integer variable named quantity with a value of 4, what will the value of price be after you execute this statement?double price = total / quantity;
15.625
Assume user_name equals "Tom" and user_age equals 22. What is printed on the console when the following statement is executed? cout << user_age << " \nis " + user_name << "'s age.";
22 is Tom's age.
Which of the following is not a valid variable name?
25Percent
How many times will the while loop that follows be executed if months has a value of 5? int i = 1;while (i < months) {futureValue = futureValue * (1 + monthlyInterestRate); i = i+1;}
4
What is the size of the vector named quantities after the following statement is executed?vector<int> quantities = { 34, 86, 22, 57, 99, 48, 55 };
7
By default, if you open an output stream for a file that already exists, what happens?
Any existing data in the file is deleted.
In an IDE, what tool do you use to enter and edit source code?
Code editor
You can use a logical operator to
Combine two Boolean expressions and reverse the value of an expression
You can use relational operators to
Compare string variables, compare numeric variables, and compare char variables
When you run a project, most IDEs automatically
Compile the project
As you develop a C++ program, you can use an IDE to
Enter and edit the source code, compile the source code, and run the application
You can use a console application to
Get data from the user, and display data to the user
Unlike an if...else statement, a switch statement
can't perform an operation based on the result of a Boolean expression
Which of the following statements is not true about a Boolean expression?
It evaluates to a string of characters.
-- Which of the following is not a reason for learning C++
Its syntax is easier to learn than the syntax of other langauges like C# and Java
Casting is the process that occurs when
One data type id converted to another data type
An IDE typically stores all of the files for a program in a
Project
Why might you sometimes need to get the minimum or maximum value that's allowable for a data type?
So your code will work correctly on all systems To be sure that a value that's assigned to a variable is in a valid range and To be sure that the value can be stored in the number of bytes that are available to the data type
What must you do if you code an infinite loop in a program?
Stop the application to end the loop
What happens when the following expression is evaluated? years_on_job >= 5 && (degree == "BA" || degree == "MA" || degree == "PHD")
The OR operations are performed before the AND operation because they're enclosed in parentheses.
What feature of an IDE can help you identify and fix syntax errors?
The error detection feature
What happens if you attempt an operation that uses the input stream and the operation fails but the stream is OK?
The failbit of the stream object will be set.
What happens if you try to open an output stream for a file that doesn't exist?
The file is created.
Which of the following statements is not true about a vector?
The indexes for the elements of a vector start at 1.
Assume that a program displays two prompts that each accept a value from the user and no data is discarded after each entry. What will happen if the user enters both values at the first prompt?
The program will extract the first value and then prompt for and extract the second value without waiting for the user to enter that value.
Assume that a program displays a prompt, accepts an int value, displays a second prompt, and then accepts a double value. Also assume that no data is discarded after each entry. What will happen if the user enters 1234.5678 at the first prompt?
The program will store 1234 for the int value and .5678 for the double value.
Which of the following is a difference between a variable and a constant?
The value of a variable can change as a program executes, but the value of a constant can't.
What happens if an integer variable is assigned a value that's too big for its type?
The variable will overflow and wrap around to the type's minimum value.
Which of the following statements do you use to jump to the end of a loop from within the loop?
break
You can provide data validation for user input by doing one of the following. Which one is it?
Use a loop that continues to accept values from the user until the value is valid and a break statement ends the loop.
Type conversion occurs automatically when
You assign the result of an arithemetic expression with one data type to a variable of anotehr data type and you perform an arithmetic operation with different data types
Which of the following is not a benefit of a typical IDE for C++
Your code compiles and runs faster.
A buffer is
a location in memory where stream data is stored as it's read or written
In a do-while loop, the Boolean expression is tested
after the loop is executed
Which of the following can you assign to a string variable?
an empty string, another string variable, and a string literal
In a while loop, the Boolean expression is tested
before the loop is executed
Which of the following data types typically has a value of either 0 or 1?
bool
f you code a while loop as shown in the following example, what do you need to do to keep it from becoming an infinite loop? while (true) {// statements}
code a break statement within the loop
A tab-delimited file is a file with
columns that are separated by a tab character and rows that are separated by a newline character
Which of the following statements do you use to jump to the top of a loop from within the loop?
continue
Which of the following statements displays the value 1473 in a column with a width of 8 characters?
cout << setw(8) << 1473 << endl;
A common extension for a C++ source file is
cpp
If you want the compiler to infer the data type of a variable based on it's initial value, you must
define and initialize the variable in one statement and code the auto keyword instead of a data type
You typically use comments in a C++ program to
describe code that is difficult to understand
You can use the cout object to:
display output on the console
Which of the following is a valid statement for defining and initializing a double variable named length to a starting value of 120?
double length = 120.0;
Based on the naming recommendations in the book, which of the following is a good identifier for a variable that will be used to hold an employee's phone number?
employee_phone_number
If the result of an arithmetic expression is assigned to a variable with a type that could cause a loss of data when the conversion is performed, you should
explicitly cast the data to indicate that you intend to perform the conversion
Which of the following is a difference between the float and double data types?
float can have up to 7 significant digits and double can have up to 16
The double data type can be used to store
floating-point numbers
You use the fixed manipulator to
format floating-point numbers in fixed-point notation
Which of the following classes could you use to append data to a file?
fstream and ofstream
You can use the cin object to:
get input from the console
To append data to an existing file, you can
set the ios::app file access flag of the file stream object and set the ios::out and ios::app file access flags of the file stream object
Which of the following functions can you use to determine if a vector contains elements?
size() and empty()
The cout object represents the
standard output stream
Which of the following statements defines and initializes a variable that stores a string of characters?
string customer_type = "Retail";
What feature of an IDE can help you enter variable names by displaying a list of the available names?
the code completion featur
When you use the setw() manipulator for a column
the column uses right justification by default
What happens when you use both integer and double values in an arithmetic expression?
the integer values are cast to double values
When a data type is promoted to another type
the new type is always wide enough to hold the original value
A syntax error occurs when
there's a syntax error in a C++ statement