Which C++ operator produces a pointer to an object or function, as in int* p = &score;?
07 Pointers and Memory Basics Online Quiz Questions
Use this free practice quiz with 20 questions to review 07 Pointers and Memory Basics, test your knowledge, and prepare for your next test or exam.
True or false: A pointer automatically keeps the object it identifies alive for as long as the pointer exists.
- A
True
- B
False
What C++ literal should you use to initialize an int* when it should point to no object?
In the declaration int* first, second;, which variable is the pointer to int? Answer the variable name: .
Given int values[3] = {4, 8, 12};, what value does *(values + 2) produce?
- A
4
- B
8
- C
12
- D
The address of
values[2]
Select all statements that are true for the declarations const int* p1 = &number; and int* const p2 = &number;.
- A
p1 = &other;is allowed. - B
*p1 = 11;is allowed. - C
p2 = &other;is not allowed. - D
*p2 = 11;is not allowed.
True or false: If end points one position past the end of an array, evaluating *end is permitted because end is a valid pointer value.
- A
True
- B
False
Given int values[] = {10, 20, 30, 40};, what number of elements is represented by &values[4] - &values[0]?
Complete the statement with the parameter form that allows a function to change the caller's pointer itself: In ordinary C++, pass a .
What does int (*operation)(int) = square; declare?
- A
A pointer to an
intobject - B
A function returning an
int*and accepting no arguments - C
A reference to a function returning
int - D
A pointer to a function accepting an
intand returning anint
Select all cases in which dereferencing a pointer is described as unsafe or undefined by the material.
- A
A null pointer
- B
A dangling pointer
- C
A pointer that is non-null and points to a live, correctly aligned object
- D
A pointer outside the permitted object or array range
Given int values[3]; int* p = values;, which statement correctly compares sizeof(values) and sizeof(p)?
- A
sizeof(values)is always equal tosizeof(p). - B
pretains the array length in its type. - C
sizeof(values)measures all threeintelements, whilesizeof(p)measures a pointer. - D
The array is converted to a pointer even inside
sizeof(values).
Consider int* bad_pointer() { int local = 42; return &local; }. Explain why the returned pointer is unsafe, and describe at least one way to redesign the function so that the result remains valid.
Given int values[] = {10, 20, 30, 40}; int* p = values;, what integer does *(p + 2) produce?
Consider the declaration const int* p1 = &number;. Which statement correctly describes what can be changed through p1?
- A
p1cannot point to another object, but the pointed-to integer can be changed throughp1. - B
p1can point to another object, but the pointed-to integer cannot be changed throughp1. - C
Neither
p1nor the pointed-to integer can ever change. - D
p1is an ordinaryintvariable, not a pointer.
Given int values[] = {10, 20, 30, 40}; int* p = values;, what value is produced by *(p + 2)?
- A
10 - B
20 - C
30 - D
Undefined behavior, because adding to an array pointer is never allowed.
In the declaration int sum(const int values[], std::size_t count), how is the values parameter treated when the function is compiled?
- A
The parameter is adjusted to
int* values, so the function does not receive the array length automatically. - B
The parameter remains an array type and carries its length with it.
- C
The parameter is adjusted to
int** values. - D
The function receives a complete copy of every array element.
Inside void redirect(int* p, int* replacement), what is the effect of the statement p = replacement; on a pointer passed by the caller?
- A
Assigning
p = replacementalways changes the caller's pointer. - B
The function cannot read or modify the caller's object through
p. - C
The pointer parameter is passed by reference automatically.
- D
Assigning
p = replacementchanges only the local copy of the pointer.
True or false: A pointer one position past the end of an array may be used as a loop boundary, but it must not be dereferenced.
- A
True
- B
False
Given int values[] = {10, 20, 30, 40}; int* first = &values[0]; int* last = &values[4];, what is the value of last - first?