Name: ___________________                       March 11, 2003

Queens College, CS211                                   Instructors: John Donath, Yosef Alayev

 

First Examination Version A

 

Note:  Following all directions will earn you 1 point.

 

Part I: (12 questions, 2 points each = 24 points)

Directions:  Answer the following questions by darkening the appropriate space on your Answer sheet.  Use a #2 pencil to darken the space.  When you are done, you should mark your final answers using a pen.  There is only one correct answer to each question.  You should do all your scratch work on either your own paper, or the fronts of the test booklet.

 

1.  Omitting the copy constructor may result in which of the following?

A:  Shallow copy when a copy of the class is made to an existing instance

B:  Shallow copy when an instance of a class is passed to a function as a value parameter

C:  Shallow copy when an instance of a class is returned from a function by reference

D:  Deep copy of the class when creating a new instance from an existing class.

E:  A memory leak

           

2.  What will be the output of the following code?

            int* p = new int[5];

            int *q = p - 1;

            for (int i = 0; i < 5; i++)

                 p[i] = i;

           cout << *(q + 3);

A:  -1               B:  0                 C:  1                 D:  2                 E: 3

 

3.  Which of the lines of code would be legal (ie. not generate a compiler error) as the next line after the one given below?

            const int* cp = new int[4];

A:  cp[0] = 3;               B:  *cp = 2;                  C:  int* const ci = cp + 3;                    

D:  cp = cp - 1;            E: None of the above

 

4.  The purpose of having the #ifndef macro in the .h file is:

A:  To prevent multiple declarations of the same class when included in multiple places

B:  To aid in debugging

C:  To set a constant

D:  To copy over a standard declaration file to different files accessing the same class

E:  All of the above


5.  Given the following code:

            class Remember {

                        public:

                                    Remember() { r = 0; }

                                    void count() {r++;}

                                    int get() {return r;}

                        private:

                                    static int r;

            };

 

            int main() {

                        Remember r;

                        r.count();

                        Remember s;

                        s.count();

                        r.count();

                        Remember::count();

                        cout << r.get();

                        return 0;

            }

What is the output of the above program?

A:  0                 B:  1                 C:  2                 D:  3                 E:  4

 

6.  What is the output of the following code?

            int v = 3;

            int *p = &v;

            int& r = v;

            v++;

            r++;

            p++;

            cout << r;

A:  3                 B:  4                 C: 5                  D:  6                

E: A hexadecimal memory address

 

7.  Which of the following is legal when overloading operators?

A:  Overloading operators with a different number of parameters than they have when defined with primitive data types (eg. 3 operands)

B:  Overloading the [] operator independent (outside) of a class

C:  Overloading operators where the operands are all primitive data types

D:  Overloading the = operator as part of a class

E:  Overloading the . operator.


8.  Which of the following lines contains illegal code?

A:  int a = new int;

B:  int* b = &a;

C:  int **c = &b;

D:  char* ch = new char[5];

E:   All of the above statements are legal code

 

9.  What is the problem with the code below?

            int **p = new int*[5];

            for (int i = 0; i < 5; i++)

                        p[i] = new int[3];

 

            // Some code goes here accessing and using the variables

 

            delete [] p;

 

A:  Dangling pointer

B:  Memory leak

C:  Segmentation fault

D:  Infinite looping

E:  There is nothing wrong with the code

 

10.  Given the following class declaration (The capital letters indicate places in the class, they are not actually parts of the class, they are the choices):

            A:

            class F

            {

                   B:

                        int a;

                        double b;

                    C:

                        int g();

                        void f();

                     D:

            }E:

At which location would the word “public” belong?


11. Use the code below to answer the question:

            void F:: abc(char* x) {

                        cout << x;

                        cin >> y;

            }

This function is most likely:

A:  Declared public in class F.

B:  Declared private in class F.

C:  Defined in a .h file.

D:  Defined in a file called main.cpp

E:  None of the above

 

12.  Given the following code:

class V {

      private:

          char p;

          int q;

     public:

          void jkl(char a);

          int mno();

   };

Which of the following is legal in a program that uses this class, after the following declaration:

V u;

A:  u.jkl(‘a’);

B:  u.p = ‘c’;

C:  V.jkl(‘a’);

D:  V.p = ‘c’; 


Part II (75 points)

Directions:  Answer the following question by writing code on the back of this test paper.   You should use a pen to provide your final answers, if you use a pencil, you waive your right to discuss grading of the exam after it is returned.  You should do all your scratch work on either your own paper, or on the front of the test booklet.

 

Given the following class:

            #ifndef BOOK_H

            #define BOOK_H

            // Stores information about a book

            class Book {

                        public:

                                    // Solicits the information stored in the class from the user

                                    Book();

 

                                    // Sets the class information to values passed as parameters

                                    Book(string t, string a, int i);

 

                                    // Accessor Fucntion for title

                                    string getTitle();

 

                                    // Accessor Function for author

                                    string getAuthor();

 

                                    // Accessor Function for isbn number

                                    int getISBN();

                                   

                        private:

                                    string title;

                                    string author;

                                    int isbn;

            };

            #endif

 

A:  Provide the .cpp file for the class Book (15 points)

 

B:  Provide the .h file for new class, Library, that stores a growing collection of Books.  It should have the following functions: (25 points)

1. addBook to add a new book to the library

2. Overload of the [] operator that returns the book indexed by i in the library

3. Overload of the << operator to print a summary of all the books in the library, including their titles, authors and isbn's

4.  Any other functions that may be necessary for the class

 

C:  Provide the .cpp file for the class Library. (35 points)