CSCI 2610
Practice questions for exam 2

The exam will probably be shorter than this, but these practice questions will prepare you for the exam.

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

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

  3. You would like to open a file called "mydata.txt" for reading. Write a declaration that will create a 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.

  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.

  5. 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;
      };
    

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

  7. 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;
         }
       }
    

  8. 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 size n of the array. A function heading is provided.
      int sum(int A[], int n)
    

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

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

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

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

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

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

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

  16. Given a 20x20 two-dimensional array T of integers, you would like to compute a single-dimensional array S of 20 integers, where for i = 0,1,...,19, S[i] is the sum T[i][0] + T[i][1] + T[i][2] + ... + T[i][19]. That is S[i] is the sum of all numbers in row i of of T. Write a function called rowSums that has T and S as parameters. It should assume that T already has values in it, and should compute the values of S as described.

  17. Write a function call to function rowSums of the preceding exercise. Make it put the row sums of two-dimensional array called Table into array Sums.

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

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