Introduction to Programming with C++, Third Edition, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Many questions in this edition have been updated in the new edition. Please check with the publisher on the newest edition.

Chapter 11 Pointers and Dynamic Memory Management


Section 11.2 Pointer Basics
11.1  Which of the following statements is correct.
A. int count = 5; int* x = &count;
B. int count = 5; int x = &count;
C. int count = 5; int& x = &count;
D. int count = 5; int** x = &count;

11.2  Suppose you declare int count = 5; which of the following is true?
A. &count is the address of count
B. &count is 5
C. *count is the address of count
D. *count is 5

11.3  Suppose you declare int count = 5 and int* pCount = &count; which of the following is true?
A. *count is the address of count
B. &count is 5
C. *pCount is 5
D. pCount contains the address of count

11.4  The ampersand (&) used in the following statement is known as ___________.

  
  int count = 5;
  cout << &count;
A. indirection operator
B. dereference operator
C. multiply operator
D. address operator

11.5  If you declare a variable double d = 5.5 and compiler stores it in the memory starting with address 04BFA810, then &d is _______________.
A. 5
B. 5.5
C. 0
D. unknown
E. 04BFA810

11.6  The asterisk (*) used in the following statement is known as ___________.

  cout << *pCount;
A. indirection operator
B. dereference operator
C. multiply operator
D. address operator

11.7  Why the following pointer variable declaration is wrong?

int area = 1;
double* pArea = &area; 
A. double* pArea = &area should be double* pArea = area;
B. the type of variable does not match the type of the pointer.
C. double* pArea = &area should be float* pArea = area;
D. double* pArea = &area should be int* pArea = area;

11.8  Which of the following statements are true?
A. A local variable is assigned an arbitrary value if you don?t initialize it.
B. A local pointer is assigned an arbitrary value if you don?t initialize it.
C. An array element is assigned an arbitrary value if you don?t initialize it.
D. Dereferencing a pointer that is not initialized could cause fatal runtime error or it could accidentally modify important data.

Section 11.3 Defining Synonymous Types Using the typedef Keyword
11.9  Which of the following is correct to define a new type that is synonymous to an existing type?
A. typedef existingType newType;
B. typedef newType existingType
C. typedef existingType as newType;
D. typedef newType as existingType

Section 11.4 Using const with Pointers
11.10  Suppose you declare the following:

double radius = 5;
doubleconst pValue = &radius;

Which of the following statements are allowed?
A. radius++;
B. (*pValue)++;
C. pValue = &radius;
D. *pValue = 0;

11.11  Suppose you declare the following:

double radius = 5;
const double* pValue = &radius;

Which of the following statements are allowed?
A. radius++;
B. (*pValue)++;
C. pValue = &radius;
D. *pValue = 0;

11.12  Suppose you declare the following:

double radius = 5;
const doubleconst pValue = &radius;

Which of the following statements are allowed?
A. radius++;
B. (*pValue)++;
C. pValue = &radius;
D. *pValue = 0;
E. cout << *pValue;

Section 11.5 Arrays and Pointers
11.13  Suppose int list[6] = {11, 12, 13, 14, 15, 16}; Is *list the same as list[0]?
A. yes
B. no

11.14  If you declare an array double list[] = {1, 3.4, 5.5, 3.5} and compiler stores it in the memory starting with address 04BFA810, which of the following displays 04BFA810?
A. cout << list << endl;
B. cout << &list << endl;
C. cout << list[0] << endl;
D. cout << &list[0] << endl;

11.15  Suppose you declare an array double list[] = {1, 3.4, 5.5, 3.5} and compiler stores it in the memory starting with address 04BFA810. Assume a double value takes eight bytes on a computer. &list[1] is ______.
A. 04BFA810
B. 04BFA818
C. 1
D. 3.4

11.16  Suppose you declare an array double list[] = {1, 3.4, 5.5, 3.5}. &list[1] is same as ________.
A. list
B. list + 1
C. list + 2
D. list[0]
E. list[1]

11.17  Suppose you declare an array double list[] = {1, 3.4, 5.5, 3.5}. *(list + 1) is same as ________.
A. *list
B. *list + 1
C. *list + 2
D. list[0]
E. list[1]

11.18  What is the output of the following code?

  #include <iostream>
  using namespace std;
  
  int main()
  {
    int list[] = {10, 20, 30, 40};
    cout << *(list + 1) << " " << *list + 1 << endl;
  
    return 0;
  }
A. 10 10
B. 20 20
C. 30 30
D. 20 11

11.19  What is the output of the following code?

  #include <iostream>
  using namespace std;
  
  int main()
  {
    int list[] = {1, 1, 1, 1};
    *(list) = *(list) + 1;
    *(list + 1) = *(list + 1) + 2;
    *(list + 2) = *(list + 2) + 3;
    *(list + 3) = *(list + 3) + 4;
    cout << list[0] << " " << list[3] << endl;
  
    return 0;
  }
A. 1 2
B. 2 2
C. 3 4
D. 3 5
E. 2 5

11.20  Suppose you defined

 int list1[4], list2[4];
 int* p1; int* p2;

Which of the following statements are correct?
A. p1 = list1;
B. p1 = p2;
C. list1 = p1;
D. list1 = list2;

11.21  Analyze the following code.

  #include <iostream>
  using namespace std;
  
  int main()
  {
    char* p;
    cout << "Enter a string: ";
    cin >> p;

    cout << p << endl;
  
    return 0;
  }
A. If you run the program and enter abc, nothing will be displayed. The program runs without errors.
B. If you run the program and enter abc, abc will be displayed.
C. If you run the program and enter abc, unpredictable characters will be displayed.
D. If you run the program and enter abc, a runtime error will occur, because p is used without being initialized.

11.22  Analyze the following code.

  #include <iostream>
  using namespace std;
  
  int main()
  {
    char t[10];
    char* p = t;
    cout << "Enter a string: ";
    cin >> p;

    cout << p << endl;
  
    return 0;
  }
A. If you run the program and enter abc, nothing will be displayed. The program runs without errors.
B. If you run the program and enter abc, abc will be displayed.
C. If you run the program and enter abc, unpredictable characters will be displayed.
D. If you run the program and enter abc, a runtime error will occur, because p is being used without initialized.

Section 11.6 Passing Arguments by References with Pointers
11.23  What is the output of the following code?

  #include <iostream>
  using namespace std;
  
  void swap(int* pValue1, int* pValue2)
  {
    cout << "swap 1 invoked" << endl;
  }
  
  void swap(int& pValue1, int& pValue2)
  {
    cout << "swap 2 invoked" << endl;
  }
  
  int main()
  {
    int num1 = 1;
    int num2 = 2;
  
    swap(&num1, &num2);
  
    return 0;
  }
A. swap 1 invoked
B. swap 2 invoked
C. The program has a runtime error because swap is declared multiple times.
D. The program has a compile error because swap is declared multiple times.

11.24  What is the output of the following code?

  #include <iostream>
  using namespace std;
  
  void swap(int* pValue1, int* pValue2)
  {
    cout << "swap 1 invoked" << endl;
  }
  
  void swap(int& pValue1, int& pValue2)
  {
    cout << "swap 2 invoked" << endl;
  }
  
  int main()
  {
    int num1 = 1;
    int num2 = 2;
  
    swap(num1, num2);
  
    return 0;
  }
A. swap 1 invoked
B. swap 2 invoked
C. The program has a runtime error because swap is declared multiple times.
D. The program has a compile error because swap is declared multiple times.

11.25  What is the output of the following code?

  #include <iostream>
  using namespace std;

  void swap(int pValue1, int pValue2)
  {
    cout << "swap 1 invoked" << endl;
  }

  void swap(int& pValue1, int& pValue2)
  {
    cout << "swap 2 invoked" << endl;
  }

  int main()
  {
    int num1 = 1;
    int num2 = 2;

    swap(num1, num2);

    return 0;
  }
A. swap 1 invoked
B. swap 2 invoked
C. The program has a runtime error because swap is declared multiple times.
D. The program has a compile error because swap(num1, num2) could match either swap(int pValue1, int pValue2) or swap(int& pValue1, int& pValue2).

Section 11.7 Returning Pointers from Functions
11.26  Which of the following function header declaration is correct?
A. int int[] reverse(int* const list, const int size)
B. int* reverse(int* const list, const int size)
C. int* reverse(int* const list[], const int size)
D. int* reverse(int const list[], const int size)

Section 11.8 Useful Array Functions
11.27  Given the array int list[] = {3, 4, 5, 1, 13, 4}, min_element(list, list + 3) returns _______.
A. 3
B. 4
C. 5
D. the pointer for element 3
E. the pointer for element 1

11.28  Given the array int list[] = {3, 4, 5, 1, 13, 4}, max_element(list, list + 6) returns _______.
A. 3
B. 4
C. 5
D. the pointer for element 13
E. the pointer for element 5

11.29  Given the array int list[] = {3, 4, 5, 1, 13, 4}, find(list, list + 6, 4) returns _______.
A. 3
B. 4
C. 5
D. the pointer for element 4
E. the pointer for element 5

11.30  Given the array int list[] = {3, 4, 5, 1, 13, 4}, max_element(list, list + 6, 45) returns _______.
A. 3
B. 4
C. 5
D. the pointer for element 13
E. the pointer list + 6

11.31  Given the array int list[] = {3, 4, 5, 1, 13, 4}, after invoking sort(list + 2, list + 4), list is _______.
A. {3, 4, 5, 1, 13, 4}
B. {3, 4, 1, 5, 13, 4}
C. {3, 1, 4, 5, 13, 4}
D. {1, 3, 4, 5, 13, 4}

Section 11.9 Dynamic Memory Allocation
11.32  Which of the following declaration is correct?
A. int* pValue = new double;
B. int* pValue = new int;
C. double* pValue = new double;
D. double* pValue = new int;

11.33  Suppose list is declared as follows:

int* list = new int[10];

How should you destroy list?
A. delete list;
B. delete* list;
C. delete [] list;
D. delete [] *list;

11.34  Does the following code cause a memory leak?

int* pValue = new int;
*pValue = 45;
pValue = new int;
delete pValue;
A. yes
B. no

11.35  Analyze the following code:

 #include <iostream>
 #include "Circle.h"
 using namespace std;

 int main()
 {
   cout << Circle(5).getArea() << endl;
   cout << (new Circle(5))->getArea() << endl;

   return 0;
 }
A. The program has a compile error on Circle(5).getArea().
B. The program has a compile error on new Circle(5).getArea().
C. The program compiles, but cannot run.
D. The program compiles and runs, but new Circle(5) creates an anonymous object on the heap. This causes memory leak.

Section 11.10 Creating and Accessing Dynamic Objects
11.36  Which of the following statements are correct?
A. Circle* pObject = new Circle();
B. Circle pObject = new Circle();
C. Circle* pObject = new Circle;
D. Circle pObject = Circle();

11.37  Which of the following statements are correct to delete a dynamic object from a

pointer p?
A. delete *p;
B. delete p;
C. delete [] *p;
D. delete [] p;

11.38  Show the output of the following code:

  #include <iostream>
  using namespace std;

  class A
  {
  public:
    int x;
    int y;
    int z;

    A(): x(1), y(2), z(3)
    {
    }
  };

  int main()
  {
    A a;
    A* p1 = &a;
    a.x = 2;

    A a1;
    p1 = &a1;
    cout << p1->x << " " << (*p1).y << " " << p1->z;

    return 0;
  }
A. 1 1 1
B. 1 1 2
C. 1 2 3
D. 2 2 2
E. 3 3 3

Section 11.11 The this Keyword
11.39  Analyze the following code:

class Circle 
{
public:
  Circle(double radius)
  {
    radius = radius;
  }

private:
  double radius;
};
A. The program has a compilation error because it does not have a main function.
B. The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0.
C. The program will compile, but you cannot create an object of Circle with a specified radius. The object will have an unpredictable value for radius.
D. The program has a compilation error because you cannot assign radius to radius.
E. The program does not compile because Circle does not have a default constructor.

Section 11.12 Destructors
11.40  Which of the following statements are true?
A. Every class has a default constructor if no constructors are defined explicitly.
B. Every class has a default destructor if no destructors are defined explicitly.
C. A class can have only one destructor.
D. The destructor does not have any arguments.

Section 11.14 Copy Constructors
11.41  Which of the following statements are true?
A. Every class has a copy constructor with the signature ClassName(const ClassName&).
B. The copy constructor can be used to create an object initialized with another object's data.
C. By default, the copy constructor simply copies each data field in one object to its counterpart in the other object.
D. By default, the copy constructor performs a shallow copy.