Chapter 14 Quiz: Pointers and Dynamic Memory
Instructions: Select the best answer for each question. Answers are provided at the end.
Question 1. What does the Pascal declaration PReal = ^Real; create?
A) A Real variable named PReal B) A type that is a pointer to a Real value C) A Real constant equal to the value of pi D) An alias for the Real type
Question 2. Where does the data reside after the following code executes?
var p: ^Integer;
begin
New(p);
p^ := 42;
end.
A) The pointer p is on the heap; the value 42 is on the stack
B) Both p and the value 42 are on the heap
C) The pointer p is on the stack; the value 42 is on the heap
D) Both p and the value 42 are on the stack
Question 3. What is the value of a pointer variable that has been declared but not initialized?
A) nil B) 0 C) Undefined (garbage) D) An empty string
Question 4. Which statement correctly dereferences a pointer p of type ^Integer and assigns the value 10 to the location it points to?
A) p := 10;
B) ^p := 10;
C) p^ := 10;
D) @p := 10;
Question 5. What does Dispose(p) do?
A) Sets p to nil
B) Frees the heap memory that p points to
C) Frees the heap memory and sets p to nil
D) Deletes the pointer variable p from the stack
Question 6. After the following code, what is the value of a^?
New(a);
New(b);
a^ := 5;
b^ := 10;
a := b;
A) 5 B) 10 C) 15 D) Undefined — there is a bug
Question 7. In Question 6, which of the following problems has occurred?
A) A nil dereference B) A double free C) A memory leak D) A stack overflow
Question 8. What does the @ operator do in Pascal?
A) Allocates new memory on the heap B) Returns the address of a variable C) Dereferences a pointer D) Compares two pointers for equality
Question 9. Which of the following is a valid reason to use the @ operator?
A) To allocate a new record on the heap B) To pass the address of a local variable to a procedure expecting a pointer C) To free memory that was allocated with New D) To convert an integer to a pointer
Question 10. Consider this type declaration:
type
PNode = ^TNode;
TNode = record
Data: Integer;
Next: PNode;
end;
Why is it legal for PNode to reference TNode before TNode is declared?
A) Pascal allows all forward references in type blocks B) Pascal has a special exception for pointer types referencing not-yet-declared types within the same type block C) The compiler makes two passes over every type block D) It is actually illegal and will cause a compilation error
Question 11. What is the output of this code?
var p, q: ^Integer;
begin
New(p);
p^ := 100;
q := p;
q^ := 200;
WriteLn(p^);
end.
A) 100 B) 200 C) 0 D) Runtime error
Question 12. What is a "dangling pointer"?
A) A pointer that equals nil B) A pointer to memory that has already been freed C) A pointer that was never allocated with New D) A pointer to a local variable in the current scope
Question 13. Which practice best prevents dangling pointer bugs?
A) Never using Dispose
B) Setting pointers to nil after calling Dispose
C) Always using global variables instead of pointers
D) Declaring all pointers as untyped Pointer
Question 14. How do you terminate a linked chain (indicate that a node is the last in the chain)?
A) Set its Value field to 0 B) Set its Next field to nil C) Call Dispose on the node D) Set its Next field to the Head pointer
Question 15. What is the correct order of operations to free a node during linked list traversal?
A) Free the node, then save the Next pointer B) Advance Head to Next, then free the old node C) Save the Next pointer, free the node, then advance D) Both B and C describe correct approaches
Question 16. In the standard head-insertion algorithm for a linked list, a new node is added by:
A) Traversing to the end and linking the last node to the new node B) Setting the new node's Next to Head, then setting Head to the new node C) Setting Head's Next to the new node, then advancing Head D) Swapping the new node with the current Head node
Question 17. What happens if you call Dispose on a pointer obtained via @ (pointing to a stack variable)?
A) The stack variable is safely freed B) Nothing — Pascal detects the error and ignores it C) Undefined behavior — you may corrupt the stack D) A compile-time error is generated
Question 18. Standard Pascal (and default Free Pascal) does NOT allow:
A) Comparing two pointers with =
B) Checking if a pointer equals nil
C) Pointer arithmetic (adding an integer to a pointer)
D) Assigning nil to a pointer
Question 19. The "ownership principle" for dynamic memory states that:
A) Every pointer should own a copy of the data it points to B) Every heap-allocated block should have exactly one pointer responsible for freeing it C) The heap manager owns all allocated memory D) Only global pointers should allocate memory
Question 20. Which of the following is the strongest advantage of a linked list over a static array?
A) Linked lists support random access by index B) Linked lists use less memory per element C) Linked lists can grow and shrink dynamically at runtime D) Linked lists are faster for sequential access
Answer Key
-
B —
PReal = ^Realdeclares a pointer type: a type whose values are addresses of Real values on the heap. -
C — The pointer variable
pis a local variable on the stack.New(p)allocates an Integer on the heap, andpstores the address of that heap location. -
C — Unlike some languages, Pascal does not automatically initialize pointer variables to nil. An uninitialized pointer contains whatever garbage was previously in its memory location.
-
C — The postfix
^dereferences the pointer:p^accesses the value at the locationppoints to. -
B —
Disposefrees the heap memory but does NOT setpto nil. The programmer must do that manually as a defensive measure. -
B — After
a := b, both pointers point to the same heap location, which contains 10. The valuea^is therefore 10. -
C — The original heap block allocated for
a(containing 5) is now unreachable. No pointer refers to it, so it can never be freed. This is a memory leak. -
B — The
@operator returns the memory address of its operand. -
B — The
@operator is commonly used to pass the address of a variable to a procedure or function that expects a pointer parameter. -
B — Pascal has a specific rule allowing pointer types to reference types not yet declared, provided both are in the same
typeblock. This enables self-referential record types needed for linked structures. -
B — Since
q := pmakes both pointers point to the same heap location,q^ := 200modifies the value thatpalso points to. Thereforep^is 200. -
B — A dangling pointer is a pointer that holds the address of memory that has been freed. The address is still stored in the pointer variable, but the memory it refers to is no longer valid.
-
B — Setting a pointer to nil after
Disposeensures that any subsequent attempt to use it can be caught by a nil check, preventing access to freed memory. -
B — The conventional way to mark the end of a linked chain is to set the last node's
Nextpointer to nil. -
D — Both B and C are correct formulations of the save-advance-free pattern. You must preserve access to the next node before freeing the current one.
-
B — Head insertion: point the new node to the current head, then update head to the new node. This is O(1).
-
C — Calling
Disposeon a pointer to stack memory is undefined behavior. The heap manager does not own that memory and may corrupt its internal data structures. -
C — Standard Pascal does not support pointer arithmetic. You cannot add an integer to a pointer to move through memory. This is a deliberate safety feature.
-
B — The ownership principle states that each dynamically allocated block has one designated owner pointer responsible for eventually calling
Dispose. -
C — The primary advantage of linked lists is dynamic sizing: they can grow and shrink without predeclaring a maximum size or wasting memory on unused slots.