Computer Science 2610
Solutions to practice questions for exam 2

  1. What is the difference between an object and a class?

    An object holds variables and functions. You can put an object in a variable. A class is a template for producing objects. It is just a type of objects.

  2. What is the responsibility of a constructor for a class?

    The constructor of a class is used to initialize the objects that the class produces.

  3. You would like to open a file called "mydata.txt" for reading. Write a declaration that will create an object that can be used to read from that file, in a way similar to the way the object cin is used to read from the standard input.

       ifstream in("mydata.txt");
    

  4. You would like to open a file called "myout" for writing. Write a declaration that will create an object that can be used to write to that file, in a way similar to the way the object cout is used to write to the standard output.

       ofstream out("myout");
    

  5. Write a function that takes a file name as a parameter, and that copies the entire content of that file to the standard output. That is, it should read the file and write it to cout. If the file cannot be opened, the function should return without printing anything.

       void copyfile(char name[])
       {
         char c;
    
         ifstream in(name);
         if(in.fail()) return;
    
         while(1) {
           in.get(c);
           if(in.eof()) break;
           cout << c;
         }
         in.close();
       }
    

  6. A circle in the plane can be described by its center point and its radius. Class Circle describes objects that are circles in the plane, and its definition is partially given below. Some operations on circles are provided. For example, function area returns the area of a circle. Write the definition of function area, as it would occur in file circle.cc.

      class Circle 
      {
        public:
          Circle(Point center, double radius);
          double area();
          ...
        private:
          Point myCenter;
          double myRadius;
      };
    

        double Circle::area()
        {
          return 3.14159265*myRadius*myRadius;
        }
    

  7. Write a C++ declaration for an array called Yarn that holds 25 integers.

       int Yarn[25];
    

  8. Suppose that array A contains the following.

        i      A[i]
        0       3
        1       7
        2       8
        3       5
    
    Function mystery is shown below. What is in array A after running mystery(A,4)?
       int mystery(int B[], int n)
       {
         int k;
         for(k = 0; k < n; k++) {
           B[k] = B[k] + k;
         }
       }
    

        i      A[i]
        0       3
        1       8
        2      10
        3       8
    

  9. Write a function that computes the sum of all of the numbers in an array of integers. There should be two parameters, the array A and the logical size n of the array. A function heading is provided. Use a loop for this function.

      int sum(int A[], int n)
      {
        int sm = 0;
        int k;
        for(k = 0; k < n; k++) {
          sm = sm + A[k];
        }
        return sm;
      }
    

  10. Give an interesting (that is, nontrivial) loop invariant for the loop in your function sum from the preceding exercise.

    sm = A[0] + ... + A[k-1]

  11. Write a definition of function sum that has the same meaning as before, but use recursion for this definition instead of a loop.

      int sum(int A[], int n)
      {
        if(n == 0) return 0;
        else return A[n-1] + sum(A, n-1);
      }
    

  12. You would like to set variable s to the sum of all of the integers in array Fish, which has 12 members. What statement would you write to use function sum to do the computation?

        s = sum(Fish,12);
    

  13. Write a function that has a single null-terminated string parameter. It should return 1 if the string contains an 'b', and 0 if it has no occurrences of the letter 'b'. Call it anyBs. For example, anyBs("bat") = 1, anyBs("rabbit") = 1, but anyBs("dog") = 0.

    Using a loop:

      bool anyBs(char s[]) 
      {
        for(int k = 0; s[k] != '\0'; k++) {
          if(s[k] == 'b') return 1;
        }
        return 0;
      }
    
    Using recursion and pointers:
      bool anyBs(char s[])
      {
        if(*s == '\0') return 0;
        else if(*s == 'b') return 1;
        else return anyBs(s+1);
      }
    

  14. To within a constant factor (that is, in terms of proportionality) how much time does it take to sort an array of n integers using insertion sort, in the worst case?

    n2

  15. To within a constant factor, how much time does it take to sort an array of n integers using quicksort, in the average case?

    n log2(n)

  16. To within a constant factor, how long does it take to do a linear search of an array of n integers, in the worst case?

    n

  17. To within a constant factor, how long does it take to do a binary search of a sorted array of n integers, in the worst case?

    log2(n)

  18. With an error of no more than 1/2, what is log base 2 of 2000?

    11

  19. When passing an array as a parameter to a function, you usually also pass the logical size of an array. That is not necessary when the array is a null-terminated string? Why not?

    You can compute the length of a null-terminated string using strlen. So it is not necessary to be told the length.

  20. What is the difference between the logical and physical size of an array?

    The physical size of the array tells how much space is available to be used. The logical size tells how much is actually in use.

  21. A dangling pointer is

    1. a pointer to memory that has been deleted.
    2. a pointer to memory that is no longer in use.
    3. a pointer to memory that also has other pointers to it.
    4. a pointer that was created by using new.

  22. If Node is a type, then expression new Node returns a value of type

    1. Node
    2. Node*
    3. Node**
    4. Node&

  23. The C++ keyword used to return memory to the pool of available memory is

    1. new
    2. free
    3. release
    4. delete

  24. The run-time stack is characterized by

    1. automatic allocation and automatic deallocation of memory
    2. automatic allocation and explicit deallocation of memory.
    3. explicit allocation and automatic deallocation of memory.
    4. explicit allocation and explicit deallocation of memory.

    Remark: The run-time stack is the area of memory where local variables of functions are placed. Local variables are allocated automatically when a function starts, and they are deallocated automatically when the function returns.

  25. The heap is characterized by

    1. automatic allocation and automatic deallocation of memory
    2. automatic allocation and explicit deallocation of memory.
    3. explicit allocation and automatic deallocation of memory.
    4. explicit allocation and explicit deallocation of memory.

    Remark: The heap is the area of memory where memory that is allocated using new is located. Memory in the heap is allocated explicitly using new, and deallocated explicitly using delete.

  26. Which of the following will copy the null-terminated string that is in array src into array dest?

    1. dest = src;
    2. dest == src;
    3. strcpy(dest, src);
    4. strcpy(src, dest);

  27. Which of the following allocates a new array A in the heap, holding 10 bytes?

    1. char A[10];
    2. int A[10];
    3. char* A = new char[10];
    4. int* A = new int[10];

  28. Which of the following is true if null-terminated strings A and B are the same string?

    1. strcmp(A,B) != 0
    2. strcmp(A,B) == 0
    3. A = B
    4. A == B