Pointers and Memory Allocation - Quiz Explanation

The correct answers are indicated below, along with the text that explains the correct answers.
 
1. A reference declaration creates:
  A. a new variable
  B. an alias to an existing variable
  C. a pointer
 D. an address
  The correct answer is B.
A reference declaration creates a new name for an existing object or variable.

2. A generic pointer is declared as:
  A. char*
  B. void*
  C. word*
  D. none of these
  The correct answer is B.
The declaration void* is a universal pointer type accepting any other pointer value.

3. The void * p pointer
  A. can be dereferenced
  B. can be added to
  C. can be compared to 0
  D. is virtual
  The correct answer is C.
A void* pointer has no underlying type. It cannot be dereferenced or added to, but it can be compared to the universal constant 0.

4. After the declaration const int N = 5 is made, which of the following is illegal?
  A. cout << N
  B. if (N == i + 3)
  C. cin >> N
  D. none of these
  The correct answer is C.
Variables declared as const are not modifiable.

5. The expression p = new int[20]:
  A. allocates 20 integers and assigns the base address to p
  B. allocates 20 integers off the stack local to a block
  C. reads in 20 integer values from cin
  D. allocates an integer of size 20 bytes
  The correct answer is A.
new allocates space for 20 contiguous integers from free store.

6. After the statement
p = new char[n];
the expression
assert(p);
tests:
  A. that p is not zero and allocation is successful
  B. that p is 1
  C. that p is an char-valued pointer
  D. none of these
  The correct answer is A.
The assert is a standard test that new worked.


7. Which form of the operator delete would you use to deallocate free store memory allocated by this statement:
student_list = new students[size]
  A. delete []student_list
  B. delete student_list
  C. delete []students
  D. delete students[size]
  The correct answer is A.
When new is used to allocate free store for an array, use the form delete []expression, where expression is the expression of the left side of the corresponding new statement.

8. Which one of the following statements about using const is false?
  A. A const variable cannot be used on the left side of an assignment.
  B. When used alone in a declaration, the base type of a const variable is implicitly int.
  C. You cannot declare a constant pointer whose pointed-at value is constant.
  D. A const value must be initialized when it is declared.
  The correct answer is C.
You actually can declare a constant pointer whose pointed-at value is constant. Here's the example used in the lesson text:
 
cont int* const cp = &M_size;
This would declare cp to be a constant pointer whose pointed-at value is constant.
The remainder of the statements about using const are true.