Name: ______________                                                                              October 17, 2002

Queens College                                                                                    Instructors: John D. Donath, Yosef Alayev

 

CS211 – First Examination Version A

 

Part I (30 points)

Directions:  Answer the following 12 questions by blackening with your #2 pencil the appropriate letter corresponding to the correct answer on your Scantron form.  Be sure to write your name and test version on the Scantron form.  After you finish answering the questions, using a pen, put a line through each of your final answers.  Failure to follow these directions may result in improper grading of your test paper.

 

1.  What would the following program output (Choose the appropriate letter on your scantron)?

                char ch[5];

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

                                ch[j] = (char) j + ‘A’;

                char* sp = ch + 2;

                cout << sp[1];

 

2.  Which of the following is true about programmer defined operator overloading?

A: An overloaded operator can have three or more parameters

B:  Any symbol can be overloaded and become an operator

C:  Operators can be overloaded as members of a class

D:  Operators involving primitive data types can be overloaded.

E:  Overloaded operators must return a const data type,

 

Use the following program segment to answer question 3:

                1.  int num = ‘A’;

                2.  char ch = num + 3;

                3.  char* str = num + ch;

                4.  if (ch > num)

                5.      cout << “ASCII value of: “ << ch << “ is larger than “ << num << endl;

                6.  else

                7.     cout << “ASCII value of: “ << ch << “ is smaller than “ << num << endl;

3.  Which line will cause a compiler error?

A:  1                        B:  2                                        C:  3                                        D:  4                        E:  5 and 7

 

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();

                                char* b();

                       C:

                                int f;

                                string g;

                         D:

                }E:

4.  At which location would the word “private belong?


class Z {

     public:

          void def(char a);

          int ghi();

      private:

          char j;

          int k;

   };

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

Z w;

A:  w.def(‘b’);

B:  w.k = 54;

C:  Z.def(‘b’);

D:  Z.k = 43;

E:  None of the above are legal.

 

6.  What is the output of the following code?

                int q = 5;

                int *p = &q;

                *p = 3;

                cout << p;

A:  3                        B:  4                                        C:  5

D:  A hexadecimal memory address                   E:  The above code is illegal, and will not compile

 

7.  Which of the following lines of code shows an attempt to access a dangling pointer, assuming this code represents five consecutive lines of a program?

A:  int* a = new int[5];

B:  *a = 6;

C:  int *c = a;

D:  delete [] a;

E:  c[2] = 3;

 

Use the following code to answer question 8:

1.             int* p1 = new int[6];

2.            int* p2 = p1 – 1;

3.             p1[2] = 3;

4.             *(p2 + 3) = 4;

5.             int n1 = p1[2];

6.             cout << n1;

7.             delete [] p1;

 

8.  What is the output of the above program?

A:  3                        B:  4                        C:  A hexadecimal address                  D:  An unknown number

E:  The code above is illegal.

 

9.  Given the following code:

                int* array = new int[5];

                int const* p1 = array + 3;

                const int* p2 = array + 2;

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

Which of the following would be legal as the body of the for loop to initialize all elements to zero using the pointers p1 or p2?

A:  p1[i] = 0;

B:  *(p1 + i – 3) = 0;

C:  *(p2 + i) = 0;

D:  p2[i-2] = 0;

E:  It is impossible to access the array given the pointers p1 and p2.

               
10.  Given the following code, what does the following code output?

class Counter {
        public:
                static int next() {return ++turn; }
        private:
                static int turn;
};
 
int Counter::turn = 1;
 
int main() {
        Counter c, d;
        for (int i = 0; i < 3; i++) {
                c.next();
                d.next();
        }
        cout << c.next();
        return 0;
}    
A:  1
B:  3
C:  4
D:  7

E:  8

 

Given the following class (for questions 11 & 12):

                class MyString {

                                public:

                                                // Operator Overload declaration belongs here

                                private:

                                                char* str;

                };

11.  Which of the following would be a legal line at the place indicated by the comment that will help overload the + operator to concatenate two instances of this class?

A:  const MyString operator+(const MyString&);

B:  friend const MyString operator+(const MyString&, const MyString&);

C:  const MyString& operator+(const MyString&);

D:  friend const MyString operator+(const MyString&);

E:  Choices A & B are both valid options

 

12.  The above class would not be complete without:

A:  A destructor

B:  Overload of the = operator

C:  Overload of the Copy Constructor

D:  Choices B & C are both correct

E:  Choices A, B, & C are all correct

 


Part II (60 points)

Directions:  Answer the question in this part by writing code in your blue examination booklet.  You should answer your questions using blue or black ink.  If you answer in pencil, you waive your right to discuss grading of the examination.  Neatness counts, and you will loose points for sloppy work.

 

Note:  You do not need to comment the code.

 

Write the .h and .cpp files for a class called Tokenize that stores an array of strings.  This array can have any length, and may increase during the program.  It also stores an integer that corresponds to a location in the array of strings.  Whenever the getNext() function is called, the String corresponding to the location indicated by this variable will be returned.  If we apply the ++ operator to an instance of this class, we return the current string and advance to the next string stored in the array.  The class should have the following functions and operators associated with it (among others that we learned, Hint: Think Dynamic allocation in classes):

                1.  getNext();   // Returns the next String in the class

                2.  ++ operator // Returns the current string and advances to the next String in the class (post increment). 

                3.  isEmpty()   // Returns true if list is empty

                4.  hasMoreWords() // Returns True if there are more words in the list

                5.  reset() // Reset the pointer

                6.  A Constructor that takes in an array of strings and its length to be stored in the class.

 

 

Part III (10 ponts)

Given the following code, write the output of the program:

#include <string>

using namespace std;

int main() {

                string* str = new string[10];

                str[0] = “I”;

                str[1] = “went”;

                str[2] = “home”;

                str[3] = “happy.”;

                for (int j = 4; j < 10; j++)

                                str[j] = “”;

                string* p = str;

                Tokenize *tk = new Tokenize(str, 10);

                for (int i = 0; i < 4; i++) {

                                if (*(p + i) == tk->getNext())

                                                cout << (*tk)++;

                                p++;

                }

                delete [] str;

                return 0;

}