Practice Quiz: Chapter 9
Write the code that assigns to p1 (an integer pointer variable) the pointer to a dynamically created integer. Answer: ________
p1 = new int;
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
A pointer can be stored in an integer variable.
False
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
The & operator is called the ________.
address of operator
Which of the following is NOT true? Select one: a) A pointer can be assigned the address of an array. b) An array can be assigned the value in a pointer variable. c) If a pointer points to an array, it can be used in place of the array name. d) If a pointer points to an array, the pointer does not know how many elements are in the array.
b) An array can be assigned the value in a pointer variable.
Given that p1 is a pointer, p1++
b) advances p1 by one unit of the type of variable to which p1 points.
Given that p1 is a pointer variable of the string class, which of the following are legal statements?
b) cout << *p1;
Given that p1 is an integer pointer variable, and a1 is an integer array, which of the following statements are NOT legal code?
d) a1 = p1;
Which of the following statements correctly prints out the value that is in the memory address that the pointer p1 is pointing to?
d) cout << *p1;
Write the code to return the dynamic memory pointed to by p1 to the freestore.
delete p1;
If two pointer variables point to the same memory location, what happens when one of the pointers is freed?
e) The other pointer should be considered to be un-initialized and If you attempt to free the other pointer a run-time error will occur
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?
e) p1 = new string[size];, p1 = new ifstream[size];, and p1 = new char[size*size];
Dynamic variables are created from the ________.
freestore or heap
Declare a pointer variable named ptr to an integer. Answer: ________
int *ptr;
If p1 is an integer pointer variable, with the value of 1000, p1++ changes P1 to point to the memory location 1001.
False
In the following statement, all the variables are pointers. int* p1, p2;
False
The size of dynamic arrays must be declared at compile time.
False
When you return a dynamic array to the freestore, you must include the number of elements in the array.
False
int *p1; declares a static variable.
False
Dynamically created variables have no name.
True
Even though pointers point to addresses which are integers, you cannot assign an integer to a pointer variable.
True
You can assign an array to a pointer variable.
True
What is the output of the following code fragment? int v1=2, v2=-1, *p1, *p2; p1 = & v1; p2= & v2; p2=p1; cout << *p2 << endl;
a) 2
What is the output of the following code fragment? float *p1; p1 = new float(3); cout << *p1;
a) 3.0
Which of the following statements correctly returns the memory from the dynamic array pointer to by p1 to the freestore?
a) delete [] p1;
If a program requires a dynamically allocate two-dimensional array, you would allocate the memory by using
a) p1 = new int*[numRows]; for(int i=0; i < numRows; i++) p1[i] = new int[numColumns];
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);
In which case would you consider using a dynamic array?
b) if the program needs to get the size of the array from the user
If p1 is an integer pointer that is pointing to memory location 1001, and an integer takes 4 bytes, then (p1+1) evaluates to
c) 1005
What is the output of the following code? int *p1, *p2; p1 = new int; p2 = new int; *p1 = 11; *p2 = 0; p2 = p1; cout << *p1 << " " << *p2 << endl;
c) 11 11
Which of the following correctly declare 3 integer pointers?
c) int *p1, *p2, *p3;
Which of the following correctly declares a dynamic array of strings?
c) p1 = new string[13];
Which of the following assigns to p1 the pointer to the address of value?
c) p1=&value;
What is wrong with the following code fragment? int *p1, *p2; p1 = new int; p2 = new int; *p1 = 11; *p2 = 0; p2 = p1; cout << *p1 << " " << *p2 << endl; delete p1; delete p2;
d) p1 and p2 both have the same value, so the delete p2 will cause an error and You have a memory leak.