Computer Science 1 Final
What is the value of donuts after the following code executes? int donuts = 10; if (donuts = 1) donuts = 0; else donuts += 2;
0
What will the following loop display? int x = 0; while (x < 5) { cout << x << endl; x++; }
0 1 2 3 4
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; }
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 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 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 5: total = total + 6; case 6: 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
After the 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 output of the following program? #include <iostream> using namespace std; void doSomething(int&); int main ( ) { int x = 2; cout << x << endl; doSomething(x); cout << x << endl; return 0; } void doSomething(int& num) { num = 0; cout << num << endl; }
2 0 0
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 = 0; i <= 10; 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 x is 5, y is 6, and z is 8, which of the following is false? x == 5; 7 <= (x + 2); z <= 4; (1 + x) != y; z >= 8; x >= 0; x <= (y * 2);
3 and 4 are false z <= 4; (1 + x) != y;
What is the output of the following program? #include <iostream> using namespace std; void showDub(int); int main ( ) { int x = 2; showDub(x); cout << x << endl; return 0 ; } void showDub(int num) { cout << (num * 2) << endl; }
4 2
What will the following code display? int numbers[ ] = {99, 87, 66, 55, 101}; cout << numbers[3] << endl;
55
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?
<
What will the following segment of code output if 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
A local variable and a global variable may not have the same name within the same program.
False
A statement that starts with a # is called a comment.
False
Assume array1 and array2 are the names of arrays. To assign the contents of array2 to array1, you would use the following statement. array1 = array2
False
If you use a C++ key word as an identifier, your program will compile, link, but not execute.
False
The following code correctly determines whether x contains a value in the range of 0 to 100. if (x >= 0 && <=100)
False
The scope of a variable declared in a for loop's initialization expression always extends beyond the body of the loop.
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: "; if (x) cout << "true" << endl; else cout << "false" << endl;
False
When a function is called, flow of control moves to the function's prototype.
False
When using the equality operator to compare integer values there will be potential round-off errors.
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
You must furnish an argument with a function call.
False
What will the following segment of code output? int = 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!
An example of a secondary storage device is a hard drive.
True
An individual array element can be processed like any other type of C++ variable.
True
As a rule of style, when writing an if statement you should indent the conditionally-executed statements.
True
Each individual element of an array can be accessed by the array name and an element number, called a subscript.
True
Floating point constants are normally stored in memory as doubles.
True
If an array is partially initialized, the uninitialized elements will be set to zero.
True
If the sub-expression on the left side of an && operator is false, the expression on the right side will not be checked.
True
If the sub-expression on the left side of the || operator is true, the expression on the right side will not be checked.
True
It is not considered good programming practice to declare all of your variables globally.
True
One reason for using functions is to break programs into manageable units, or modules.
True
The following code correctly determines whether x contains a value out of the range of 0 through 100. if (x < 0 || x > 100)
True
Variables need to be declared before they can be used.
True
When you pass an array as an argument to a function, the function can modify the contents of the array.
True
You may use the exit( ) function to terminate a program, regardless of which control mechanism is executing.
True
You should be careful when using the equality operator to compare floating point values because of potential round-off errors.
True
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
Input values should always be checked for:
appropriate range reasonableness division by zero, if division is taking place
Unlike regular variables, these can hold multiple values.
arrays
In C++ the = operator indicates:
assignment
Subscript numbering in C++
begins with 0
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
Functions are ideal for use in menu-driven programs. When a user selects a menu item, the program can ___________ the appropriate function.
call
This statement may be used to stop a loop's current iteration and begin the next one.
continue
In a function, you must furnish:
data type(s) of the parameters data type of the return value the name of the function names of parameter variables
These types of arguments are passed to parameters automatically if no argument is provided in the function call.
default
A function __________ contains the statements that make up the function.
definition
Look at the following function prototype. int myFunction (double); What is the data type of the function's parameter variable?
double
Local variables are initialized to zero by default.
false
If you want a user to enter exactly 20 values, which loop would be 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
An array can easily be stepped through by using a
for loop
This is a collection of statements that performs a specific task.
function
A ___________ variable is declared outside all functions.
global
If a function does not have a prototype, default arguments may be specified in the function ____________.
header
In a for statement, this expression is executed only once.
initialization
A(n) ___________ can be used to specify the starting values of an array.
initialization list
Arrays may be _________ at the time they are __________.
initialized, declared
When a program lets the user know that an invalid choice has been made, this is known as:
input validation
It is ________ to pass an argument to a function that contains an individual array element, such as numbers[3].
legal in C++
Which line in the following program contains a call to the showDub function? #include <iostream> using namespace std; void showDub(int); int main ( ) { int x = 2; showDub(x); cout << x << endl; return 0; } void showDub(int num) { cout << (num * 2) << endl; }
line 10 void showDub(x);
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 8 if (number >= 0 && <= 100)
To pass an array as an argument to a function, pass the ___________ of the array.
name
When an if statement is placed within the conditionally-executed code of another if statement, this is known as:
nesting
The _________ is automatically appended to a character array when it is initialized with a string constant.
null terminator
A file must be _______ before data can be written to or read from it.
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;
The do-while loop is considered a(n) ___________ loop.
post-test
Which of the following is not one of the five major components of a computer system? - preprocessor - cpu - main memory - i/o devices - secondary storage
preprocessor
A two-dimensional array can be viewed as _________ and __________.
rows, columns
Given the following declaration, where is 77 stored in the scores array? int scores[ ] = {83, 62, 77, 97};
scores [2]
The value in this type of local variable persists between function calls.
static
This value in a(n) __________ variable persists between function calls.
static local
An array of string objects that will hold 5 names would be declared using which statement?
string names [5];
By using the same ________ you can build relationships between data stored in two or more arrays.
subscript
To access an array element, use the array name and the element's
subscript
If you place a semicolon after the statement if (x < y)
the compiler will interpret the semicolon as a null statement
An array can store a group of values, but the values must be
the same data type
The default section of a switch statement performs a similar task as the _________ portion of an if/else if statement.
trailing else
Both of the following if statements perform the same operation. if (sales > 10000) commissionRate = 0.15; if (sales > 10000) commissionRate = 0.15;
true
What is the value of the following expression? true && true
true
A for statement contains three expressions: initialization, test, and:
update
_____________ represent storage locations in the computer's memory.
variables
Which of the following expressions will determine whether x is less than or equal to y? x > y x =< y x <= y x >= y
x <= y
When a relational expression is false, it has the value _________.
zero