The following are examples of the types of questions that may appear on the final exam. The posted quiz questions are also another good place to find example questions.
What is wrong with the following definition for a C-style symbolic constant called PI?
#define PI = 3.24159
The C++ style of symbolic constant uses an equal sign as well as a semi-colon. For the symbolic constant above, the definition would be:
const double PI = 3.24159;
In either case, the compiler does not do any error checking to make sure that the appropriate (or correct) value is associated with the constant. That is up to the programmer.
Which of the following is a condition that will test if the value in the integer variable num is even?
To test if an integer contains an even value, check if the remainder is equal to 0 when the integer is divided by 2. If it is equal, then the integer is even. If it is not equal (i.e.. it is equal to 1 instead), then the integer is odd.
Which of the following (a-c) is a control structure?
Four control structures were discussed in lecture: sequence, decision (if and switch), repetition (for, while, and do...while), and branch and return (calling functions).
How many times will the following while loop print "hello"?
int i = 1; while( i <= 10 ) cout << "hello"
This is an infinite loop because the value of i is 1 and that is less than or equal to 10 and the value of i will remain as 1 because there is nothing in the loop body to change the value of i.
A function prototype does not have to __________.
A function prototype supplies the return data type, function name, number of arguments the function requires, and the data type of any arguments that the function requires. The argument names, if specified, are ignored.
What will be displayed by the following code?
int main() { int num = 3; fn(num); cout << num; return 0; } void fn( int n ) { n++; }
The contents of main's integer variable num does not get changed by the fn function because a copy of the contents of num is being passed to the function.
What is the subscript of the last element in the array Table if it is defined as follows?
float Table[30];
The subscripts for Table run from 0 through 29. In general, if an array is defined to hold n elements, then the subscripts run from 0 through n-1.
Which of the following statements (a-d) about Table is true?
float Table[] = { 0.9, 0.8, 0.7, 0.6 };
Statement a: FALSE. The number of elements that an array can hold can be omitted when the array is created, but ONLY if the array is initialized at time it is created.
Statement b: FALSE. Subscripts start at 0, therefore the value at subscript 1 is 0.8.
Statement c: FALSE. Null terminators are only in arrays of characters.
Statement d: FALSE. Arrays, like any other variable, can be initialized when they are created.
Which of the following "chunks" of C++ code will print the contents of an array called ar that has been defined to hold 25 integer values and has been initialized with 25 integer values?
int i;
cout << ar[];
for( i = 0; i == 25; i++ ) cout << ar[i] << " ";
for( i = 0; i < 25; i++ ) cout << ar[i] << " ";
for( i = 0; i < 25; i++ ); cout << ar[i] << " ";
"Chunk" a: This is a compile error because there is no value specified between the square brackets.
"Chunk" b: This will never execute because the condition is testing if i is equal to 25 and it is not because it was initialized to 0.
"Chunk" d: The semi-colon at the end of the for loop will cause the loop to "think" that there is no loop body. Therefore all that the loop will do is increment i from 0 to 25. The cout statement will then be executed and the value at ar[25] will be displayed - however 25 is not a valid subscript for array ar because it was defined to hold 25 elements. This means that a "garbage" or unknown value will be displayed (i.e.. whatever value happens to be immediately after the array will be displayed).
The following is a valid array definition.
int ar[];
The number of elements that an array can hold can be omitted when the array is created, but ONLY if the array is initialized at time it is created.
What will be displayed by the following code?
int main() { int num[5] = { 2, 4, 6, 8, 10 }; int i; fn(num); for( i = 0; i < 5; i++ ) cout << num[i] << ' '; return 0; } int fn( int n[] ) { n[3] = n[3] + 1; }
The contents of an array CAN be altered by a function because when an array is passed to a function its address (or location) is passed.
Which of the following statements (a-d) are true if the following lines of code are executed?
int G = 17; int &H = G;
The variable H is a local reference variable that has been assigned to the variable G. Therefore, any change that is applied to G will effect H and any change that is applied to H will effect G.
NOTE: don't forget that when a local reference variable is created it MUST be assigned to a variable (i.e.. the one that it will refer to). Once a local reference variable has been assigned to a variable it cannot be "moved" to another variable.
Which of the following is the correct definition for a variable called dblRef that can hold a reference to the double variable dblNum?
The null terminator is __________.
Which of the following string functions will ALWAYS produce a valid string after execution?
If a string will need room for 25 characters, how should it be defined so that it will be a valid string?
25 characters for the string plus 1 for the null terminator equals 26 characters total.
Which of the following statements (a-d) about classes is true?
The :: operator is used to associate a method with a specific class.
An instance of a class is known as a/an __________.
The class X contains two data members: an integer called xVal and a float called xPerc. Which of the following is not a valid prototype for the class definition for X?
void X( int, float); is not a valid prototype because only constructors can have the same name as the class and constructors NEVER have a return type.