C++ Test Bank

18
Section 11.2 Pointer Basics 1 Which of the following correctly declares pointer variables. A. int *x; B. int x; C. int &x; D. int **x; The correct answer is A Your answer B is incorrect 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 The correct answer is A Your answer B is incorrect 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 The correct answer is CD Your answer A is incorrect 4 The ampersand (&) used in the following statement is known as ___________. int count = 5;

Transcript of C++ Test Bank

Page 1: C++ Test Bank

Section 11.2 Pointer Basics1  Which of the following correctly declares pointer variables.

A. int *x;

B. int x;

C. int &x;

D. int **x;

The correct answer is A

Your answer B is incorrect 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

The correct answer is A

Your answer B is incorrect 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

The correct answer is CD

Your answer A is incorrect 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

Page 2: C++ Test Bank

D. address operator

The correct answer is D

Your answer is correct 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

The correct answer is E

Your answer A is incorrect 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

The correct answer is AB

Your answer D is incorrect 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;

Page 3: C++ Test Bank

The correct answer is BD

Your answer A is incorrect 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.

The correct answer is ABCD

Your answer A is incorrect Section 11.3 Using const with Pointers9  Suppose you declare the following:

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

Which of the following statements are allowed?

A. radius++;

B. (*pValue)++;

C. pValue = &radius;

D. *pValue = 0;

The correct answer is ABD

Your answer A is incorrect 10  Suppose you declare the following:

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

Which of the following statements are allowed?

A. radius++;

B. (*pValue)++;

Page 4: C++ Test Bank

C. pValue = &radius;

D. *pValue = 0;

The correct answer is C

Your answer D is incorrect 11  Suppose you declare the following:

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

Which of the following statements are allowed?

A. radius++;

B. (*pValue)++;

C. pValue = &radius;

D. *pValue = 0;

E. cout << *pValue;

The correct answer is E

Your answer A is incorrect Section 11.4 Arrays and Pointers12  Suppose int list[6] = {11, 12, 13, 14, 15, 16}; Is *list the same as list[0]?

A. yes

B. no

The correct answer is A

Your answer B is incorrect 13  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;

Page 5: C++ Test Bank

The correct answer is ABD

Your answer A is incorrect 14  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

The correct answer is B

Your answer A is incorrect 15  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]

The correct answer is B

Your answer A is incorrect 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]

Page 6: C++ Test Bank

The correct answer is E

Your answer A is incorrect 17  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

The correct answer is D

Your answer A is incorrect 18  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

Page 7: C++ Test Bank

D. 3 5

E. 2 5

The correct answer is E

Your answer A is incorrect 19  Suppose you defined

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

Which of the following statements are correct?

A. p1 = list1;

B. p1 = p2;

C. list1 = p1;

D. list1 = list2;

The correct answer is AB

Your answer C is incorrect 20  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.

Page 8: C++ Test Bank

D. If you run the program and enter abc, a runtime error will occur, because p is used without being initialized.

The correct answer is D

Your answer A is incorrect 21  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.

The correct answer is B

Your answer D is incorrect Section 11.5 Passing Arguments by References with Pointers22  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)  {

Page 9: C++ Test Bank

    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.

The correct answer is A

Your answer is correct 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;

Page 10: C++ Test Bank

  }

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.

The correct answer is B

Your answer is correct 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(num1, num2) could match either swap(int pValue1, int pValue2) or swap(int &pValue1, int &pValue2).

Page 11: C++ Test Bank

The correct answer is D

Your answer A is incorrect Section 11.6 Returning Pointers from Functions25  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)

The correct answer is BD

Your answer A is incorrect Section 11.7 Dynamic Memory Allocation26  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;

The correct answer is BC

Your answer C is incorrect 27  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;

The correct answer is C

Your answer is correct

Page 12: C++ Test Bank

28  Does the following code cause a memory leak?

int *pValue = new int;*pValue = 45;pValue = new int;delete pValue;

A. yes

B. no

The correct answer is A

Your answer is correct 29  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.

The correct answer is D

Your answer A is incorrect Section 11.8 Creating and Accessing Dynamic Objects 30  Which of the following statements are correct?

A. Circle *pObject = new Circle();

B. Circle pObject = new Circle();

Page 13: C++ Test Bank

C. Circle *pObject = new Circle;

D. Circle pObject = Circle();

The correct answer is A

Your answer B is incorrect 31  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;

The correct answer is B

Your answer A is incorrect 32  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;

Page 14: C++ Test Bank

    return 0;  }

A. 1 1 1

B. 1 1 2

C. 1 2 3

D. 2 2 2

E. 3 3 3

The correct answer is C

Your answer is correct Section 11.9 The this Keyword33  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.

The correct answer is C

Your answer A is incorrect Explanation: You have replace radius = radius by this->radius = radiusSection 11.10 Destructors34  Which of the following statements are true?

Page 15: C++ Test Bank

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.

The correct answer is ABCD

Your answer B is incorrect Section 11.12 Copy Constructors 35  Which of the following statements are true?

A. Every class has a copy constructor with the signature ClassName(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.

The correct answer is ABCD

Your answer A is incorrect