CSCI 240 Sample Final Exam Answer Key

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.

Beginning Material -- material through the midterm exam

  1. What is wrong with the following definition for a C-style symbolic constant called PI?

  2. #define PI = 3.24159
    1. The actual value of the mathematical constant pi is not 3.24159
    2. There should be a semi-colon at the end of the line
    3. There should not be an equal sign
    4. The name PI is a reserved word and therefore cannot be used
    5. There is nothing wrong with the definition

    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.

  3. Which of the following is a condition that will test if the value in the integer variable num is even?

    1. (num % 2) == 0
    2. (num / 2) == 0
    3. (num % 2) == 1
    4. (num / 2) == 1
    5. none of the above

    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.

  4. Which of the following (a-c) is a control structure?

    1. sequence structure
    2. repetition structure
    3. decision structure
    4. all of the above are control structures
    5. none of the above are control structures

    Four control structures were discussed in lecture: sequence, decision (if and switch), repetition (for, while, and do...while), and branch and return (calling functions).

  5. How many times will the following while loop print "hello"?

  6. int i = 1;
      
    while( i <= 10 )
      cout << "hello"
    1. 10
    2. 8
    3. an infinite number of times
    4. 0

    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.

  7. A function prototype does not have to __________.

    1. include argument names
    2. end with a semi-colon
    3. agree with the function header
    4. match with all calls of the function

    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.

  8. What will be displayed by the following code?

  9. int main()
      {
      int num = 3;
      
      fn(num);
      cout << num;
      
      return 0;
      }
      
      
    void fn( int n )
      {
      n++;
      }
    1. The code will not display anything
    2. 3
    3. 4
    4. 5
    5. There is not enough information provided to determine what will be displayed.

    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.

Arrays

  1. What is the subscript of the last element in the array Table if it is defined as follows?

  2. float Table[30];
    1. 30
    2. 31
    3. 29
    4. 0
    5. none of the above

    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.

  3. Which of the following statements (a-d) about Table is true?

  4. float Table[] = { 0.9, 0.8, 0.7, 0.6 };
    1. The definition for Table will cause an error because there is no value specified inside of the square brackets
    2. Table[1] contains the value 0.9
    3. There are actually 5 values stored in Table - the four listed and a null terminator as the fifth element
    4. The definition for Table will cause an error because an array cannot be initialized when it is created
    5. All of the statement (a-d) are false

    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.

  5. 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?

  6. int i;
    1. cout << ar[];
    2. for( i = 0; i == 25; i++ )
        cout << ar[i] << " ";
    3. for( i = 0; i < 25; i++ )
        cout << ar[i] << " ";
    4. 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).

  7. The following is a valid array definition.

  8. int ar[];
    1. true
    2. 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.

  9. What will be displayed by the following code?

  10. 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;
      }
    1. The code will not display anything
    2. 2 4 6 8 10
    3. 2 4 7 8 10
    4. 2 4 6 9 10
    5. There is not enough information provided to determine what will be displayed.

    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.

References

  1. Which of the following statements (a-d) are true if the following lines of code are executed?

  2. int G = 17;
    int &H = G;
    1. H is now an alternate name for G
    2. Adding 1 to H will change the value of G to 18
    3. Subtracting 5 from G and printing H will display the value 12
    4. The condition H == G will give a true result
    5. All of the statements (a-d) are true

    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.

  3. Which of the following is the correct definition for a variable called dblRef that can hold a reference to the double variable dblNum?

    1. double dblRef = dblNum;
    2. double *dblRef = &dblNum;
    3. double &dblRef = dblNum;
    4. double &dblRef;

C++ Strings & string functions

  1. The null terminator is __________.

    1. used to advance to a new line when executing a cout statement
    2. represented as '\0'
    3. used to end a valid string
    4. a movie starring Arnold Schwarzenegger
    5. All of the statements (a-d) are correct
    6. Only statements b and c are correct

  2. Which of the following string functions will ALWAYS produce a valid string after execution?

    1. strcpy
    2. strcat
    3. Both of the functions (a-b) will produce a valid string
  3. If a string will need room for 25 characters, how should it be defined so that it will be a valid string?

    1. char str[25];
    2. char str[24];
    3. char str[26];
    4. char str[];

    25 characters for the string plus 1 for the null terminator equals 26 characters total.

Object Oriented Programming

  1. Which of the following statements (a-d) about classes is true?

    1. A class can contain both data members and methods
    2. The members of a class may be private or public
    3. The name of the constructor is always the same as the name of the class
    4. The class definition contains prototype statements for the methods
    5. All of the statements (a-d) are true.

  2. The :: operator is used to associate a method with a specific class.

    1. true
    2. false

  3. An instance of a class is known as a/an __________.

    1. variable
    2. method
    3. object
    4. thing

  4. 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?

    1. void X( int, float );
    2. void setxVal( int );
    3. float getxPerc();
    4. X( int, float );
    5. void printX();

    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.