HW3

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

T/F In C++, pointer is a reserved word.

False, pointer is not reserved.

T/F In C++, pointer variables are declared using the word pointer.

False, pointer variables are declared with *

T/F The address of operator returns the address and value of its operand.

False, the address of operator will return the address, but not the value.

T/F In C++, the dereferencing operator has a higher precedence than the dot operator.

False, the dot operator has a higher precedence than the dereferencing operator.

T/F Dynamic variables are destroyed using the operator new.

False, the new operator is not used to destroy dynamic variables.

T/F The binding of virtual functions occurs at compile time.

False, virtual functions are bound when program is executed.

The statement delete p; deallocates the variable pointer p.

False, will deallocate the dynamic variable that p points to.

T/F If p is a pointer variable, then the statement p = p * 2; is valid in C++.

False, you can't multiply a pointer and int that way.

int* p, q; What misinterpretation could this lead to?

You could think q is a pointer as well, but it is a regular variable.

int *p; p = new int[10]; for(int j = 0; j< 10; j++) { p[1] = 2 * j - 2; } Write statement that deallocates the memory space occupied by the array to which p points.

delete []p;

int *length; int *width; length = new int; *length = 5; width = length; length = new int; *length = 2 * (*width); cout << *length << " " << *width << " " << (*length) * (*width) << endl; Output?

10 5 50

int num; int *listPtr; int *temp; listPtr = new int[5]; num = 8; temp = listPtr; for(int j = 0; j< 5; j++) { *listPtr = num; num = num + 2; listPtr++; } listPtr = temp; for(int k = 0; k < 5; k++) { *temp = *temp + 3; temp++; } for(int k = 0; k < 5; k++) { cout << *listPtr << " "; listPtr++; } cout << endl; Output?

11 13 15 17 19

int *p; int *q = new int; p = q; *q = 75; delete p; p = new int; *p = 62; q = new int; q = p; *q = 26; cout << *p << " " << *q << endl; Output?

26 26

int *tempList; int num = 3; tempList = new int[7]; tempList[6] = 4; for(int j = 5; j >=0; j--) { tempList[j] = tempList[j+1]+j*num } for(int j = 0; j < 7; j++) { cout << tempList [j] << " "; } cout << endl; Output?

49 49 46 40 31 19 4

int x; int y; int *p = &x; int *q = &y; x = 62; y = 38; q = p; *p = 55; x = *q; cout << x << " " << y << endl; cout << *p << " " << *q << endl; What's the output?

55 38 55 55

string *str; string fName, 1Name; str = &fName; *str = "Miller"; str = &1Name; *str = "Tommy"; cout << fName << " " << 1Name << endl; Output?

Miller Tommy

T/F Given the declaration: int *p; the statement: p = new int[50]; dynamically allocates an array of 50 components of type int, and p contains the base address of the array.

True

T/F If p is a pointer variable, then *p refers to the memory location to which p points.

True

T/F In C++, the return type of a function can be a pointer.

True

T/F In a shallow copy , two or more pointers of the same type point to the same memory.

True

T/F The statement delete p; deallocates the dynamic variable that is pointed to by p.

True

T/F Variables that are created during program execution are called dynamic variables.

True

a) Write a statement that declares sales to be a pointer to a pointer of type double. b)Write code to dynamically create a two-dimensional array of 5 rows and 7 columns and sales contains the base address of the array. c)Write code that inputs data from the standard input device into the array sales.

a) double **sales; b) sales = new double*[5]; for(int i = 0; i < 5; i++) { sales[i] = new double[7]; } c) for(int i = 0; i < 5; i++) { for(int j = 0; j < 7; j++) cin >> sales[i][j]; }


Ensembles d'études connexes

biology unit 2: biological molecules

View Set

Basic Overview of GSM Communications

View Set

9.) Introduction to Body Cavities

View Set