Lab 8 -- Pointers and Linked Lists

Answer Sheet

Exercise 1

(Multiple Choice) Given a non-sorted array with 6 locations containing the following items (note location 5 in the array is empty) and with next_available_location = 5:
         index    array value
           0        10
           1        22
           2        40
           3        80
           4       100
           5       
     What would be the value of next_available_location if 10 were deleted?

         A.    0        B.    1        C.    2        D.    4        E.    5



Exercise 2

(Multiple Choice) Given an array with 5 locations containing the following items and next_available_location = 0:
         index    array value
           0        10
           1        22
           2        40
           3        80
           4       100       
     What would be the result of adding the value 5?

     A.    index    array value
             0         10
             1         22
             2         40
             3         80
             4         5
     B.    index    array value
             0         5
             1         10
             2         22
             3         40
             4         80
     C.    index    array value
             0         5
             1         22
             2         40
             3         80
             4         100         
     D.    index    array value
             0         5
             1         10
             2         22
             3         40
             4         80
             5         100
     E.    can not add 5 because the list is full



Exercise 3

(Multiple Choice)Given a non-sorted array with 6 locations containing the following items (note location 5 in the array is empty):
         index    array value
           0        10
           1        22
           2        40
           3        80
           4       100
           5       
     What would be the result of deleting the value 40 and inserting the value 50?

         A.    index    array value
                 0         10
                 1         22
                 2         50
                 3         80
                 4         100
                 5    
         B.    index    array value
                 0         50
                 1         10
                 2         22
                 3         80
                 4         100
                 5    
         C.    index    array value
                 0         10
                 1         22
                 2         80
                 3         100
                 4         50
                 5    
         D.    index    array value
                 0         10
                 1         22
                 2         80
                 3         100
                 4         100
                 5         50    
         E.    index    array value
                 0         10
                 1         22
                 2         
                 3         80
                 4         100
                 5         50    



Exercise 4.P

(Multiple Choice)Given a sorted array with 6 locations containing the following items (note location 5 in the array is empty):
         index    array value
           0        10
           1        22
           2        40
           3        80
           4       100
           5       
     What would be the result of adding the value 50?


         A. index    array value
             0         10
             1         22
             2         40
             3         80
             4         50
             5         100

         B. index    array value
             0         10
             1         22
             2         40
             3         50
             4         80
             5         100

         C. index    array value
             0         10
             1         22
             2         40
             3         80
             4         100
             5         50

         D. index    array value
             0         50
             1         10
             2         22
             3         40
             4         80
             5         100


Exercise 5.P

(True/False)If the following declarations have been made:
     int xarr[5]; 
     int next_available_location; 
	 

and the current value of next_available_location is 5, it is possible to insert one more item into the list.



Exercise 6

(Multiple Choice)Which of the following is  not one of the fundamental data types?

         A.    int         B.    pointer         C.    char        D.     short        E.     none of the above




Exercise 7

Write a declaration statement that will  statically allocate an integer variable x.




Exercise 8

(Multiple Choice)The compiler allocates memory storage for statically allocated variables during which of the following phases?

         A.    execution         B.    loading         C.    linking         D.    compilation        E.     none of the above




Exercise 9.P

(True/False) Variables that are of the fundamental or the structured data types are statically allocated.


Exercise 10.P

True/False) If the following declaration has been made:

            int grade_array[30][5];

when the program is compiled and loaded, only 1 location is set aside for grade_array with the understanding that if the program needs to use more locations, it can use up to 150. The locations the program uses will be allocated as needed.


Exercise 11

(True/False) Pointer variables can contain any type of data except the file data type.




Exercise 12

(True/False) The following statement correctly declares a structure type ClientRec.
        struct  ClientRec
     {       
          double   balance;
          double   credit;
          ClientRec* nextRec;
     };




Exercise 13

(Multiple Choice) Which of the following declaration statements will statically allocate memory locations?

         A.    int *onevar;
         B.    char twovar;
         C.    struct onestruct
        {
            float xvar;
            int intvar;
        };

                 onestruct *ystruct;
         D.    float *threevar;
         E.    All of the above




Exercise 14.P

(True/False) The following declaration:   int *  p,  q ;       
creates two pointer variables that can be used to reference integer variables.



Exercise 15.P

(Short Answer) Show a C++ statement to declare charPtr to be a pointer to a char.




Exercise 16

(True/False) The following statement renames the type "pointer to unsigned integer" to be "myAgePointer":
   typedef unsigned int*   myAgePointer; 




Exercise 17

(True/False) The following is a correct way of renaming a pointer to a book struct:

     typedef   book*   BookPointer;
     struct    book
     {
         string          title;
         unsigned int    isbn;
         float           cost;
     };




Exercise 18

(Multiple Choice) Which of the following is NOT a valid usage of typedef

        A.   typedef int** ppInt;
        B.   struct time
       {
          int hour,
              minute,
              second;
       };

              typedef time* pTime;
         C.  typedef character* pChar;
         D.  all of the above are valid




Exercise 19

(True/False) Assume we have defined p1 and p2 to be integer pointer variables and we have dynamically allocated memory for each variable. A snapshot of memory is shown below:

Address     Memory Contents     Variable Allocations
  2000         4040                p1
  2004         3020                p2
   ...          ...               ...
  3020         1004
   ...          ...
  4040         8040

The value stored in *p1 is 4040.


Exercise 20

(Multiple Choice) Assume we have defined p1 and p2 to be integer pointer variables. Also assume we have the picture of memory shown below

Address     Memory Contents     Variable Allocations
  2000         2020                p1
  2004         2024                p2
   ...          ...               ...
  2020          100
  2024           55
Given the following code, what would be printed?
     *p1 = 42;
	     p1 = p2;
	     cout << *p1 << " " << *p2;

A.  100 55       B.   42 42      C.  42 55       D.   55 55


Exercise 21

(Multiple Choice) Given that p1 and p2 are integer pointer variables and that the beginning picture of memory is shown below:

Address     Memory Contents     Variable Allocations
  2000          ?                  p1
  2004          ?                  p2
   ...          ...                ...
  2016          ?
   ...          ...                ...
  3020          ?
If we assume that memory locations and then 3020 are the next locations to be allocated, which of the colde segments below would cause memory to be changed as shown below:
Address     Memory Contents     Variable Allocations
  2000          3020                  p1
  2004          2016                  p2
   ...          ...                   ...
  2016          2
   ...          ...                   ...
  3020          2
  A.  p1 = new int;
      p2 = new int;
      *p1 = 2;
      *p2 = *p1;

  B.  *p1 = new int;
      *p2 = new int; 
      p1 = 2;
      p2 = 2;

  C.  p2 = new int;
      p1 = new int;
      *p1 = 2;
      *p2 = 2; 
 
  D.  *p1 = new int;
      *p2 = new int;
      p1 = 2;
      p2 = p1;

Exercise 22.P

(Short Answer) Given the following code, what would be stored in *p1 after the code is executed?
int *p1, *p2;
int x = 42;
p1 = & x;

Exercise 23.P

(Multiple Choice) What is printed by the following code?
int *ptr1, *ptr2;
ptr1 = new int;
ptr2 = new int;
*ptr1 = 100;
ptr2 = ptr1;
*ptr2 = 55;
cout << *ptr1 << " " << *ptr2;
	 A.     100 55
	 B.     55 100
	 C.     55 55
	 D.     100 100
	 

Exercise 24

(True/False) Given:
struct BankRec
{
	int acctNum;
	int balance;
};
BankRec *bankPtr;
bankPtr = new BankRec;
 

The field "balance" in the structure pointed at by "bankPtr" can be referenced using bankPtr->balance.


Exercise 25.P

Given:

struct PersonRec
{
     string name;
     int age;
};
typedef PersonRec* PersonRecType;
PersonRecType personPtr;
The following statement dynamically creates memory referenced by "personPtr":
A. personPtr = new PersonRec();                   B. new personPtr;
C. personPtr = new (PersonRec);                   D. personPtr = new PersonRec;
	

Exercise 26

Given:
struct PersonRec
{
     string name;
     int age;
};
typedef PersonRec* PersonRecType;
PersonRecType personPtr;

Which one of the following statements correctly de-allocates the memory space referenced by personPtr?
A.  personPtr.delete();                          B. personPtr = delete(personPtr);
C.  delete personPtr();                          D. delete personPtr;

Exercise 27

Given:
struct PersonRec
{
     string name;
     int age;
};
typedef PersonRec* PersonRecType;
PersonRecType personPtr;
personPtr = new PersonRec;
which one of the following statements is incorrect in assigning the value "John Smith" to field "name" of the dynamically created memory, referenced by personPtr?
A.  personPtr.name = "John Smith";
B.  string newName = "John Smith";
           personPtr->name = newName;
C.  personPtr->name = "John Smith";
D.  (*personPtr).name = "John Smith";

Exercise 28

{ Script }

Exercise 29

{ Script }

Exercise 30

{ Script }

Exercise 31.P

(Multiple Choice)Suppose we have a linked list of integers. Each node in the list is of Node type and contains an integer and a pointer to the next item in the list. The variable first points to the first node in the list. Which of the code segments below will add a new node containing the value 55 to the beginning of the list?

A.  first = new Node;
    first->value = 55;
    first->next = first;
B.  Node* temp = new Node;
    first->value = 55;
    temp->next = first;
C.  Node* temp = new Node;
    temp->value = 55;
    temp->next = first->next;
    first = temp;
D.  Node* temp = new Node;
    temp->value = 55;
    temp->next = first;
    first = temp;

Exercise 32.P

(Multiple Choice) Suppose we have a linked list of integers. Each node in the list is of Node type and contains an integer and a pointer to the next item in the list. The variable first points to the first node in the list. Which of the code segments below will properly delete the last node in the list?
A.  Node*cur=first;
    while(cur != NULL)
    {
        cur = cur->next;
    }
    delete cur;
    cur = NULL;
B.  Node* cur = first;
    while(cur->next != NULL)
    {
        cur = cur->next;
    }
    delete cur->next;
    cur->next = NULL;
C.  Node * cur = first->next;
    while (cur != NULL)
    {
        cur = cur->next;
    }
    delete cur;
    cur = NULL;
D.  None of the above.

----- End of Lab 8 - Pointers and Linked Lists -----
Complete the Exercises on the Answer Sheet.
Turn in the Answer Sheet and the printouts required by the exercises.