Revel for Liang C++, Fourth 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.

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? Please select all that apply.
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 ___________. Please select all that apply.

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

11.7  Fix errors in the following code. Please select all that apply.

 int area = 1;
 double* pArea = &area;
A. The error can be fixed by changing double* pArea = &area to double* pArea = area;
B. The error can be fixed by changing double* pArea = &area to int* pArea = &area;
C. The error can be fixed by changing double* pArea = &area to float* pArea = area;
D. The error can be fixed by changing double* pArea = &area to int* pArea = area;
E. The error can be fixed by changing int area = 1 to double area = 1;

11.8  Which of the following statements are true? Please select all that apply.
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  Given the following code, which of the following choices is wrong?

 double radius = 5;
 double* const pValue = &radius;
A. radius++;
B. (*pValue)++;
C. pValue = &radius;
D. *pValue = 0;

11.11  Given the following delarations, which statements are allowed? Please select all that apply.

 double radius = 5;
 const double* pValue = &radius;
A. radius++;
B. (*pValue)++;
C. pValue = &radius;
D. *pValue = 0;

11.12  Given the following declaration, Which of the following statements are allowed? Please select all that apply.

 double radius = 5;
 const double* const pValue = &radius;
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  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.

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  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? Which of the following statements are allowed? Please select all that apply.
A. cout << list << endl;
B. cout << &list << endl;
C. cout << list[0] << endl;
D. cout << &list[0] << endl;

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  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.19  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.20  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.21  Given the following declarations, which statements are correct? Please select all that apply.

 int list1[4], list2[4];
 int* p1; int* p2;
A. p1 = list1;
B. p1 = p2;
C. list1 = p1;
D. list1 = list2;

11.22  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.

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  For a one-dimensional array list, which of the following function header declaration is correct? Please select all that apply.
A. 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}, find(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? Please select all that apply.
A. int* pValue = new double;
B. int* pValue = new int;
C. double* pValue = new double;
D. double* pValue = new int;

11.33  Given that list is declared as follows, How should you destroy list?

 int* list = new int[10];
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

Section 11.10 Creating and Accessing Dynamic Objects
11.35  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.36  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.

11.37  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

11.38  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;

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 does not compile because Circle does not have a default constructor.
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.

Section 11.12 Destructors
11.40  Which of the following statements is false?
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.
E. The destructor must always be explicitly defined.

Section 11.14 Copy Constructors
11.41  Which of the following statements is false?
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.
E. The copy constructor must always be explicitly defined.