Computer Science 2610

Spring 2000

Quiz 3

 

Answer all of the questions.  For the multiple choice questions, circle the letter of the best answer.  You have 25 minutes.

 

  1. Which of the following is true about classes and objects?

(a)   A class is a plan for constructing objects.

(b)  An object is a plan for constructing classes.

(c)   Classes and objects are unrelated.

(d)  Classes should not be used in large programs, but objects should.

 

  1. Which of the following is true about constructors for classes?

(a)   Every class must have at least one constructor written inside it.

(b)  The name of a constructor must not be the same as the name of the class that it is inside.

(c)   A constructor is usually used to initialize the variables of an object.

(d)  A constructor is not allowed to use the private variables of the object to which it belongs.

 

  1. You would like to open a file called “mydata.txt” for writing.  Which of the following will do the job?

(a)   ifstream “mydata.txt”;

(b)  ofstream “mydata.txt”;

(c)   ifstream fil(“mydata.txt”);

(d)  ofstream fil(“mydata.txt”);

 

  1. Which of the following is a correct C++ definition of an array called jam of 30 characters?

(a)   char jam[31];

(b)  char jam[30];

(c)   char jam(31);

(d)  char jam(30);

 


  1. Function mystery is shown below.  Suppose that array A holds 4 integers, and mystery(A,4) is run.  What is the content of array A afterwards?  Be careful on this problem.  Do a careful hand simulation.

 

     void mystery(int A[], int n)

     {

       int k;

       for(k = 0; k < n; k++) A[k] = k;

       for(k = 0; k < n-1; k++) A[k+1] = A[k];

     }

 

(a)   It is impossible to say from the information given.

(b)  A[0] = 0, A[1] = 0, A[2] = 0, A[3] = 0

(c)   A[0] = 0, A[1] = 0, A[2] = 1, A[3] = 2

(d)  A[0] = 0, A[1] = 1, A[2] = 2, A[3] = 3

 

  1. Class Circle is defined as follows.

 

class Circle

{

  public:

    Circle(Point Center, double radius);

    double perimeter();

 

  private:

    Point myCenter;

    double myRadius;

};

 

Write a definition of function perimeter.  Expression c.perimeter() should return the perimeter of circle c.  Write it as it would occur if written outside of the class.