Absolute C++ 5th Edition Test Bank – Walter Savitch - Kenrick Mock

Absolute C++ 5th Edition Test Bank – Walter Savitch – Kenrick Mock


Title : Absolute C++

Author : Walter Savitch

Edition : 5th Edition

Type : Test Bank

Product Description

Absolute C++ 5th Edition Test Bank – Walter Savitch – Kenrick Mock

Absolute C++ 5th Edition Test Bank – Walter Savitch – Kenrick Mock


Chapter 10 –Pointers and Dynamic Arrays – Test Questions

These test questions are fill in the blank, multiple choice, and true false.  The multiple-choice questions may have more than one correct answer. There is one matching question. Mark all of the correct answers for full credit. True/false questions require an explanation in addition to the true/false response. If the response is false, a correction is required.

True False:

Comment required.

  1. A pointer is a variable that holds the address of some other location in memory.

Answer: True

Explanation: Computer memory is composed of addressable chunks of information called bytes. A pointer value is the memory address stored in a pointer variable.

  1. Pointer variables are just memory addresses and can be assigned to one another without regard to type.

Answer: False

Explanation: A pointer variables, like everything else in C++ is typed, and that typing is enforced strongly. A pointer variable of type double* cannot normally hold a pointer value of type int* for example.

  1. The declaration below declares three pointer variables of type pointer to double that is, a pointer of type (double*)

double* p1, p2, p3;

Answer: False

Explanation: This declares one pointer variable, p1, and two double variables, p2 and p3. The * binds more closely to the variable, not to the type. In spite of this, the usually style puts the asterisk against the type.

To get three pointer variables, one must write

double *p1, *p2, *p3;

or better, declare the three pointer variables on separate lines

double* p1;

double *p2;

double *p3;

  1. A pointer is an address, an address is an integer, but a pointer is not an integer.

Answer: True.

Explanation: Not crazy, rather this is an abstraction. The type system in C++ is sufficiently strong that it requires that you not assign pointer values to other types nor other types to pointers (without a cast, but that tells the compiler you know what you are doing, so it lets you.)

  1. You can get a pointer value to initialize a pointer variable from an object of an appropriate type with the “address-of” operator, &.

Answer: True

Explanation: Every object has a value and an address. The & “address-of” operator extracts the address to make it available to initialize a pointer variable of appropriate type. Example: int x; int *ptrX = &x;

  1. One can use the & operator to extract the value that a pointer points to.

Answer: False

Explanation: The operator is the dereferencing operator, *. This says “follow the pointer to the storage place” or “the variable pointed to by …”

  1. When declaring several pointer variables, there must be one pointer declarator * for each pointer variable.

Answer: True

Explanation: Consider this definition of p1, p2 and p3 as pointer to doubles:

double *p1, *p2, *p3;
There must be one * for each pointer variable defined.


  1. Dangling pointers present no problem in C++

Answer: False

Explanation: A dangling pointer occurs when several pointers point to the same chunk of allocated freestore memory. If one of the pointers has had the delete
pointer applied to it, releasing the freestore memory, the other pointers are said to be dangling. Because the pointer that had delete applied to it most of the time isn’t
changed by the delete operation (not guaranteed by any means), a deleted pointer is frequently called a ‘dangling pointer.’

  1. Dynamic variables or dynamically allocated variables in C++ are created and destroyed according to the program’s needs.

Answer: True

Explanation: Dynamic variables are unaffected by function calls or function return. This is not true of local pointer variables that may point to dynamic variables. Consequently, in a poorly written program, a local pointer can go out of scope and the dynamic variable to which the pointer points may be lost.

  1. There should eventually be a call to the operator delete on a pointer that
    points to the memory allocated by each call to new.

Answer: True

Explanation: Pointer variables are usually local variables. Memory allocated on the free store using the new operator remains allocated whether you use it or even have a pointer pointing to it. If the pointer that points to the allocated memory dies with the end of a function or a block, the memory allocated is locked away so that no one can use it until the program terminates.


  1. The default copy constructor and default operator = provide deep copy.

Answer: False

Explanation: The default copy constructor and operator = just copy the members. This is the definition of shallow copy.


  1. In deep copy, pointers are followed and data and the pointer structure are duplicated.

Answer: True

Explanation: This is the definition of deep copy: duplicating the pointer structure and data.

  1. A function can return an array.

Answer: False

Explanation: A function can return a pointer to a object whose lifetime is longer than the function’s lifetime, which can be a  dynamic array, but arrays may not be returned by a function. Specifically, the object must not be a local variable,

  1. An array name is a constant pointer to array base type.

Answer: True

Explanation: Pointer arithmetic shows how array indexing works: If I declare

int a[10];

a[4]  //is equal to

*(a + 4)


Multiple Choice
  1. The Big Three consists of which three from this list?
    1. Default constructor
    2. Copy constructor
    3. Constructor with two parameters
    4. destructor
    5. Operator=

Answer: b) d) and e)

Explanation: The compiler will provide a do-nothing destructor, a copy members copy constructor and a copy members operator = if you don’t supply them. If you have dynamically allocated memory, you need all three. Here’s why.  If you need a copy constructor it is because you have pointers to data allocated on the freestore without a copy constructor, just the pointers will be copied. The result of this will be evil. This means you have freestore allocated memory, which you must release, the automatic way to do that is with the destructor. These same reasons compel an overloading of operator=, since you will only get the pointers copied if you don’t provide an operator=.


  1. If a class is named MyClass, what must theconstructors be named?
    1. initializer
    2. MyClass
    3. Any name the programmer wishes except the name of the class
    4. ~MyClass
    5. None of the above.

Answer: b) all constructors must be named MyClass

Explanation: That is a language design decision that several languages that support classes made.

  1. If a class is named MyClass, what must the destructor  be named?
    1. Erase
    2. MyClass
    3. Any name the programmer wishes except the name of the class
    4. ~MyClass
    5. None of the above.

Answer: d)

Explanation: That is a language design decision that several languages that support classes made.

  1. A copy constructor has the same name as the class (let’s call it A) and has a parameter that
    1. Is call-by value of an A object
    2. Is call-by-reference of another class
    3. Is call-by-reference of an A class object
    4. Is call-by-name of an object named ~A.
    5. None of these

Answer: c is the closest. It is preferable to have a const call-by-reference parameter
of class A type..

Explanation: The reference is to break the recursion of A(A object) if a call-by-value were used. What happens is you are passing A object by value, this calls the copy constructor, which has an A object called by value which … and so on.  Early C++ compilers would crash, using all of memory  if you left out the &. Modern compilers in my experience catch this error.

  1. Non-static function members of a class gain access to the calling object’s members by
    1. a pointer to the calling object that is implicitly provided by the compiler. The name of this pointer is Members are prefixed implicitly by this->, as in


  1. a variable called self that is an alias for the calling object. This self is used to access the members of the object.
  2. There is no particular mechanism, the variables just know what the calling object is and automatically refer to the calling object.
  3. None of the above.

Answers: a)

Explanation: The “this” pointer is implicitly passed to every constructor, destructor and all other non-static member function. this-> is an automatic prefix that is applied to every member of the class in the definition of function members.

  1. Some pointer arithmetic is allowed. Which of the following arithmetic operators is allowed?
    1. pointer + integer
    2. pointer – pointer
    3. pointer – integer
    4. integer + pointer
    5. integer * pointer

Answer: a) b) c) and d) are correct.

  1. Explanation: In a) and d), a number, equal to the product of the integer and the size of the object pointed to, is added to the pointer. In c), things are similar to a) and d) except that the number is subtracted from the pointer. e) Neither multiplication of pointers or of pointers by integers is allowed. In c), the two pointers must point into the same data structure.
Free Form Questions:
  1. Suppose we have the following definitions and assignments:

double *p, v;

p = &v; // p gets the address of v

What is the effect of this assignment?

*p = 99.99


The effect is to assign 99.99 to v.

  1. Given the definitions,

int *p1, *p2;

p1 = new int;

p2 = new int;


You are to compare and contrast the two assignments.

  1. a) p1 = p2;
  2. b) *p1 = *p2



  1. a) Makes p1 point to the same place in memory to which p2 points. Orphans memory p1 did point to.
  2. b) Makes data where p1 points the same as the data where p2
  3. Given the declarations below, write a code fragment that allocates a nameless variable for pointer p1 to point to.

int *p1, *p2;


p1 = new int;

Explanation: Pointer p1 can be used to refer to a variable, so we can use the operator new to create variables that have no identifiers to serve as name, they have only addresses. We assign the address to the pointer variable p1.

  1. Give three uses for the * operator. Name and describe each use.


Multiplication:   x * y

Pointer definition: int *p;

Pointer dereferencing:  int x; x = *p;

Pointer parameter declaration: void foo( int * p);

  1. Give the output from this code fragment:

int *p1, *p2;

p1 = new int;

p2 = new int;

*p1 = 10;

*p2 = 20;

cout << *p1 << “ “ << *p2 << endl;

*p1 = *p2;

cout << *p1 << “ “ << *p2 << endl;

*p1 = 30;

cout << *p1 << “ “ << *p2 << endl;


10 20

20 20

30 20


  1. Tell about the freestore (also known as heap). What is put there? How is this done? How does a program access the freestore? What happens to memory that has been allocated but is no longer needed?


The freestore is a special area of memory that is reserved for dynamically allocated variables. Any use of the operator new to allocate space will allocate from the freestore. The run-time support package for C++ includes a freestore manager. Operator new causes the freestore manager to allocate memory and return a pointer to the newly allocated memory. The program that has allocated freestore memory should return no longer needed memory to the freestore manager by use of the delete operator on a pointer to the memory.

  1. Write a type definition for a type called NumberPtr that will be the type for pointer variables that hold pointers to dynamic variables of type double. Also, write a declaration for a pointer variable called myPoint, which is of type NumberPtr.


typedef double

NumberPtr myPoint;


To write a type definition, of a name, declare the name to have the type you want the name to carry after the type definition. Then put the keyword typedef in front. So:

double * NumberPtr;//NumberPtr is a pointer to double.

Then put the keyword typedef in front:

typedef double * NumberPtr;     // NumberPtr is the type

// pointer to double

  1. Describe the action of the new What does the new operator return? What are the indications of error?


The new operator takes a type name for its argument, new allocates space on the freestore for a variable of the type of the argument. It returns a pointer to the allocated memory if there is enough memory space. If there is insufficient memory space the new operator may abort the program, or return a null pointer, depending on your particular compiler.

  1. Write a type definition for pointer variables that will point to dynamically allocated arrays whose base type is char. The name of the type will be CharArray.


typedef char* CharArray;

  1. Your program creates a dynamically allocated array as follows:

int *entry;

entry = new int[10];

so that the pointer variable entry is pointing to the dynamically allocated array. Write code to fill this array with 10 numbers typed in at the keyboard.


#include <iostream>

using namespace std;

int main()


int *entry;

entry = new int[10];

cout << “Enter 10 integers:\n”;

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

cin >> entry[i];

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

cout << entry[i] << ” “;

cout << endl;

return 0;


  1. Give the sequence of steps for creating and using a dynamic array.


  1. a) define a pointer type for pointers to variables of the type the elements are to be

typedef double * doublePtr’

  1. b) declare a pointer variable of the defined type to point to the dynamically allocated array.

doublePtr  a;

  1. c) assign to the pointer variable the returned value from a call to new

a = new double[arraySize];

  1. d) use as you would any array.

a[4] = 17.94;

  1. e) before the end of the block where the pointer variable is declared, call the delete operator with the pointer variable as argument. Note the syntax for the deletion of memory allocated as an array.

delete [] a;

  1. Here is the first line of the copy constructor for PFArrayD. The identifier PFArrayD is the name of the class, but in the header it is used three times with different meaning each time. Give the meaning for each use:

PFArrayD::PFArrayD( const PFArrayD& pfaObject)


PFArrayD::, the first one, says the identifier on the right of the scope resolution operator is being defined in the scope of class named by the identifier on the left.

::PFArrayD, the second one, is the name of the constructor

const PFArrayD&, the third one, is the name of the type argument that is expected.

  1. Answer these questions about destructors
    1. What is a destructor and what must the name of a destructor be?
    2. When is a destructor called?
    3. What does a destructor actually do?
    4. What should a destructor do?

Answer: a) A destructor is a member function of a class named ~NameOfClass. b) A destructor is called automatically at the end of the block when a class object goes out of scope. c) A destructor actually does whatever the class author has written into the destructor d) A destructor should release any resources that the constructor has allocated, including releasing dynamically allocated memory.


  1. a) Why is no overloaded assignment operator needed when the data members consist of built-in types.
  2. b) same for copy constructor
  3. c) same for destructor


The answer is the same for a) and b). The compiler provided copy constructor and operator assignment are sufficient: They copy the data. c) The destructor has nothing to do since there are no pointers. The default action (do nothing) is what is needed.

  1. It is an error to call the delete operator on a pointer a second time. Consider what happens if there is memory allocated on the free store for a class, a destructor, and initializing constructors have been defined, but no copy constructor was defined. Now suppose a function is called that has call-by-value parameters. Why does the program either die in the middle or give a segmentation fault?

An swer:

The call-by-value parameters cause the default (compiler generated) copy constructor to be called. The default copy constructor copies the pointer members of the class. This makes the value parameters class object’s pointers point to the argument’s data. When the function terminates, the destructor is called on the parameter. The destructor calls delete on the pointers in parameter. Unfortunately, this data is the same data that the argument’s pointers point to, so the argument’s pointers are now dangling. When the argument goes out of scope, the destructor is called. This calls delete on pointer variables in the argument object. The result it that these pointer have been deleted twice, once at function termination and once when the argument goes out of scope. The result is a segmentation fault, which either causes the program to crash or causes a segmentation fault.  Segmentation fault are usually reported after the program terminates. The moral is that if you have any of destructor, copy constructor or operator = you likely need all three. (This actually occurred to this writer while writing another textbook.)


Absolute C++ 5th Edition Test Bank – Walter Savitch – Kenrick Mock


There are no reviews yet.

Be the first to review “Absolute C++ 5th Edition Test Bank – Walter Savitch – Kenrick Mock”

Your email address will not be published. Required fields are marked *