Test I, CSCI 230, Spring 2018


  1. Abstract Data Type (ADT)
    1. [4 points] Give the definition for an abstract data type.
    2. [6 points] Give an example of a C++ type which is an abstract data type. Explain why the type you selected is an ADT. Please make sure you demonstrate how the type you select corresponds to the definition of an ADT.
  2. Recursion
    1. [1 point] What is recursion?
    2. [3 points] Name three characteristics of a successful recursive algorithm/function.
    3. [8 points] Give pseudo-code for a recursive version of binary search on a string. Clearly label each of the characteristics from the previous answer in your pseudo-code.
    4. [6 points] A function X can be defined as follows:



      Give complete code for a recursive function which computes X(n,a). For this function n is an unsigned short, a is a double.

    5. [4 points] Is a recursive implementation always the best choice? Why or why not. Why would a programmer choose to implement a recursive solution?
  3. Strings
    1. [2 points] Describe the conditions which must be met to use the [] operator on a string.
    2. [2 points] What is the difference between the .at() function and the [] operator for strings.
    3. [6 points] Write a complete function that takes two strings and returns a bool. The return value should be true if the two strings contain the same letters in the same order regardless of case. You may not compare the two strings directly in this function.
  4. Common errors [4 points each] What happens when the following code is executed? Describe the error and the cause of the error in detail. How can the error be corrected?
    1. string s;
      size_t i;
      for(i=s.size()-1;i>=0;i--) {
         cout << s[i];
      }
      
    2. ifstream inFile;
      string word;
      
      inFile.open("input.txt");
      while (inFile) {
         inFile >> word;
         cout << "The word is " << word << endl;
      }