Chapter 5: Starting out with C++ Definitions, Starting out with C++ Chapter 2 Quiz, Starting out with C++ Chapter 3 Quiz, Starting out with C++ Chapter 4 Quiz
int x = 0, y = 1, z = 2; cout << x << y << z << endl;
012
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 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; }
1
What will the following program segment display? 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;
1 1
For loop
1. Initializes a counter variable; 2. Tests an Expression; 3. Updates the Expression (Best used if testing for "false").
File Use in a Program
1. Open the file 2. Process the file 3. Close the file
What is the output of the following statement? cout << 4 * (15 / (1 + 3)) << endl;
12
What is the value of donuts after the following code executes? int donuts = 10; if (donuts != 10) { donuts = 0; } else { donuts += 2; }
12
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
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 following segment of code output if the value 11 is entered at the keyboard? int number; cin >> number; if (number > 0) { cout << "C++"; } else { cout << "Soccer"; } cout << " is "; cout << "fun" << endl;
C++ is fun
Infinite Loop
Continues to repeat until the program is interrupted.
Loop control variable
Controls the number of times that a loop iterates.
If you intend to place a block of statements within an if statement, you must place these around the block.
Curly braces { }
What is the value of average after the following code executes? double average; average = 1.0 + 2.0 + 3.0 / 3.0;
4.0
Which line in the following program will cause a compiler error? 1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 const int MY_VAL; 7 MY_VAL = 77; 8 cout << MY_VAL << endl; 9 return 0; 10 }
6 and 7
Which line in the following program will cause a compiler error? 1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int number = 5; 7 8 if (number >= 0 && <= 100) 9 cout << "passed.\n"; 10 else 11 cout << "failed.\n"; 12 return 0; 13 }
8
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"; }
99
What operator follows the cout object?
<<
What operator follows the cin object?
>>
loop
A control structure that causes a statement or group of statements to repeat.
Text file
A file that contains data that has been encoded as text, using a scheme such as ASCII or Unicode.
Binary File
A file that contains data that has not been converted to text.
Input File
A file that data is read from.
Output File
A file that data is written to.
<fstream>
A header file that contains all the declarations necessary for file operations.
Nested Loop
A loop that is inside another loop.
Count-controlled loop
A loop that repeats a specific number of times. (for). It must posses three elements: 1. An initialized counter variable 2. Test the counter by comparing it to a max value. 3. Update the counter during each iteration.
While Loop
A pretest loop. It is especially useful for validating input. (Best used when testing for "true").
File Buffer
A small "holding section" of memory that file-bound data is first written to.
Sentinel
A special value that marks the end of a list of values and cannot be mistaken as a member of the list.
Continue Statement
A statement that causes a loop to stop its current iteration and begin the next one.
Break Statement
A statement that causes a loop to terminate early.
Running total
A sum of numbers that accumulates with each iteration of a loop.
Counter
A variable that is regularly incremented or decremented each time a loop iterates.
User-controlled loop
Allows the user to decide the number of iterations. (The do-while is a good choice for repeating a menu).
File Stream Object
An *object* that is associated with a specific *file*, and provides a way for the program to work with that file.
Test Expression
An expression that controls the execution of the loop. As long as it is true, the body of the loop will repeat.
In C++ 11, if you want an integer literal to be treated as a long long int, you can append ________ at the end of the number.
LL
This stream manipulator forces cout to print the digits in fixed-point notation.
fixed
This is a variable, usually a bool or an int, that signals when a condition exists.
flag
________ reads a line of input, including leading and embedded spaces, and stores it in a string object.
getline
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" and then advance to a new line?
if (code == 'C') cout << "This is a check\n";
When a program lets the user know that an invalid choice has been made, this is known as:
input validation
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;
When a variable is assigned a number that is too large for its data type, it:
overflows
When converting some algebraic expressions to C++, you may need to insert ________ that do not appear in the algebraic expression.
parenthesis
A variable's ________ is the part of the program that has access to the variable.
scope
The ________ of a variable is limited to the block in which it is declared.
scope
This manipulator is used to establish a field width for the value immediately following it.
setw
The total number of digits that appear before and after the decimal point is sometimes referred to as:
significant digits AND precision
A character literal is enclosed in ________ quotation marks.
single
This function in C++ allows you to identify how many bytes of storage on your computer system an integer data value requires.
sizeof
The first step in using the string class is to #include the ________ header file.
string
The default section of a switch statement performs a similar task as the ________ portion of an if/else if statement.
trailing else
What is the value of the following expression? false || true
true
What is the value of the following expression? true && true
true
________ represent storage locations in the computer's memory.
variables
Which statement is equivalent to the following? x = x * 2;
x *= 2;
Which of the following expressions will determine whether x is less than or equal to y?
x <= y
This operator is known as the logical OR operator.
||
cout.fill( )
This member function of cout changes the fill character, which is a space by default. (fill characters are used when using setw( )).
Decrement
To decrease a value.
Increment
To increase a value.
Update the Expression
Typically a statement that increments/decrements a loop's counter variable.
Sequential Access File
You *access* the data from the beginning of the file to the end of the file.
Relational operators allow you to do what with numbers.
compare
Which statement is equivalent to the following? number += 1;
number = number + 1;
This operator performs a logical NOT operation.
!
This operator takes an operand and reverses its truth or falsehood.
!
The _______ causes the contents of another file to be inserted into a program.
#include directive
This operator represents the logical AND.
&&
You can use these to override the rules of operator precedence in a mathematical expression.
(Parentheses)
When this operator is used with string operands it concatenates them, or joins them together.
+
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
What is the value of donuts after the following code executes? int donuts = 10; if (donuts = 1) { donuts = 0; } else { donuts += 2; }
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 program display? #include <iostream> using namespace std; int main() { int a = 0, b = 2, x = 4, y = 0; cout << (a == b) << " "; cout << (a != b) << " "; cout << (b <=x) << " "; cout << (y > a) << endl; return 0; }
0 1 1 0
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
What is the value of cookies after the execution of the following statements? int number = 38, children = 4, cookies; cookies = number % children;
2
What will the value of result be after the following statement executes? result = 6 - 3 * 2 + 7 - 10 / 2 ;
2
What will the value of x be after the following statements execute? int x; x = 18 % 4;
2
Input values should always be checked for:
Appropriate range Reasonableness Division by zero, if division is taking place
Iteration
Each repetition of a loop.
Conditional Loop
Executes as long as a particular condition exists.
These are used to declare variables that can hold real numbers.
Floating point data types
What will the following code display? cout << "Four " << "score "; cout << "and " << "seven/n"; cout << "years" << "ago" << endl;
Four score and seven/nyearsago
loop header
It consists of the key word (while/for) followed by an expression enclosed in parenthesis.
Post-test loop
Its expression is tested AFTER each iteration. (do-while). It always performs at least one iteration. (Can be used as a user control loop).
A statement that starts with a # symbol is called a:
Preprocessor directive
Whereas < is called a relational operator, x < y is called a(n) ________.
Relational expression
Filename Extensions
Short sequences of characters that appear at the end of a filename preceded by a period (known as a "dot").
Pretest Loop
Tests its expression before each iteration. (while & for).
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!
In a C++ program, two slash marks (//) indicate:
The beginning of a comment
If you place a semicolon after the statement: if (x < y)
The compiler will interpret the semicolon as a null statement.
When the final value of an expression is assigned to a variable, it will be converted to:
The data type of the variable
Initialization Expression
The first expression in a for loop. It is normally used to initialize a counter variable to its starting value. (It is only done once in the loop).
________ must be included in any program that uses the cout object.
The header file iostream
What will the following code display? int number = 7; cout << "The number is " << "number" << endl;
The number is number
What will the following segment of code output? 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!
Given that, x = 2, y = 1, and z = 0, what will the following cout statement display? cout << "answer = " << (x || !y && z) << endl;
answer = 1
In C++ 11, the ________ tells the compiler to determine the variable's data type from the initialization value.
auto key word
A variable whose value can be either true or false is of this data type.
bool
Which data type typically requires only one byte of storage?
char
You want the user to enter the length, width, and height from the keyboard. Which cin statement is correctly written?
cin >> length >> width >> height;
The what causes a program to wait until information is typed at the keyboard and the Enter key is pressed.
cin object
The numeric data types in C++ can be broken into two general categories:
integer and floating point
In any program that uses the cin object, you must include the ________.
iostream header file
This manipulator causes the field to be left-justified with padding spaces printed to the right.
left
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';
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:
nesting
If you use a C++ key word as an identifier, your program will:
not compile
In memory, C++ automatically places a ________ at the end of string literals.
null terminator
Read position
Marks the location of the next byte that will be *read* from a file.
What is the output of the following code segment? int x = 5; if (x = 2) { cout << "This is true!" << endl; } else { cout << "This is false!" << endl; } cout << "This is all folks!" << endl;
This is true! This is all folks!
The function, pow(x, 5.0), requires this header file.
cmath
Random Access File or Direct Access File
You can jump directly to the desired data in this type of file without reading the data that comes before it.
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
You must have a ________ for every variable you intend to use in a program.
definition
A string literal is enclosed in ________ quotation marks.
double
What is the value of the following expression? true && false
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
What will be the value of result after the following code has been executed? int a = 60; int b = 15; int result = 10; if (a = b) { result *= 2; }
20
Given the following code segment, what is output after "result = "? int x = 1, y = 1, z = 1; y = y + z; x = x + y; cout << "result = " << (x < y ? y : x) << endl;
3
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
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.
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 will the value of x be after the following statements execute? int x; x = 18.0 / 4;
4
Postfix Mode
The operator is placed after the variable. The increment or decrement will happen last.
Prefix Mode
The operator is placed before the variable. The increment or decrement will happen first.
Input Validation
The process of inspecting data given to a program by the user and determining if it is valid.
Reading Data
The process of retrieving data from a file.
Writing Data
The process of saving data in a file.
Priming Read
The read operation that takes place just before a loop. It provides the first value for the loop to test.
body of a loop
The statement that is repeated if the condition is true.
Accumulator
The variable used to keep a running total.