C++ Pointers & References MCQ

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

Answer:

a) A memory address

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?

a) *
b) &
c) ->
d) <<

Answer:

b) &

Explanation:

The address-of operator (&) is used to fetch the memory address of a variable.

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

Answer:

d) Pointer to a null integer

Explanation:

Answer: 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

Answer:

b) An alias for an existing variable

Explanation:

A reference is an alias, or another name, for an already existing variable.

5. How do you declare a reference to an integer?

a) int* ref;
b) &int ref;
c) int ref&;
d) int& ref;

Answer:

d) int& ref;

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:

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

Answer:

b) It can’t refer to any other variables

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?

a) int ptr = num;
b) int ptr = #
c) int ptr = #
d) int &ptr = num;

Answer:

b) int* ptr = #

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’?

a) &ptr
b) ptr
c) ptr
d) ptr

Answer:

c) *ptr

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?

a) ref&
b) &ref
c) ref
d) ref

Answer:

b) &ref

Explanation:

Just like any other variable, the memory address of a reference is obtained using the & operator.

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.

Answer:

d) It gives a compile-time error.

Explanation:

Answer: b) It makes ref an alias for the value pointed to by ptr.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top