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;
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
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;
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:
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;
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]
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
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.
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) {
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;
}
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?
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?
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.