CSCI 2610
Practice questions for exam 2

The exam will be shorter than this but these practice questions will prepare you for the exam. You will have 90 minutes, and can use one page of prepared notes.

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

  2. What is the responsibility of a constructor for a class? What do you use it for?

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

  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. That is, the definition of area is physically outside the class. Use 3.14159 as an approximation of pi.

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

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

  8. Suppose that array A contains the following.

        i      A[i]
        0       3
        1       7
        2       8
        3       5
    
    Function mystery is shown below. Starting with A holding what is shown above, 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;
         }
       }
    

  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)
    

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

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

  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?

  13. Write a function that has a single null-terminated string parameter. It should return 1 if the string contains the letter '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.

  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?

  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?

  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?

  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? (Note: each time around the loop, the size of the part that needs to be searched is cut in half. If you start with n, and each time around the loop you cut the size in half, how many times do you go around the loop before there is only one left?)

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

  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?

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

  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.

  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.

  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