Ch.9 Flashcards

1
Q

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which of the following is not true?

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

A

An array can be assigned the value in a pointer variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which of the following statements correctly returns the memory from the dynamic array pointer to by p1 to the freestore?

A

delete [] p1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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.

&

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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 char[size*size];
B) p1 = new string[size];
C) p1 = new ifstream[size];
D) A and B
E) A, B and C

A
p1 = new string[size];,
p1 = new ifstream[size];, and
p1 = new char[size\*size];
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which of the following correctly declares a user-defined data type that defines a pointer to a float?

A) typedef floatPtr* float

B) float* floatPtr ;
C) typedef float* floatPtr;

D) typedef floatPtr *float;

A

typedef float* floatPtr;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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 < delete p1;
delete p2;

A

p1 and p2 both have the same value, so the delete p2 will cause an error and You have a memory leak.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Given that p1 is a pointer variable of the string class, which of the following are legal statements?

A

cout << *p1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Given that p1 is a pointer, p1 ++

A

advances p1 by one unit of the type of variable to which p1 points.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Which of the following assigns to p1 the pointer to the address of value?

A

p1=&value;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which of the following correctly declare 3 integer pointers?

A

int *p1, *p2, *p3;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

dereferencing operator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A

p1 = new string[arraySize];

17
Q

Declare a pointer variable named ptr to an integer

A

int *ptr;

18
Q

The & operator is called the

A

address of operator

19
Q

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

A

p1 = new int;

20
Q

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

A

delete p1;

21
Q

Dynamic variables are created from the

A

freestore or heap.

22
Q

A pointer can be stored in an integer variable

A

FALSE

23
Q

When you return a dynamic array to the freestore, you must include the number of elements in the array.

A

False

24
Q

Even though pointers point to addresses which are integers, you cannot assign an integer to a pointer variable.

A

True

25
Q

You can assign an array to a pointer variable.

A

True

26
Q

Dynamically created variables have no name.

A

True

27
Q

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.

A

False