Final Exam, CSCI 230, Fall 2020


  1. Abstract Data Type: An abstract data type (ADT) is defined as a data type whose properties(domain and operations) are specified independently of any particular implementation.
    1. [1 point] What is the domain of an ADT?
      The domain of an ADT is the set of values that the ADT can hold or contain. This is the data that the ADT can store.
    2. [1 point] What are the operations of an ADT?
      The operations of an ADT are the functions that can manipulate the ADT.
    3. [4 points] Why is it important that the definition of an ADT does not rely on the implementation?
      It is important that the definition of the ADT does not relay on the implementation because this allows the implementer of the ADT to change the underlying implementation without impacting the users of the ADT.
    4. [2 points] What C++ mechanism is used to implement an ADT?.
      A class.
    5. [4 points] Describe two mechanisms used in C++ to accomplish information hiding.
      There are two basic mechanisms
      1. The ability to declare class members as private
      2. The separation of the declaration in the header file from the implementation in the implementation file.
  2. Classes vs Structures.
    1. [1 point] What is the purpose of a structure in C++?
      A structure is a way to group heterogeneous data into a single item.
    2. [2 points] When should a programmer select a structure over a class when implementing a program in C++?
      When only data is involved, there are no methods.
  3. File Processing.
    1. [2 point] Describe the operation of the ifstream member function eof.
      EOF indicates that the stream failed because there was an attempt to read past the end of the file.
    2. [2 point] describe the exact conditions which must occur for the ifstream member function eof to return true.
      EOF is true only AFTER the program has attempted to read beyond the end of the file.
    3. [6 point] Describe (exactly) the output from the following code if the file input.dat contains the single value 1. Any error in this description will result in loss of points. Explain how the output value was calculated.
      The output will be 2.

      The program will read the value of 1 from the file and add that to sum, which will become 1. The value of EOF will be false, because the code has not yet read past the end of file so the loop will execute a second time. This time the input to data will fail, and data will be unchanged. Sum will be changed to the value of 2.

      #include <iostream>
      #include <fstream>
      
      using namespace std;
      
      int main() {
      
          ifstream inFile;
          int sum = 0;
          int data;
      
          inFile.open("input.dat");
      
          while(not inFile.eof()) {
              inFile >> data;
              sum += data;
          }
      
          inFile.close();
      
          cout << sum << endl;
      
          return 0;
      } 
  4. Dynamic Memory
    1. [2 points] What does the C++ new operator do?
      The new operator allocates the correct amount of memory from the heap AND returns a pointer to this memory.
    2. [2 points] When implementing a class with dynamic memory, why is a destructor required?
      The destructor is required to eliminate a memory leak when the instance of the class goes out of scope and lifetime.
    3. When an class object is passed by value
      1. [1 point] what member function is called?
        The copy constructor is called.
      2. [2 points] What must this member function do?
        It must copy all data members of the source class to the destination class. This includes a deep copy of all dynamic memory.
      3. [2 points] Describe the parameter(s) to this function and how they must be passed.
        A source must be passed by reference.
    4. [2 points] Describe the difference between a deep copy and a shallow copy.
      A shallow copy only copies the data stored in a variable. A deep copy will allocate new memory and copy the data stored in the dynamic memory of the source object.
  5. Lists
    1. [4 points] Describe the ADT list. Include all relevant portions from the definition of an ADT.
      A list is a homogeneous collection of 0 or more data items accessed in a sequential manner. Operations include
      • IsEmpty
      • IsFull
      • Length
      • IsPresent(key)
      • Delete(key)
      • Insert(key)
      • Reset
      • HasNext
      • Next
      • GetItem
    2. [2 points] How is a list different from an array?
      Arrays support random access, lists support sequential access. (There are other considerations but we did not discuss this in detail)
    3. [6 points] Describe in detail how a list can be implemented in an array.
      The list is stored in an array. The programmer must keep track of the size of the list.

      Insertion and deletion are performed by moving all the data past the insertion/deletion point through the end of the array.

      The "current" pointer is an index into the array.

      This will be a highly individualized answer. I will judge the results based on completeness.
    4. [4 points] Describe in detail how a list can be implemented as a linked structure.
      A node is a struct/class that holds a data item and a link to the next node.

      The head pointer points to the first element in the list.

      The head pointer initially points to nullptr.

      The list is maintained by making sure that the list is always terminated by the nullptr.

      This will be a highly individualized answer. I will judge the results based on completeness.
    5. [2 points] What advantages are gained by implementing a list as a linked structure?
      Insertion and deletion can be performed in O(1) time once the location is found.

Submission Instructions