Computing Lab 8

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

If p1 is an integer pointer that is pointing to memory location 1001, and an integer takes 4 bytes, then (p1 + 1) evaluates to A) 1005. B) 1002. C) 1004. D) unknown

A

Which of the following assigns the address of value to the pointer p1? A) p 1 = &value; B) &p1 = *value; C) p 1 = value; D) *p 1 = &value;

A

Given that a typedef for IntPtr defines a pointer to an integer, what would be the correct declaration for a function that expects a reference to an integer pointer? A) void f1 (IntPtr&* ptr); B) void f1 (IntPtr& ptr); C) void f1 (IntPtr*& ptr); D) all of the above

B

Given that p1 is a pointer, p1 ++ A) adds 1 to whatever p1 is pointing to. B) advances p1 by one unit of the type of variable to which p1 points. C) always causes a run time error. D) adds one element to the array that p1 is pointing to.

B

If a program requires a dynamically allocated two-dimensional array, you would allocate the memory by using A) p 1 = new[numRows][numColumns]int; B) p 1 = new int*[numRows]; for(int i = 0; i < numRows; i ++) p1[i] = new int[numColumns]; C) p 1 = new int*[numRows][numColumns]; D) none of the above

B

When a dynamic array with a class for a base type is declared, which constructor is called? A) the destructor B) the default constructor C) the copy constructor D) an explicit constructor

B

Which of the following correctly declare 3 integer pointers? A) int* p1, p2, p3; B) int **p1, ***p2, **p3; C) int *p1, p2, p3; D) all of the above

B

Which of the following correctly declares a dynamic array of strings? A) p 1 = new string[ ]; B) p 1 = new string[13]; C) p 1 = new string(13); D) p 1 = new stringArray(13);

B

Given that p1 is an integer pointer variable, and a1 is an integer array, which of the following statements are not legal code? A) cin > > p1[0]; B) p 1 = a1; C) a1 = p1; D) cout < < p1[0];

C

What is the output of the following code fragment? float *p1; p 1 = new float(3); cout < < *p1; A) 0.0 B) Unknown, the code is illegal, p1 points to a dynamic array. C) 3.0 D) Unknown, the address p1 points to is not initialized.

C

What is the output of the following code? int **p1, **p2; p 1 = new int; p 2 = new int; *p 1 = 11; *p 2 = 0; p 2 = p1; cout < < **p 1 < <" " < < **p 2 < < endl; A) 11 0 B) 0 0 C) 11 11 D) 0 11

C

Which of the following statements are true? A) A dynamic array can have a base type which is a class or a struct. B) A class or a struct may have a member which is a dynamic array. C) A and B D) neither

C

Which of the following statements correctly returns the memory from the dynamic array pointed to by p1 to the freestore? A) delete p1[ ]; B) delete *p1; C) delete [ ] p1; D) delete p1;

C

Given that p1 is a pointer variable of the string class, which of the following are legal statements? A) *p 1 = new string; B) p 1 = new int; C) p 1 = new char[10]; D) cout < < *p1; E) B and D

D

In which case would you consider using a dynamic array? A) if the array is small, and the size is known before the program runs B) You should always use a dynamic array. C) if the array size is big, but known at compile time D) if the program needs to get the size of the array from the user

D

What is the output of the following code fragment? int v1 = 2, v2 = -1, **p1, **p2; p 1 = & v1; p 2 = & v2; p 2 = p1; cout < < *p 2 < < endl; A) -2 B) 1 C) -1 D) 2

D

What is wrong with the following code fragment? int **p1, **p2; p 1 = new int; p 2 = new int; *p 1 = 11; *p 2 = 0; p 2 = p1; cout < < **p 1 < <" " < < **p 2 < < endl; delete p1; delete p2; A) nothing B) p1 and p2 both have the same value, so the delete p2 will cause an error. C) You have a memory leak. D) B and C

D

Which of the following correctly declares a user-defined data type that defines a pointer to a float? A) float* floatPtr ; B) typedef floatPtr* float C) typedef floatPtr *float; D) typedef float* floatPtr;

D

Which of the following is not true? A) A pointer can be assigned the address of an array. B) If a pointer points to an array, the pointer does not know how many elements are in the array. C) If a pointer points to an array, it can be used in place of the array name. D) An array can be assigned the value in a pointer variable.

D

Which of the following statements correctly prints out the value that is in the memory address that the pointer p1 is pointing to? A) cout < < p1; B) cout < < int* p1; C) cout < < &p1; D) cout < < *p1;

D

Assuming that the pointer variable p1 is of the correct type and size is an integer with some value > 1, which of the following declarations are legal? A) p 1 = new ifstream[size]; B) p 1 = new char[size*size]; C) p 1 = new string[size]; D) A and B E) A, B and C

E

If two pointer variables point to the same memory location, what happens when one of the pointers is freed? A) If you attempt to free the other pointer a run-time error will occur. B) The other pointer still points to a valid memory address. C) The other pointer should be considered to be un-initialized. D) all of the above E) A and C

E

(T/F) A pointer can be stored in an integer variable.

False

(T/F) If p1 and p2 are both pointers that point to integers in memory, the condition p1 = = p2 will be true if the values that are in those memory locations are the same.

False

(T/F) If p1 is an integer pointer variable, with the value of 1000, p1++ changes P1 to point to the memory location 1001.

False

(T/F) In the following statement, all the variables are pointers. int* p1, p2;

False

(T/F) The size of dynamic arrays must be declared at compile time.

False

(T/F) When you return a dynamic array to the freestore, you must include the number of elements in the array.

False

(T/F) int *p1; declares a static variable.

False

(T/F) Dynamically created variables have no name.

True

(T/F) Even though pointers point to addresses which are integers, you cannot assign an integer to a pointer variable.

True

(T/F) You can assign an array to a pointer variable.

True

The & operator is called the ________.

address operator

Write the code to return the dynamic memory pointed to by p1 to the freestore.

delete p1;

In the statement cout < < **p1;, the ** is called the ________.

dereference operator

Dynamic variables are created from the ________.

heap

Declare a pointer variable named ptr to an integer.

int* ptr;

Write the code that assigns to p1 (an integer pointer variable) the pointer to a dynamically created integer.

p1 = new int;

Write the code to declare a dynamic array of strings (use the string pointer variable p1) that has as many elements as the variable arraySize.

p1 = new string[arraySize];

A ________ is the memory address of a variable.

pointer

Dynamic variables are created at ________.

run time

The size of a dynamic array is defined at ________.

run time


Ensembles d'études connexes

Superficial back and scapular muscles

View Set