week 3 and 4 quiz

Ace your homework & exams now with Quizwiz!

true or false

The destructor does not have any arguments.

The asterisk (*) used int he following statement is known as ------. cout << *pCount

indirection operator dereference operator

If you declare a variable double d = 5.5 and compiler stores it in the memory starting with address 04BFA810, then &d is

04BFA810

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

20 11

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; D. cout << &list[0] << endl;

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;

Which of the following is correct to define a new type that is synonymous to an existing type?

A. typedef existingType newType;

11.13 Suppose int list[6] = {11, 12, 13, 14, 15, 16}; Is *list the same as list[0]?

A. yes

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.

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

11.8 Which of the following statements are true?

All correct! 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.

Fix errors in the following code. int area = 1; double* pArea = &area;

Answers B and E B. The error can be fixed by changing double*(asterisk) pArea = &area to int* pArea = &area; E. The error can be fixed by changing int area = 1 to double area = 1.

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

B. 04BFA818

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;

B. If you run the program and enter abc, abc will be displayed.

Which of the following statements are correct to delete a dynamic object from a pointer p?

B. delete p;

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;

B. int* pValue = new int; C. double* pValue = new double;

Suppose you declare an array double list[] = {1, 3.4, 5.5, 3.5}. &list[1] is same as ________.

B. list + 1

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

B. {3, 4, 1, 5, 13, 4}

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

C. 1 2 3

Analyze the following code: class Circle { public: Circle(double radius) { radius = radius; } private: double radius; };

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.

Suppose list is declared as follows: int* list = new int[10]; How should you destroy list?

C. delete [] list;

Suppose you declare the following: double radius = 5; double* const pValue = &radius; Which of the following statements are allowed?

C. pValue = &radius;

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();

Circle* pObject = new Circle();

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

D. The program compiles and runs, but new Circle(5) creates an anonymous object on the heap. This causes memory leak.

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

E. 2 5

11.30 Given the array int list[] = {3, 4, 5, 1, 13, 4}, max_element(list, list + 6, 45) returns _______.

E. the pointer list + 6

Analyze the following code. #include <iostream> using namespace std; int main() { char* p; cout << "Enter a string: "; cin >> p; cout << p << endl; return 0; }

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

A. &count is the address of count

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

address operator

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

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

The program has a compile error because swap(num1, num2) could match either swap(int pValue1, int pValue2) or swap(int& pValue1, int& pValue2).

Suppose you declare the following: double radius = 5; double* const pValue = &radius;

These 3 statements are allowed: A. radius++; B. (*pValue)++; D. *pValue = 0;

true or false

True A local variable is assigned an arbitrary value if you don't initialize it.

true or false

True Dereferencing a pointer that is not initialized could cause fatal runtime error or it could accidently modify important data.

A. int count = 5; int* x = &count;

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.26 Which of the following function header declaration is correct?

both B. int* reverse(int* const list, const int size) D. int* reverse(int const list[], const int size)

Suppose you declare the following: double radius = 5; const double* const pValue = &radius; Which of the following statements are allowed?

cout << *pValue;

Suppose you declare an array double list[] = {1, 3.4, 5.5, 3.5}. *(list + 1) is same as ________.

list[1]

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

swap 1 invoked

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

swap 2 invoked

11.27 Given the array int list[] = {3, 4, 5, 1, 13, 4}, max_element(list, list + 3) returns _______.

the pointer for element 13

11.27 Given the array int list[] = {3, 4, 5, 1, 13, 4}, min_element(list, list + 3) returns _______.

the pointer for element 3

true or false

true A class can have only one destructor

true or false

true A local pointer is assigned an arbitrary value if you don't initialize it.

true or false

true An array element is assigned an arbitrary value if you don't initialize it.

true or false

true By default, the copy constructor performs a shallow copy

true or false

true By default, the copy constructor simply copies each data field in one object to its counterpart in the other object

true or false

true Every class has a copy constructor with the signature ClassName(const ClassName&)

True or false

true Every class has a default constructor if no constructors are defined explicitly.

true or false

true Every class has a default destructor if no destructors are defined explicitly.

true or false

true The copy constructor can be used to create an object initialized with another object's data

Does the following code cause a memory leak? int* pValue = new int; *pValue = 45; pValue = new int; delete pValue;

yes


Related study sets

Chapter 21: Orthopedic Surgery; Short Answer: Lower Leg Surgery

View Set

NU371 HESI Case Study: Major Depressive Disorder

View Set

Policy and the branches of government, Unit test, Quiz 4, Discretionary and rule-making authority, Holding the bureaucracy accountable

View Set

Foods 1 - Obj. 6.01 Meal Planning

View Set

Bio 104S Ch 24 - Origin of Species

View Set

Chapter 8 // From Inquiry to Academic Writing (Greene & Lidinsky, 4th Ed.)

View Set

Geography Alive Chapter 27 Monsoon Asia

View Set

Types & Characteristics of Pooled Investment Unit 14

View Set