CIS 251 Test 1
What will the following code display? int number = 6; cout << number++ << endl;
6
What will the following code display? int number = 6; cout << ++number << endl;
7
What will the following code display? int number = 6; ++number; cout << number << endl;
7
This step will uncover any syntax errors in your program.
Compiling
If you intend to place a block of statements within an if statement, you must place these around the block.
Curly braces { }
You may not use the break statement in a nested loop.
FALSE
What will the following code display? cout << "Four\n" << "score\n"; cout << "and" << "\nseven"; cout << "\nyears" << " ago" << endl;
Four score and seven years ago
Which is true about the following statement? cout << setw(4) << num4 << " ";
It allows four spaces for the value in the variable num4.
Words that have a special meaning and may be used only for their intended purpose are known as:
Key Words
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
Associativity is either right to left or:
Left to right
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
An example of a secondary storage device is:
The disk drive
The name for a memory location that may hold data is
Variable
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;
In any program that uses the cin object, you must include the ________.
iostream header file
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 manipulator causes the field to be left-justified with padding spaces printed to the right.
left
These operators connect two or more relational expressions into one, or reverse the logic of an expression.
logical
This is a control structure that causes a statement or group of statements to repeat.
loop
When an if statement is placed within the conditionally-executed code of another if statement, this is known as:
nesting
If you place a semicolon after the test expression in a while loop, it is assumed to be a(n):
null statement
Which statement is equivalent to the following? number += 1;
number = number + 1;
To write data to a file, you define an object of this data type.
ofstream
A file must be ________ before data can be written to or read from it.
opened
The do-while loop is considered a(n) ________ loop.
post-test
The while loop is this type of loop.
pre-test
This operator increments the value of its operand, then uses the value in context.
prefix increment
The ________ of a variable is limited to the block in which it is declared.
scope
This is a special value that marks the end of a list of values.
sentinel
The float data type is considered ________ precision, and the double data type is considered ________ precision.
single, double
This function in C++ allows you to identify how many bytes of storage on your computer system an integer data value requires.
sizeof
This may be used to write information to a file.
stream insertion operator
The first step in using the string class is to #include the ________ header file.
string
This statement uses the value of a variable or expression to determine where the program will branch to.
switch
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
Which one of the following would be an illegal variable name?
3dGraph
What is the value of average after the following code executes? double average; average = 1.0 + 2.0 + 3.0 / 3.0;
4.0
Arithmetic operators that share the same precedence have right to left associativity.
FALSE
C++ 11 introduces an alternative way to define variables, using the template key word and an initialization value.
FALSE
If you do not follow a consistent programming style, your programs will generate compiler errors.
FALSE
If you want to know the length of the string that is stored in a string object, you can call the object's size member function.
FALSE
In C++, it is impossible to display the number 34.789 in a field of 9 spaces with 2 decimal places of precision.
FALSE
In programming, the terms "line" and "statement" always mean the same thing.
FALSE
These are used to declare variables that can hold real numbers.
Floating point data types
This is a volatile type of memory, used for temporary storage.
RAM
Multiple relational expressions cannot be placed into the test condition of a for loop.
TRUE
The term "bit" stands for binary digit.
TRUE
Programmer-defined names of memory locations that may hold data are
Variables
In C++, key words are written in all lowercase letters.
TRUE
Internally, the CPU consists of two parts:
The Control Unit and the Arithmetic and Logic Unit
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;
When a variable is assigned a number that is too large for its data type, it:
overflows
This manipulator is used to establish a field width for the value immediately following it.
setw
Software engineering is a field that encompasses designing, writing, testing, debugging, documenting, modifying, and maintaining computer programs.
TRUE
The cin << statement will stop reading input when it encounters a newline character.
TRUE
The only difference between the get function and the >> operator is that get reads the first character typed, even if it is a space, tab, or the [Enter] key.
TRUE
The update expression of a for loop can contain more than one statement, e.g. counter++, total+= sales.
TRUE
When C++ is working with an operator, it strives to convert the operands to the same type.
TRUE
When the fixed manipulator is used, the value specified by the setprecision manipulator will be the number of digits to appear after the decimal point.
TRUE
When typing in your source code into the computer, you must be very careful since most of your C++ instructions, header files, and variable names are case sensitive.
TRUE
When writing long integer literals or long long integer literals in C++ 11, you can use either an uppercase or lowercase L.
TRUE
You should be careful when using the equality operator to compare floating point values because of potential round-off errors.
TRUE
string objects have a member function named c_str that returns the contents of the object formatted as a null-terminated C-string.
TRUE
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!
The numeric data types in C++ can be broken into two general categories:
integer and floating point
This is used to mark the end of a complete C++ programming statement.
Semicolon
In C++ the = operator indicates:
assignment
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 once
In C++ 11, the ________ tells the compiler to determine the variable's data type from the initialization value.
auto key word
What will the following loop display? int x = 0; while (x < 5) { cout << x << endl; x++; }
0 1 2 3 4
This operator performs a logical NOT operation.
!
This operator takes an operand and reverses its truth or falsehood.
!
Which of the following is a preprocessor directive?
#include <iostream>
The ________ causes the contents of another file to be inserted into a program.
#include directive
What is the modulus operator?
%
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.
+
These are operators that add and subtract one from their operands.
++ and --
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
How many times will the following loop display "Hello"? for (int i = 1; i < 20; i++) cout << "Hello!" << endl;
19
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
How many times will the following loop display "Hello"? for (int i = 0; i < 20; i++) cout << "Hello!" << endl;
20
How many times will the following loop display "Hello"? for (int i = 20; i > 0; i--) cout << "Hello!" << endl;
20
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
How many times will the following loop display "Hello"? for (int i = 0; i <= 20; i++) cout << "Hello!" << endl;
21
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
What will the following code display? int x = 0; for (int count = 0; count < 3; count++) x += count; cout << x << endl;
3
Assuming you are using a system with 1-byte characters, how many bytes of memory will the following string literal occupy? "William"
8
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
Look at the following statement. while (x++ < 10) Which operator is used first?
<
This operator is used in C++ to represent equality.
==
The ________ operator always follows the cin object, and the ________ operator follows the cout object.
>>, <<
The programmer usually enters source code into a computer using:
A text editor
What statement best describes a variable and its primary purpose?
A variable is a named storage location in the computer's memory used for holding a piece of information.
A set of well-defined steps for performing a task or solving a problem is known as a(n):
Algorithm
Which of the following best describes an operator?
An operator allows you to perform operations on one or more pieces of data.
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
During which stage does the central processing unit analyze the instruction and encode it in the form of a number, and then generate an electronic signal?
Decode
You must have a ________ for every variable you intend to use in a program.
Definition
The programming process consists of several steps, which include:
Design, Creation, Testing, and Debugging
This term refers to the programmer reading the program from the beginning and stepping through each statement.
Desk Checking
A variable called "average" should be declared as an integer data type because it will probably hold data that contains decimal places.
FALSE
A while loop is somewhat limited, because the counter can only be incremented by one each time through the loop.
FALSE
In a broad sense, the two primary categories of programming languages are:
Low-level and High-level
The statement: cin >> setw(10) >> str; will read up to this many characters into str.
Nine
When converting some algebraic expressions to C++, you may need to insert ________ that do not appear in the algebraic expression.
Parentheses
A CPU really only understands instructions that are written in machine language.
TRUE
A preprocessor directive does not require a semicolon at the end.
TRUE
A while loop's body can contain multiple statements, as long as they are enclosed in braces.
TRUE
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!
Which character signifies the beginning of an escape sequence?
\
A loop that is inside another loop is called:
a nested loop
The while loop has two important parts: 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
Given that, x = 2, y = 1, and z = 0, what will the following cout statement display? cout << "answer = " << (x || !y && z) << endl;
answer = 1
A variable whose value can be either true or false is of this data type.
bool
This statement causes a loop to terminate early.
break
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
A file ________ is a small holding section of memory that file-bound information is first written to.
buffer
The ________ causes a program to wait until information is typed at the keyboard and the Enter key is pressed.
cin object
This statement will pause the screen, until the [Enter] key is pressed.
cin.get();
You want the user to enter the length, width, and height from the keyboard. Which cin statement is correctly written?
cin.get(length, width, height);
This function tells the cin object to skip one or more characters in the keyboard buffer.
cin.ignore
Assuming dataFile is a file stream object, the statement: dataFile.close();
closes a file
The function, pow(x, 5.0), requires this header file.
cmath
When using the sqrt function you must include this header file.
cmath
Relational operators allow you to ________ numbers.
compare
This statement may be used to stop a loop's current iteration and begin the next one.
continue
This is a variable that is regularly incremented or decremented each time a loop iterates.
counter
To use the rand() function, you must #include this header file in your program.
cstdlib
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
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
If you want a user to enter exactly 20 values, which loop would be the best to use?
for
This loop is a good choice when you know how many times you want the loop to iterate in advance of entering the loop.
for
To allow file access in a program, you must #include this header file.
fstream
Every complete C++ program must have a ________.
function named main
________ reads a line of input, including leading and embedded spaces, and stores it in a string object.
getline
Which statement will read an entire line of input into the following string object? string address;
getline(cin, address);
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";
To read data from a file, you define an object of this data type.
ifstream
This means to increase a value by one.
increment
Something within a while loop must eventually cause the condition to become false, or a(n) ________ results.
infinite loop
In a for statement, this expression is executed only once.
initialization
What is the value of the following expression? false || true
true
What is the value of the following expression? true && true
true
What is the value of the following expression? true || true
true
A for statement contains three expressions: initialization, test, and
update
You may define a ________ in the initialization expression of a for loop.
variable
This is a pre-test loop that is ideal in situations where you do not want the loop to iterate if the condition is false from the beginning.
while
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
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
The default section of a switch statement performs a similar task as the ________ portion of an if/else if statement.
trailing else
When a relational expression is false, it has the value ________.
zero
This operator is known as the logical OR operator.
||
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
It is possible to define a file stream object and open a file in one statement.
TRUE
If you want to stop a loop before it goes through all its iterations, the break statement may be used.
TRUE
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
If the sub-expression on the left side of an && operator is false, the expression on the right side will not be checked.
TRUE
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
What is the output of the following code segment? n = 1; for ( ; n <= 5; ) cout << n << ' '; n++;
1 1 1 ... and on forever
What is the output of the following code segment? n = 1; while (n <= 5) cout << n << ' '; n++;
1 1 1... and on forever
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
In C++ 11 you can pass a string object as argument to a file stream object's open member function.
TRUE
What will the following code display? int number = 6 int x = 0; x = --number; cout << x << endl;
5
What will the following code display? int number = 6; int x = 0; x = number--; cout << x << endl;
6
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
What will the following code display? int number = 6; number++; cout << number << endl;
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 const int MY_VAL = 77; 7 MY_VAL = 99; 8 cout << MY_VAL << endl; 9 return 0; 10 }
7
Input values should always be checked for:
A) Appropriate range B) Reasonableness C) Division by zero, if division is taking place D) All of these
The total number of digits that appear before and after the decimal point is sometimes referred to as:
A) floating points B) significant digits C) precision D) B and C E) None of these
For every opening brace in a C++ program, there must be a:
Closing brace
________ are used to translate each source code instruction into the appropriate machine language instruction.
Compilers
The C++ language requires that you give variables names that indicate what the variables are used for.
FALSE
The condition that is tested by a while loop must be enclosed in parentheses and terminated with a semicolon.
FALSE
The default section is required in a switch statement.
FALSE
The fixed manipulator causes a number to be displayed in scientific notation.
FALSE
The following code correctly determines whether x contains a value in the range of 0 through 100. if (x >= 0 && <= 100)
FALSE
The following statement will output $5.00 to the screen: cout << setprecision(5) << dollars << endl;
FALSE
The increment and decrement operators can be used in mathematical expressions; however, they cannot be used in relational expressions.
FALSE
The scope of a variable declared in a for loop's initialization expression always extends beyond the body of the loop.
FALSE
When a program uses the setw manipulator, the iosetwidth header file must be included in a preprocessor directive.
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
Three primary activities of a program are:
Input, Processing, and Output
If the sub-expression on the left side of the || operator is true, the expression on the right side will not be checked.
TRUE
A(n) ________ is a set of instructions that the computer follows to solve a problem.
Program
Whereas < is called a relational operator, x < y is called a(n) ________.
Relational expression
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
An output file is a file that data is written to.
TRUE
As a rule of style, when writing an if statement you should indent the conditionally-executed statements.
TRUE
Both of the following if statements perform the same operation. if (sales > 10000) commissionRate = 0.15; if (sales > 10000) commissionRate = 0.15;
TRUE
C++ does not have a built in data type for storing strings of characters.
TRUE
Escape sequences are always stored internally as a single character.
TRUE
Floating point constants are normally stored in memory as doubles.
TRUE
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!
Which of the following defines a double-precision floating point variable named payCheck?
double payCheck;
The do-while loop is a(n) ________ loop that is ideal in situations where you always want the loop to iterate at least once.
post-test
When the increment operator precedes its operand, as in ++num1, the expression is in this mode.
prefix
Character constants in C++ are always enclosed in ________.
'single quotation marks'
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
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 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.5
An Integrated Development Environment typically consists of:
A) A text editor B) A compiler C) A debugger D) All of the above
Of the following, which is a valid C++ identifier?
A) June1997 B) _employee_number C) ___department D) myExtraLongVariableName E) All of the above are valid identifiers.
Which of the following is a common input device?
A) Keyboard B) Mouse C) Scanner D) Microphone E) All of the above
At the heart of a computer is its central processing unit. The CPU's job is:
A) To fetch instructions B) To carry out the operations commanded by the instructions C) To produce some outcome or resultant information D) All of the above
The ________ decodes an instruction and generates electrical signals.
Control Unit
Machine language is an example of a high-level language.
FALSE
Programs are commonly referred to as hardware.
FALSE
Pseudocode is a form of program statement that will always evaluate to "false."
FALSE
The preprocessor executes after the compiler.
FALSE
A(n) ________ is a diagram that shows the logical flow of a program.
Flowchart
What will the following code display? cout << "Four " << "score "; cout << "and " << "seven/n"; cout << "years" << "ago" << endl;
Four score and seven/nyearsago
What will the following code display? cout << "Four" << "score" << endl; cout << "and" << "seven" << endl; cout << "years" << "ago" << endl;
Fourscore andseven yearsago
Besides decimal, two other number systems you might encounter in C++ programs are:
Hexadecimal and Octal
These are data items whose values do not change while the program is running.
Literals
Mistakes that cause a running program to produce incorrect results are called:
Logic errors
What will the following code display? cout << "Monday"; cout << "Tuesday"; cout << "Wednesday";
MondayTuesdayWednesday
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
A(n) ________ is the most fundamental set of programs on a computer.
Operating System
Characters or symbols that perform operations on one or more operands are:
Operators
Which of the following is not one of the five major components of a computer system?
Preprocessor
A statement that starts with a # symbol is called a:
Preprocessor directive
This is used in a program to mark the beginning or ending of a statement, or separate items in a list.
Punctuation
The computer's main memory is commonly known as:
RAM
Even when there is no power to the computer, data can be held in:
Secondary storage
A character literal is enclosed in ________ quotation marks, whereas a string literal is enclosed in ________ quotation marks.
Single, double
Computer programs are also known as:
Software
The statements written by the programmer are called
Source code
In the process of translating a source file into an executable file, which of the following is the correct sequence?
Source code, preprocessor, modified source code, compiler, object code, linker, executable code.
This is a complete instruction that causes the computer to perform some action.
Statement
In programming terms, a group of characters inside a set of quotation marks is called a(n):
String literal
This is a set of rules that must be followed when constructing a program.
Syntax
The CPU is the most important component in a computer because without it, the computer could not run software.
TRUE
________ 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 does the term hardware refer to?
The physical components that a computer is made of
A variable declaration announces the name of a variable that will be used in a program, as well as:
The type of data it will be used to hold
The purpose of a memory address is
To identify the location of a byte in memory
________ represent storage locations in the computer's memory.
Variables
Which escape sequence causes the cursor to move to the beginning of the current line?
\r
This control sequence is used to skip over to the next horizontal tab stop.
\t
Which data type typically requires only one byte of storage?
char
The ________ is/are used to display information on the computer's screen.
cout object
During which stage does the central processing unit retrieve from main memory the next instruction in the sequence of program instructions?
fetch
Assume that a program has the following variable definition: char letter;
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";
A variable's ________ is the part of the program that has access to the variable.
scope