In this post, we present you 10+ multiple-choice questions to test your knowledge of C++ pointers and references.
Each question is followed by the correct answer and an explanation to help reinforce your knowledge.
1. What does a pointer store?
Answer:
Explanation:
A pointer is used to store the memory address of a variable.
2. Which operator is used to get the memory address of a variable?
Answer:
Explanation:
The address-of operator (&) is used to fetch the memory address of a variable.
3. What will the following declaration create?
Answer:
Explanation:
Answer: c) Null pointer to an integer
4. What is a reference in C++?
Answer:
Explanation:
A reference is an alias, or another name, for an already existing variable.
5. How do you declare a reference to an integer?
Answer:
Explanation:
A reference is declared using the ‘&’ symbol after the type but before the identifier (variable name).
6. Once a reference is initialized with a variable:
Answer:
Explanation:
Unlike pointers, once a reference is initialized with a variable, it cannot be reassigned to refer to another variable.
7. Which of the following is the correct way to declare a pointer to an integer and assign it the address of an integer variable, num?
Answer:
Explanation:
A pointer is declared using the * symbol and is assigned the address of a variable using the & operator.
8. Which of the following accesses the value at the address stored in a pointer ‘ptr’?
Answer:
Explanation:
The * operator (dereference operator) is used to access the value at the address stored in the pointer.
9. If ‘ref’ is a reference to a variable, which of the following is used to get its memory address?
Answer:
Explanation:
Just like any other variable, the memory address of a reference is obtained using the & operator.
10. What does the following statement do?
Answer:
Explanation:
Answer: b) It makes ref an alias for the value pointed to by ptr.