True or false: For an int*, adding one always advances the pointer by exactly one byte.
7 Pointers and Dynamic Memory Online Quiz Questions
Use this free practice quiz with 20 questions to review 7 Pointers and Dynamic Memory, test your knowledge, and prepare for your next test or exam.
Select all operations that are valid for a null pointer without first making it point to an object.
- A
Compare a null pointer with
nullptr. - B
Dereference a null pointer to read an object.
- C
Copy a null pointer to another pointer.
- D
Assign
nullptrto a pointer.
Which expression correctly releases the storage allocated by int* data = new int[5];?
- A
delete data - B
delete[] data - C
free(data) - D
No release operation is needed.
Select all choices that the material identifies as preferable or useful alternatives to unmanaged raw-pointer ownership in appropriate situations.
- A
std::unique_ptrfor single ownership - B
A raw pointer with no documented owner
- C
std::shared_ptrwhen shared ownership is genuinely required - D
std::vectorfor a dynamic sequence of values
Which statement best explains the distinction between a raw array and a pointer to its first element?
- A
An array always points to dynamically allocated storage.
- B
A pointer always stores the number of elements it addresses.
- C
An array and a pointer are different types with different information.
- D
An array cannot be indexed with the subscript operator.
True or false: Returning &local from a function safely extends the lifetime of the local variable named local.
- A
True
- B
False
True or false: If a pointer refers to one position past the end of an array, dereferencing it is valid because the pointer is still within the array's range.
- A
True
- B
False
Given int value = 10; int* p = &value; *p = 25;, what integer value does evaluating *p produce afterward?
How should the declaration int* const fixed = nullptr; be described?
The expression new int(42) dynamically creates and returns a pointer to it.
Abandoning the original pointer to a dynamically allocated object without releasing the object causes .
Explain why declaring int* p; and then using p is unsafe. Give one appropriate initialization and describe the condition required before dereferencing the pointer.
Given the C++ code int value = 25; int* p = &value;, what does the expression *p evaluate to?
- A
The address of value
- B
The size of value in bytes
- C
The integer value stored in value
- D
A new integer allocated on the heap
Given int numbers[] = {10, 20, 30, 40}; int* p = numbers;, what value does *(p + 2) produce?
- A
10
- B
30
- C
40
- D
The expression is always invalid
Which statement correctly releases the dynamically allocated array created by int* data = new int[5]{};?
- A
delete data - B
free(data) - C
delete[] &data - D
delete[] data
What C++11 keyword should be used to initialize a pointer that intentionally points to no object?
What is wrong with returning &local from a function when local is an automatic local variable?
- A
The pointer is dangling because
localhas been destroyed - B
The pointer remains valid because returning an address extends the local variable's lifetime
- C
The pointer becomes null automatically
- D
The pointer safely refers to a newly allocated copy of
local
Which statement correctly distinguishes a raw array from a pointer to its first element?
- A
They are interchangeable types with identical storage and size information
- B
An array owns its elements and has a fixed size; a pointer stores an address without remembering the element count
- C
A pointer always owns the object it points to, but an array never owns its elements
- D
An array can be reseated to point to another object, but a pointer cannot
Given int numbers[4] = {10, 20, 30, 40}; int* first = &numbers[0]; int* last = &numbers[4];, what is the value of last - first?
Given the C++ code int score = 95; int* address = &score;, which statement best describes address?
- A
address contains the value 95, because it points to score
- B
address contains the address of score, whose exact numeric representation is implementation-dependent
- C
address contains the address of score as a portable integer byte offset
- D
address contains a copy of score that is independent of score