Computer Science 2610

Spring 2000

Quiz 5

 

1. What does test() print?  Do a careful hand simulation.

 

void test()

{

  int *p, *q, *r, *s;

  p = new int;

  *p = 55;

  q = new int;

  *q = 43;

  r = p;

  s = q;

  *p = *q;

  p = q;

  *s = 47;

  cout << *r << " " << *p << " " << *q << endl;

}

 

 

Answer: ________________________________________________________

 

 

2. An important difference between memory allocated with new and memory that is local to a function is

(a)   Memory that is local to a function is automatically deallocated by the system, but memory allocated with new is only allocated when you ask for deallocation.

(b)  Memory that is allocated using new is automatically deallocated by the system, but memory that is local to a function is only allocated when you ask for deallocation.

(c)   Allocating memory using new is more efficient that allocating local variables in functions.

(d)  You can allocate arrays as local variables of functions, but cannot allocate arrays using new.

 

3. When declaring an array, you must give the size of the array.  The size that you give is

      (a) the logical size of the array.

      (b) the physical size of the array.

      (c) never larger than 256.

      (d) never larger than 65536.

 

4. A dangling pointer is

  (a) a pointer to memory that has been deleted.

  (b) a pointer to memory that is no longer in use.

  (c) a pointer to memory that also has other pointers to it.

  (d) a pointer that was created by using new.

 

 

 

5. The following function contains a serious error.  What is the error?  (You don’t even need to know what the function is supposed to do to find this error.)

 

int chars()

{

  char* str;

  cin >> str;

  return strlen(str);

}

 

(a)   Line cin >> str is not allowed, since str has type char*.

(b)  The return line is wrong, since the type is wrong.

(c)   The line #include <string.h> needs to occur inside the function.

(d)  Memory is not allocated for array str.

 

6. If Node is a type, then expression new Node returns a value of type

  (a) Node

  (b) Node*

  (c) Node**

  (d) Node&

 

7. Which of the following will copy the null-terminated string that is in array src into array dest?

  (a) dest = src;

  (b) dest == src;

  (c) strcpy(dest, src);

  (d) strcpy(src, dest);