Deck 9: Pointers and Dynamic Arrays
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/40
Play
Full screen (f)
Deck 9: Pointers and Dynamic Arrays
1
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];
2
Even though pointers point to addresses which are integers,you can not assign an integer to a pointer variable.
True
3
Dynamically created variables have no name.
True
4
A ___________ is the memory address of a variable.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
5
If p1 is an integer pointer variable,with the value of 1000,p1++ changes P1 to point to the memory location 1001.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
6
A pointer can be stored in an integer variable.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
7
Write the code that assigns to p1 an integer pointer variable)the pointer to a dynamically created integer.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
8
In the statement cout << *p1;,the * is called the ________________
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
9
The & operator is called the _______________
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
10
You can assign an array to a pointer variable.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
11
In the following statement,all the variables are pointers.
int* p1,p2;
int* p1,p2;
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
12
The size of dynamic arrays must be declared at compile time.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
13
Dynamic variables are created at __________
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
14
When you return a dynamic array to the freestore,you must include the number of elements in the array.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
15
Write the code to return the dynamic memory pointed to by p1 to the freestore.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
16
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
17
The size of a dynamic array is defined at ___________
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
18
int *p1; declares a static variable.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
19
Declare a pointer variable named ptr to an integer.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
20
Dynamic variables are created from the ___________.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
21
If a program requires a dynamically allocate two-dimensional array,you would allocate the memory by using
A)p1 = new int*[numRows]; forint i=0; i < numRows; i++)
P1[i] = new int[numColumns];
B)p1 = new int*[numRows][numColumns];
C)p1 = new[numRows][numColumns]int;
D)none of the above
A)p1 = new int*[numRows]; forint i=0; i < numRows; i++)
P1[i] = new int[numColumns];
B)p1 = new int*[numRows][numColumns];
C)p1 = new[numRows][numColumns]int;
D)none of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
22
Given that p1 is a pointer,p1++
A)always causes a run time error
B)advances p1 by one unit of the type of variable to which p1 points
C)adds 1 to whatever p1 is pointing to
D)adds one element to the array that p1 is pointing to
A)always causes a run time error
B)advances p1 by one unit of the type of variable to which p1 points
C)adds 1 to whatever p1 is pointing to
D)adds one element to the array that p1 is pointing to
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
23
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.
A)int* p1,p2,p3;
B)int *p1,p2,p3;
C)int *p1,*p2,*p3;
D)all of the above.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
24
Which of the following is not true?
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.
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
25
Which of the following correctly declares a dynamic array of strings?
A)p1 = new string13);
B)p1 = new string[];
C)p1 = new string[13];
D)p1 = new stringArray13);
A)p1 = new string13);
B)p1 = new string[];
C)p1 = new string[13];
D)p1 = new stringArray13);
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
26
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 << p1;
C)cout << int* p1;
D)cout << *p1;
A)cout << &p1;
B)cout << p1;
C)cout << int* p1;
D)cout << *p1;
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
27
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)If the program needs to get the size of the array from the user
C)If the array size is big,but known at compile time
D)You should always use a dynamic array
A)If the array is small,and the size is known before the program runs.
B)If the program needs to get the size of the array from the user
C)If the array size is big,but known at compile time
D)You should always use a dynamic array
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
28
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
A)void f1 IntPtr& ptr);
B)void f1 IntPtr&* ptr);
C)void f1 IntPtr*& ptr);
D)All of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
29
Given that p1 is a pointer variable of the string class,which of the following are legal statements?
A)p1 = new int;
B)cout << *p1;
C)p1 = new char[10];
D)*p1 = new string;
E)B and D
A)p1 = new int;
B)cout << *p1;
C)p1 = new char[10];
D)*p1 = new string;
E)B and D
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
30
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;
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
Int *p1,*p2;
P1 = new int;
P2 = new int;
*p1=11;
*p2=0;
P2=p1;
Cout << *p1 <<" " << *p2 << 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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
31
Which of the following assigns to p1 the pointer to the address of value?
A)*p1=&value;
B)p1=value;
C)p1=&value;
D)&p1 = *value;
A)*p1=&value;
B)p1=value;
C)p1=&value;
D)&p1 = *value;
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
32
What is the output of the following code fragment?
Float *p1;
P1 = new float3);
Cout << *p1;
A)3.0
B)unknown,the address p1 points to is not initialized
C)unknown,the code is illegal,p1 points to a dynamic array
D)0.0
Float *p1;
P1 = new float3);
Cout << *p1;
A)3.0
B)unknown,the address p1 points to is not initialized
C)unknown,the code is illegal,p1 points to a dynamic array
D)0.0
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
33
Given that p1 is an integer pointer variable,and a1 is an integer array,which of the following statements are not legal code?
A)p1= a1;
B)cout << p1[0];
C)cin >> p1[0];
D)a1 = p1;
A)p1= a1;
B)cout << p1[0];
C)cin >> p1[0];
D)a1 = p1;
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
34
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)p1 = new string[size];
B)p1 = new ifstream[size];
C)p1 = new char[size*size];
D)A and B
E)A,B and C
A)p1 = new string[size];
B)p1 = new ifstream[size];
C)p1 = new char[size*size];
D)A and B
E)A,B and C
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
35
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)1002
B)1004
C)1005
D)Unknown
A)1002
B)1004
C)1005
D)Unknown
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
36
Which of the following correctly declares a user-defined data type that defines a pointer to a float?
A)float* floatPtr ;
B)typedef float* floatPtr;
C)typedef floatPtr *float;
D)typedef floatPtr* float
A)float* floatPtr ;
B)typedef float* floatPtr;
C)typedef floatPtr *float;
D)typedef floatPtr* float
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
37
Which of the following statements correctly returns the memory from the dynamic array pointer to by p1 to the freestore?
A)delete [] p1;
B)delete p1[];
C)delete *p1;
D)delete p1;
A)delete [] p1;
B)delete p1[];
C)delete *p1;
D)delete p1;
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
38
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
B)-1
C)-2
D)1
Int v1=2,v2=-1,*p1,*p2;
P1 = & v1;
P2= & v2;
P2=p1;
Cout << *p2 << endl;
A)2
B)-1
C)-2
D)1
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
39
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;
A)11 0
B)0 11
C)11 11
D)0 0
Int *p1,*p2;
P1 = new int;
P2 = new int;
*p1=11;
*p2=0;
P2=p1;
Cout << *p1 <<" " << *p2 << endl;
A)11 0
B)0 11
C)11 11
D)0 0
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
40
If two pointer variables point to the same memory location,what happens when one of the pointers is freed?
A)The other pointer should be considered to be un-initialized
B)The other pointer still points to a valid memory address
C)If you attempt to free the other pointer a run-time error will occur.
D)All of the above
E)A and C
A)The other pointer should be considered to be un-initialized
B)The other pointer still points to a valid memory address
C)If you attempt to free the other pointer a run-time error will occur.
D)All of the above
E)A and C
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck