Name:________________________

 

SSN:_________________________

 

Lab section:_________________________

 

 

Page

Possible

Earned

Page

Possible

Earned

1

 

 

7

 

 

2

 

 

8

 

 

3

 

 

9

 

 

4

 

 

10

 

 

5

 

 

11

 

 

6

 

 

Total

 

 

 

 

 

 

CS12 Final

June 16th, 2001

 

 

1.      (2 points) Write a statement that will declare a variable that will contain the address of a pointer to a double.

 

 

 

 

 

 

 

 

2.      Given the declarations

        int * p = new int;

        int * a = new int[10];

 

a.             (2 points) write a statement that will free the memory pointed to by p.

 

 

 

 

 

 

b.             (3 points) write a statement that will free all the memory that a points to.

 

 

 

 

 

3.      Given the declarations

 

        int a[5] = {2,4,6,8,3};

        char word[] = “cat”;

        int * ap = a[1];

        char * cp = word;

        double * s = new double;

        int * p = NULL;

 

(1 point each)What is the type and value of each of the following variables?

 

                              type                                                    value

 

 

 

a[4]    __________________      _________________

 

 

 

ap      __________________      _________________

 

 

 

*ap     __________________      _________________

 

 

 

cp      __________________      _________________

 

 

 

cp[4]   __________________      _________________

 

 

 

s       __________________      _________________

 

 

 

p       __________________      _________________

 

 

 

*p      __________________      _________________

 

 

 

 

 

 

 

 

 

4.      Assume you have two classes declared in your program, A and B.  Class B has a protected data member, int safe.  List two different ways in which class A can gain access to data member safe in class B.   Write the code segment that grants the access, and explain where it would appear in the code.

 

a.        

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

b.       

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

In questions 5-10, please refer to the class declarations below

 

class Parent                        void Parent:: printName()

{                                   {

  public:                              cout << lastname();

    Parent();                       }

    ~Parent();                     

    void printName();               void Parent:: whoAmI()

    void whoAmI();                  }

    virtual void myType();             cout << “I am a Parent\n”;

  protected:                        }

    int stuff;                     

    char lastname[];                void Parent::myType()

  private:                          {

    int hidden;                        cout << “my type is Parent\n”;

};                                  }

 

 

class Daughter : public Parent      void Daughter::whoAmI()

{                                   {

  public:                              cout << “I am a Daughter\n”;

    Daughter();                     }

    ~Daughter();

    void whoAmI();                  void Daughter::myType()        

    virtual void myType();          {

  private:                             cout << “my type is Daughter\n”;

    char girlstuff[];               }

};

 

 

class Son : public Parent           void Son::whoAmI()

{                                   {

  public:                              cout << “I am a Son\n”;

    Son();                          }

    ~Son();

    void whoAmI();                  void Son::myType()            

    virtual void myType();          {

    char gear[];                       cout << “my type is Son\n”;

};                                  }

 

 

5.      (2 point each) True or False?

 

T / F   Son objects have access to data member stuff.

 

T / F   Son objects have access to data member hidden.

 

T / F   Parent objects have access to data member girlstuff.

 

T / F   Parent objects have access to data member gear.

 

T / F   Daughter objects have access to data member gear.

 

6.      (3 points)  List all the data members that are part of a Daughter object.

 

 

 

 

 

 

 

 

 

 

7.      (4 points)  What happens when an object of type Son is declared? (List in the order in which things happen).

 

 

 

 

 

 

 

 

 

 

 

 

 

8.      (3 points) What happens when an object of type Son invokes the method printName()? (List in the order in which things happen).

 

 

 

 

 

 

 

 

 

 

 

 

 

9.      (2 points) What happens when an object of type Son is declared? (List in the order in which things happen).

 

 

 

 

 

 

 

 

 

 


10.   (2 points each)What is the output of the following code segments?

 

a.  Parent * person_1;

    person_1 = new Parent;

    person_1->whoAmI();

 

 

 

 

 

 

b.  Parent * person_2;

    person_2 = new Daughter;

    person_2->myType();

 

 

 

 

 

 

c.  Parent * person_3;

    person_3 = new Daughter;

    person_3->whoAmI();

 

 

 

 

 

 

d.  Parent * person_4;

    person_4 = new Parent;

    person_4->myType();

 

 

 

 

 

 

e.  Son * person_5;

    person_5 = new Son;

    person_5->whoAmI();

 

 

 

 

 

 

 

 

 

 

 


Assume the following declarations for a singly linked list,

 

struct Node

{

    int data;

    Node * next;

};

 

 

and the list pictured below,

 

 

 

 

 

 

 

 

 

 


11.  What does the following code segment do to the list? (explain or draw).

 

Node * temp = p->next;

p->next = new Node;

p->next->data = p->data;

p->next->next = temp;

 

 

 

 

 

 

 

 

 

 


12.  (4 points) What are the two conditions necessary for recursion to work?

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

13.  Refer to the question on the previous page for Node declarations.  Assume you have a linked list of Nodes, and a pointer, head, pointing to the first node in the list.  Given the function

 

     void Mystery(Node * p)

     {

         if (p != NULL)

         {

             Node * temp = p->next;

             p->next = new Node;

             p->next->data = p->data;

             p->next->next = temp;

             Mystery(temp);

         }

     }

 

 

What does the call Mystery(head) do to the list?

 


14.  (14 points) For each of the statements or code segments below, draw what happens to the list after it is executed.  In each problem, you will be erasing some arrows and adding others.  The problems are independent of each other (new list every time).

 

a.               p = p->next;

 

 

 

 

 

 


b.               p->next = p;

 

 

 

 

 

 


c.                   p = p->next->next;

 

 

 

 

 

 


d.                  p->next = p->next->next;

 

 

 

 

 

 

 


e.                   p->next->next = p;

 

 

 

 

 

 

 


f.                    p->next->next = p->next;

 

 

 

 


15.  A set of integers is a collection of items where order does not matter and no elements repeat.  Below is the header file of a C++ class for storing a set in a linked list of integers. The list has no header node.  The set can be searched to determine if a number is in it. 

 

class Set

{

  public:

    Set();                  // constructor. Sets count to 0.

    bool isEmpty();         // returns true if the list is empty.

    int getCount();         // returns how many numbers are in the set

    bool find(int num);     // returns true if num is in the set.

    void insert(int num);   // inserts num into the set, if it’s not there 

    void print();           // prints all the numbers in the set

  private:

    int * head;             // array for storing the list

    int count;              // count of numbers in the list

};   

 

a.       (8 points) Write the method print() that prints out all the numbers in the set

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

b.      (8 points) Write the method find(int) that searches for a number in the set.  The method returns true if the number is in the set, false otherwise. 

 

 

 

 

 

 

 

 

 

 

 

c.       (5 points) Write the member function insert(int) that inserts a number into the set if it is not already in it.  Try and make use of methods that were already writte, instead of rewriting code segments.