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?
a) A memory address
b) A data type
c) A value directly
d) None of the above
2. Which operator is used to get the memory address of a variable?
a) *
b) &
c) ->
d) <<
3. What will the following declaration create?
int *ptr = nullptr;
a) Integer variable named ptr
b) Pointer to a char
c) Null pointer to an integer
4. What is a reference in C++?
a) Another name for a pointer
b) An alias for an existing variable
c) A memory location
d) A type of function
5. How do you declare a reference to an integer?
a) int* ref;
b) &int ref;
c) int ref&;
d) int& ref;
6. Once a reference is initialized with a variable:
a) It can be reassigned to another variable
b) It can't refer to any other variables
c) It becomes a pointer
d) None of the above
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?
a) int ptr = num;
b) int ptr = #
c) int ptr = #
d) int &ptr = num;
8. Which of the following accesses the value at the address stored in a pointer 'ptr'?
a) &ptr
b) ptr
c) ptr
d) ptr
9. If 'ref' is a reference to a variable, which of the following is used to get its memory address?
a) ref&
b) &ref
c) ref
d) ref
10. What does the following statement do?
int &ref = *ptr;
a) It assigns the address of ptr to ref.
b) It makes ref an alias for the value pointed to by ptr.
c) It creates a new pointer ref and assigns it the value of ptr.
Comments
Post a Comment