C++ Pointers and References - MCQs

1 min read

In C++, pointers and references are powerful features that provide direct access to memory and allow for highly efficient data manipulation. A pointer is a variable that stores the exact memory address of another variable, enabling dynamic memory allocation, array iteration, and pointer arithmetic. Conversely, a reference acts as a constant, underlying alias for an already existing variable, providing a safer and more intuitive syntax without requiring explicit dereferencing. Understanding the distinction between the address-of operator (&) and the dereference operator (*) is a critical milestone for C++ programmers. Mastery of these concepts is essential for optimizing application performance, managing heap memory, and efficiently passing large objects to functions.

1. What is a pointer in C++?

a) A variable that stores the memory address of another variable
b) A variable that stores the value of another variable
c) A function that allocates memory
d) A macro for dynamic memory
Correct Answer: a) A variable that stores the memory address of another variable
Explanation:
A pointer is a variable whose value is the memory address of another variable.

2. Which operator is used to get the memory address of a variable?

a) *
b) &
c) ->
d) ::
Correct Answer: b) &
Explanation:
The address-of operator (&) is used to obtain the memory address of a variable.

3. Which operator is used to access the value stored at the address held by a pointer?

a) &
b) ->
c) *
d) #
Correct Answer: c) *
Explanation:
The dereference operator (*) is used to access the value at the memory address stored in a pointer.

4. What is the typical size of a standard pointer in a 64-bit system?

a) 2 bytes
b) 4 bytes
c) 8 bytes
d) 16 bytes
Correct Answer: c) 8 bytes
Explanation:
On a 64-bit system, memory addresses are 64 bits (8 bytes) long, so standard pointers are typically 8 bytes in size.

5. What do you call a pointer that has not been initialized?

a) Null pointer
b) Void pointer
c) Wild pointer
d) Dangling pointer
Correct Answer: c) Wild pointer
Explanation:
A wild pointer is a pointer that has not been initialized and points to an arbitrary memory location, which can lead to undefined behavior.

6. What is a pointer called when it points to a memory location that has been freed?

a) Wild pointer
b) Dangling pointer
c) Null pointer
d) Void pointer
Correct Answer: b) Dangling pointer
Explanation:
A dangling pointer points to memory that has already been deallocated or freed, leading to undefined behavior if accessed.

7. Which keyword was introduced in C++11 to represent a null pointer?

a) NULL
b) null
c) nullptr
d) nil
Correct Answer: c) nullptr
Explanation:
The nullptr keyword was introduced in C++11 as a type-safe alternative to the macro NULL for representing null pointers.

8. What is the data type of `nullptr`?

a) int
b) void*
c) std::nullptr_t
d) long
Correct Answer: c) std::nullptr_t
Explanation:
The type of nullptr is std::nullptr_t, which is implicitly convertible to any pointer type or pointer-to-member type.

9. What is the value of a pointer after it is deleted but not set to null?

a) 0
b) nullptr
c) It becomes a dangling pointer
d) It automatically becomes null
Correct Answer: c) It becomes a dangling pointer
Explanation:
After a pointer is deleted, it still holds the old memory address, making it a dangling pointer until it is explicitly set to nullptr.

10. Which of the following is the correct way to declare a pointer to an integer?

a) int ptr;
b) int &ptr;
c) int *ptr;
d) int @ptr;
Correct Answer: c) int *ptr;
Explanation:
The asterisk (*) is used in the declaration to indicate that the variable is a pointer to the specified type.

11. What does `const int* ptr` mean?

a) ptr is a constant pointer to an integer
b) ptr is a pointer to a constant integer
c) ptr is a constant pointer to a constant integer
d) ptr is an integer constant
Correct Answer: b) ptr is a pointer to a constant integer
Explanation:
In `const int* ptr`, the const applies to the int, meaning the integer value being pointed to cannot be changed through this pointer, but the pointer itself can be reassigned.

12. What does `int* const ptr` mean?

a) ptr is a constant pointer to an integer
b) ptr is a pointer to a constant integer
c) ptr is a constant pointer to a constant integer
d) ptr is an integer constant
Correct Answer: a) ptr is a constant pointer to an integer
Explanation:
In `int* const ptr`, the const applies to the pointer itself, meaning the address stored in ptr cannot be changed, but the integer value it points to can be modified.

13. What does `const int* const ptr` mean?

a) ptr is a constant pointer to an integer
b) ptr is a pointer to a constant integer
c) ptr is a constant pointer to a constant integer
d) ptr is an integer constant
Correct Answer: c) ptr is a constant pointer to a constant integer
Explanation:
Both the pointer and the value it points to are constant. Neither the address stored in ptr nor the integer value can be changed.

14. Can you change the value pointed to by a `const int*`?

a) Yes
b) No
c) Only if cast to int*
d) Only using dynamic memory
Correct Answer: b) No
Explanation:
The const qualifier on the int means the value being pointed to is read-only through this pointer.

15. Can you change the address stored in a `const int*`?

a) Yes
b) No
c) Only if cast to int*
d) Only using dynamic memory
Correct Answer: a) Yes
Explanation:
The pointer itself is not constant, so it can be reassigned to point to a different memory address.

16. Can you change the value pointed to by an `int* const`?

a) Yes
b) No
c) Only if cast to const int*
d) Only using dynamic memory
Correct Answer: a) Yes
Explanation:
The integer value being pointed to is not constant, so it can be modified through the pointer.

17. Can you change the address stored in an `int* const`?

a) Yes
b) No
c) Only if cast to int*
d) Only using dynamic memory
Correct Answer: b) No
Explanation:
The const qualifier applies to the pointer itself, meaning the memory address it holds cannot be changed.

18. What is a 'wild pointer'?

a) A pointer that has not been initialized
b) A pointer that points to freed memory
c) A pointer that points to null
d) A pointer that cannot be changed
Correct Answer: a) A pointer that has not been initialized
Explanation:
A wild pointer is an uninitialized pointer that points to an arbitrary memory location, which can cause unpredictable behavior if dereferenced.

19. What is a 'dangling pointer'?

a) A pointer that has not been initialized
b) A pointer that points to freed or deallocated memory
c) A pointer that points to null
d) A pointer that cannot be changed
Correct Answer: b) A pointer that points to freed or deallocated memory
Explanation:
A dangling pointer occurs when a pointer continues to hold the address of memory that has been deallocated, leading to undefined behavior if accessed.

20. What happens if you dereference a null pointer?

a) It returns 0
b) It returns nullptr
c) Undefined behavior, typically a crash
d) It allocates new memory
Correct Answer: c) Undefined behavior, typically a crash
Explanation:
Dereferencing a null pointer results in undefined behavior, which usually causes a segmentation fault or access violation, crashing the program.

21. If `int* p` points to address 1000, what is the value of `p + 1` on a system where `int` is 4 bytes?

a) 1001
b) 1004
c) 1008
d) 1000
Correct Answer: b) 1004
Explanation:
Pointer arithmetic scales by the size of the pointed-to type. Adding 1 to an int pointer adds the size of an int (4 bytes), resulting in 1004.

22. What is the result of subtracting two pointers that point to elements of the same array?

a) The difference in their memory addresses in bytes
b) The number of elements between them
c) A compilation error
d) Undefined behavior
Correct Answer: b) The number of elements between them
Explanation:
When subtracting two pointers to the same array, the result is the number of elements between them, not the byte difference.

23. Can you add two pointers together in C++?

a) Yes
b) No
c) Only if they are void pointers
d) Only if they point to the same array
Correct Answer: b) No
Explanation:
Adding two pointers is not a valid operation in C++ because the result would not represent a meaningful memory address.

24. If `char* c` points to address 2000, what is the value of `c + 3`?

a) 2001
b) 2003
c) 2006
d) 2000
Correct Answer: b) 2003
Explanation:
Since a char is 1 byte, adding 3 to a char pointer advances the address by 3 bytes, resulting in 2003.

25. Which of the following pointer arithmetic operations is invalid?

a) ptr + 1
b) ptr - 1
c) ptr1 - ptr2
d) ptr1 + ptr2
Correct Answer: d) ptr1 + ptr2
Explanation:
You can add an integer to a pointer or subtract two pointers, but adding two pointers together is not allowed in C++.

26. What does the `->` operator do?

a) Dereferences a pointer and accesses a member
b) Gets the address of a member
c) Assigns a value to a pointer
d) Compares two pointers
Correct Answer: a) Dereferences a pointer and accesses a member
Explanation:
The arrow operator (->) is used to access members of a class or struct through a pointer to that object.

27. What is the equivalent of `ptr->member`?

a) *ptr.member
b) (*ptr).member
c) &(ptr.member)
d) ptr.*member
Correct Answer: b) (*ptr).member
Explanation:
The expression `ptr->member` is syntactic sugar for `(*ptr).member`, which first dereferences the pointer and then accesses the member.

28. Can you compare two pointers using relational operators if they point to different arrays?

a) Yes, always
b) No, it results in undefined behavior
c) Yes, if they are void pointers
d) Yes, if they are const pointers
Correct Answer: b) No, it results in undefined behavior
Explanation:
Relational comparisons between pointers are only valid if they point to elements of the same array or one past the end; otherwise, it is undefined behavior.

29. What is the result of `p - p` where `p` is a valid pointer?

a) 0
b) 1
c) The size of the type
d) Undefined behavior
Correct Answer: a) 0
Explanation:
Subtracting a pointer from itself yields 0, representing zero elements between the same address.

30. If `double* d` points to address 4000, what is `d - 1` (assuming double is 8 bytes)?

a) 3999
b) 3992
c) 3996
d) 4000
Correct Answer: b) 3992
Explanation:
Subtracting 1 from a double pointer subtracts the size of a double (8 bytes), resulting in 3992.

31. What happens to an array name when it is used in an expression?

a) It remains an array
b) It decays to a pointer to its first element
c) It becomes a reference
d) It causes a compilation error
Correct Answer: b) It decays to a pointer to its first element
Explanation:
In most expressions, an array name implicitly decays (converts) to a pointer to its first element.

32. Are arrays and pointers exactly the same in C++?

a) Yes
b) No, but they are closely related
c) Yes, if declared with const
d) No, they are completely unrelated
Correct Answer: b) No, but they are closely related
Explanation:
Arrays and pointers are not the same; arrays are fixed-size contiguous blocks of memory, while pointers are variables that store addresses, though arrays often decay to pointers.

33. What is the output of `sizeof(arr)` if `arr` is an array of 10 integers (assuming int is 4 bytes)?

a) 4
b) 10
c) 40
d) 8
Correct Answer: c) 40
Explanation:
The sizeof operator on an array returns the total size in bytes, which is 10 elements * 4 bytes = 40 bytes.

34. What is the output of `sizeof(ptr)` if `ptr` is a pointer to an integer on a 64-bit system?

a) 4
b) 8
c) 10
d) 40
Correct Answer: b) 8
Explanation:
On a 64-bit system, pointers are 8 bytes in size, regardless of the type they point to.

35. How do you declare a pointer to an array of 5 integers?

a) int *ptr[5];
b) int (*ptr)[5];
c) int ptr[5];
d) int &ptr[5];
Correct Answer: b) int (*ptr)[5];
Explanation:
The parentheses are necessary to declare a pointer to an array. Without them, it would declare an array of pointers.

36. How do you declare an array of 5 pointers to integers?

a) int (*ptr)[5];
b) int *ptr[5];
c) int ptr[5];
d) int &ptr[5];
Correct Answer: b) int *ptr[5];
Explanation:
This declares an array of 5 elements, where each element is a pointer to an integer.

37. Can you use pointer arithmetic on an array name?

a) Yes
b) No
c) Only if it is a const array
d) Only if it is dynamically allocated
Correct Answer: a) Yes
Explanation:
Since the array name decays to a pointer to its first element in expressions, you can perform pointer arithmetic on it.

38. What is the difference between `arr[i]` and `*(arr + i)`?

a) They are completely different
b) They are equivalent
c) arr[i] is faster
d) *(arr + i) is faster
Correct Answer: b) They are equivalent
Explanation:
The subscript operator [] is defined such that `arr[i]` is exactly equivalent to `*(arr + i)`.

39. Can you assign a new address to an array name?

a) Yes
b) No
c) Only if it is a pointer
d) Only if it is dynamically allocated
Correct Answer: b) No
Explanation:
An array name is a constant pointer to its first element; it cannot be reassigned to point to a different memory location.

40. How do you pass an array to a function so it doesn't decay to a pointer?

a) Pass by value
b) Pass by reference
c) Pass by pointer
d) It always decays
Correct Answer: b) Pass by reference
Explanation:
Passing an array by reference (e.g., `void func(int (&arr)[10])`) prevents it from decaying to a pointer and preserves its size information.

41. What is a `void*` pointer?

a) A pointer that points to nothing
b) A generic pointer that can point to any data type
c) A pointer to a void function
d) A null pointer
Correct Answer: b) A generic pointer that can point to any data type
Explanation:
A void pointer is a generic pointer type that can hold the address of any data type, but it cannot be dereferenced directly.

42. Can you dereference a `void*` pointer directly?

a) Yes
b) No
c) Only if it points to an int
d) Only if it is null
Correct Answer: b) No
Explanation:
A void pointer has no associated type, so the compiler does not know how many bytes to read or how to interpret the data; it must be cast first.

43. How do you dereference a `void*` pointer that points to an integer?

a) *ptr
b) (int*)*ptr
c) *(int*)ptr
d) void(int)ptr
Correct Answer: c) *(int*)ptr
Explanation:
You must first cast the void pointer to an int pointer, and then dereference it to access the integer value.

44. Can a `void*` pointer perform pointer arithmetic?

a) Yes
b) No, not in standard C++
c) Only if cast to int*
d) Only if it points to an array
Correct Answer: b) No, not in standard C++
Explanation:
Standard C++ does not allow pointer arithmetic on void pointers because the size of the pointed-to type is unknown. (GCC allows it as an extension, treating void as size 1).

45. What is the primary use of a `void*` pointer?

a) To store null values
b) To write generic functions and data structures that can handle any data type
c) To point to void functions
d) To allocate memory
Correct Answer: b) To write generic functions and data structures that can handle any data type
Explanation:
Void pointers are commonly used in C-style generic programming, such as in the parameters of qsort or malloc, to handle data of any type.

46. Which cast is recommended in C++ to convert a `void*` to an `int*`?

a) (int*)ptr
b) static_cast<int*>(ptr)
c) dynamic_cast<int*>(ptr)
d) const_cast<int*>(ptr)
Correct Answer: b) static_cast<int*>(ptr)
Explanation:
static_cast is the appropriate C++ cast for converting a void pointer to a typed pointer, as it performs compile-time type checking.

47. Can a `void*` point to a member function?

a) Yes
b) No
c) Only if it is static
d) Only if it is virtual
Correct Answer: b) No
Explanation:
A void pointer can point to data objects, but it cannot point to member functions because member functions have a different representation and may require a 'this' pointer.

48. What is the size of a `void*` compared to an `int*`?

a) void* is smaller
b) void* is larger
c) They are the same size
d) It depends on the data type
Correct Answer: c) They are the same size
Explanation:
All data pointers on a given architecture typically have the same size, regardless of the type they point to.

49. Is an implicit cast from `void*` to `int*` allowed in C++?

a) Yes
b) No
c) Only for const void*
d) Only in C++11
Correct Answer: b) No
Explanation:
C++ requires an explicit cast to convert a void pointer to a typed pointer to ensure type safety.

50. Is an implicit cast from `void*` to `int*` allowed in C?

a) Yes
b) No
c) Only for const void*
d) Only in C99
Correct Answer: a) Yes
Explanation:
In C, void pointers can be implicitly assigned to and from any other object pointer type without an explicit cast.

51. How do you declare a pointer to a function that takes an int and returns a double?

a) double *func(int);
b) double (*func)(int);
c) double &(func)(int);
d) double func*(int);
Correct Answer: b) double (*func)(int);
Explanation:
The parentheses around *func are necessary to indicate that func is a pointer to a function, rather than a function returning a pointer.

52. How do you call a function using a function pointer `funcPtr`?

a) *funcPtr(args)
b) funcPtr->(args)
c) funcPtr(args)
d) &funcPtr(args)
Correct Answer: c) funcPtr(args)
Explanation:
Function pointers can be called using the same syntax as regular function names, without needing to explicitly dereference them.

53. What is the primary use of function pointers?

a) To allocate memory
b) To implement callbacks and tables of functions
c) To store data
d) To handle exceptions
Correct Answer: b) To implement callbacks and tables of functions
Explanation:
Function pointers allow functions to be passed as arguments, stored in arrays, and called dynamically, enabling callbacks and jump tables.

54. Can an array of function pointers be created?

a) Yes
b) No
c) Only if they return void
d) Only if they take no arguments
Correct Answer: a) Yes
Explanation:
You can create an array of function pointers, provided all functions in the array have the exact same signature.

55. What is the type of the function pointer `void (*fp)(int)`?

a) Pointer to a function taking int and returning void
b) Pointer to a void taking int
c) Function returning a void pointer
d) Pointer to an int returning void
Correct Answer: a) Pointer to a function taking int and returning void
Explanation:
The declaration specifies that fp is a pointer to a function that accepts an int parameter and returns void.

56. Can a function pointer be null?

a) Yes
b) No
c) Only if it points to a void function
d) Only if it is const
Correct Answer: a) Yes
Explanation:
Function pointers can be initialized to or assigned nullptr, and should be checked before calling to avoid undefined behavior.

57. How do you pass a function as an argument to another function?

a) By passing its name or a function pointer
b) By passing its return value
c) By passing its address using &&
d) You cannot pass functions
Correct Answer: a) By passing its name or a function pointer
Explanation:
You can pass a function by simply using its name (which decays to a pointer) or by explicitly passing a function pointer.

58. What is a callback function?

a) A function that calls itself
b) A function passed as an argument to another function and called later
c) A function that returns a pointer
d) A function that handles exceptions
Correct Answer: b) A function passed as an argument to another function and called later
Explanation:
A callback is a function passed as an argument to another function, which is then invoked inside the outer function to complete a routine or action.

59. Does the `&` operator need to be used to get the address of a function?

a) Yes, always
b) No, the function name decays to a pointer
c) Only for member functions
d) Only for virtual functions
Correct Answer: b) No, the function name decays to a pointer
Explanation:
Just like arrays, function names implicitly decay to function pointers in expressions, so the & operator is optional.

60. Can function pointers be overloaded?

a) Yes, directly
b) No, but you can overload the function itself
c) Only if they return void
d) Only in C++11
Correct Answer: b) No, but you can overload the function itself
Explanation:
You cannot overload the pointer itself, but if the functions being pointed to are overloaded, you must cast to the specific function signature to resolve the ambiguity.

61. What is a reference in C++?

a) A pointer to a variable
b) An alias for an existing variable
c) A copy of a variable
d) A constant variable
Correct Answer: b) An alias for an existing variable
Explanation:
A reference is an alternative name (alias) for an existing variable; once initialized, it acts exactly like the original variable.

62. Which operator is used to declare a reference?

a) *
b) &
c) ->
d) ::
Correct Answer: b) &
Explanation:
The ampersand (&) is used in the declaration to indicate that the variable is a reference to the specified type.

63. Must a reference be initialized when it is declared?

a) Yes
b) No
c) Only if it is a const reference
d) Only if it is an rvalue reference
Correct Answer: a) Yes
Explanation:
References must be initialized upon declaration because they are aliases and cannot exist without referring to an actual object.

64. Can a reference be reassigned to refer to a different variable?

a) Yes
b) No
c) Only if it is a const reference
d) Only if it is an rvalue reference
Correct Answer: b) No
Explanation:
Once a reference is bound to an object, it cannot be rebound to refer to a different object; any assignment modifies the original object.

65. Can a reference be null?

a) Yes
b) No
c) Only if it is a const reference
d) Only if it is an rvalue reference
Correct Answer: b) No
Explanation:
References must always refer to a valid object; there is no such thing as a null reference in standard C++.

66. What is the size of a reference compared to a pointer?

a) References are always smaller
b) References are always larger
c) They are typically the same size in implementation
d) References have no size
Correct Answer: c) They are typically the same size in implementation
Explanation:
Although references are conceptually aliases, compilers typically implement them using pointers under the hood, making their size equal to a pointer.

67. What is a const reference?

a) A reference that cannot be reassigned
b) A reference to a constant variable
c) A reference that cannot be null
d) A reference to a pointer
Correct Answer: b) A reference to a constant variable
Explanation:
A const reference (e.g., `const int&`) is a reference through which the referred object cannot be modified.

68. Can a const reference be used to modify the variable it refers to?

a) Yes
b) No
c) Only if cast to non-const
d) Only if it is an rvalue reference
Correct Answer: b) No
Explanation:
The const qualifier prevents modification of the referred object through that specific reference.

69. Can a non-const lvalue reference bind to a temporary object?

a) Yes
b) No
c) Only if it is const
d) Only if it is an rvalue reference
Correct Answer: b) No
Explanation:
Standard C++ does not allow non-const lvalue references to bind to temporary objects (rvalues) to prevent accidental modification of soon-to-be-destroyed data.

70. Can a const lvalue reference bind to a temporary object?

a) Yes, and it extends the temporary's lifetime
b) No
c) Yes, but the temporary is destroyed immediately
d) Only if it is an rvalue reference
Correct Answer: a) Yes, and it extends the temporary's lifetime
Explanation:
A const lvalue reference can bind to a temporary, and the C++ standard guarantees that the temporary's lifetime is extended to match the lifetime of the reference.

71. Which of the following is a difference between pointers and references?

a) Pointers can be null, references cannot
b) References can be null, pointers cannot
c) Pointers must be initialized, references do not
d) References can be reassigned, pointers cannot
Correct Answer: a) Pointers can be null, references cannot
Explanation:
Pointers can hold a null value, but references must always be bound to a valid object and cannot be null.

72. When should you prefer passing by reference over passing by pointer?

a) When the argument can be null
b) When the argument should not be null and you want cleaner syntax
c) When you need to change the pointer itself
d) When passing arrays
Correct Answer: b) When the argument should not be null and you want cleaner syntax
Explanation:
References provide cleaner syntax (no dereferencing needed) and guarantee the argument is not null, making them ideal when nullability is not required.

73. What is an rvalue reference?

a) A reference to a constant variable
b) A reference that can bind to temporary objects (rvalues)
c) A reference to a pointer
d) A reference that cannot be null
Correct Answer: b) A reference that can bind to temporary objects (rvalues)
Explanation:
Rvalue references (declared with &&) are designed to bind to temporary objects, enabling move semantics and perfect forwarding.

74. Which operator is used to declare an rvalue reference?

a) &
b) &&
c) *
d) ->
Correct Answer: b) &&
Explanation:
The double ampersand (&&) is used to declare an rvalue reference in C++11 and later.

75. What can an rvalue reference bind to?

a) Only lvalues
b) Only rvalues (temporaries)
c) Both lvalues and rvalues
d) Only null pointers
Correct Answer: b) Only rvalues (temporaries)
Explanation:
By default, an rvalue reference can only bind to rvalues (temporary objects or values explicitly cast with std::move), not to named lvalues.

76. What is the main purpose of rvalue references?

a) To create aliases for variables
b) To implement move semantics and perfect forwarding
c) To prevent null references
d) To replace pointers
Correct Answer: b) To implement move semantics and perfect forwarding
Explanation:
Rvalue references were introduced primarily to support move semantics (efficiently transferring resources from temporaries) and perfect forwarding in templates.

77. What is reference collapsing?

a) The process of destroying a reference
b) Rules that determine the resulting reference type when combining references in templates
c) The process of converting a reference to a pointer
d) The process of making a reference null
Correct Answer: b) Rules that determine the resulting reference type when combining references in templates
Explanation:
Reference collapsing is a set of rules in C++ that dictate how nested references (e.g., in template type deduction) are simplified to a single reference type.

78. What does `T&&` collapse to if `T` is `int&`?

a) int&&
b) int&
c) int
d) const int&
Correct Answer: b) int&
Explanation:
According to reference collapsing rules, if either reference is an lvalue reference (&), the result is an lvalue reference (&).

79. What does `T&&` collapse to if `T` is `int&&`?

a) int&&
b) int&
c) int
d) const int&
Correct Answer: a) int&&
Explanation:
If both references are rvalue references (&&), the collapsed result remains an rvalue reference (&&).

80. What does `T&&` collapse to if `T` is `const int&`?

a) const int&&
b) const int&
c) int&
d) int&&
Correct Answer: b) const int&
Explanation:
Since T is an lvalue reference, the collapsed result is an lvalue reference, preserving the const qualifier.

81. What header file contains smart pointers in C++11?

a) <pointer>
b) <memory>
c) <smart>
d) <utility>
Correct Answer: b) <memory>
Explanation:
The header contains the definitions for standard smart pointers like std::unique_ptr, std::shared_ptr, and std::weak_ptr.

82. What is the main advantage of using smart pointers?

a) They are faster than raw pointers
b) They automatically manage memory and prevent leaks
c) They can point to any data type
d) They do not require initialization
Correct Answer: b) They automatically manage memory and prevent leaks
Explanation:
Smart pointers use RAII to automatically deallocate memory when they go out of scope, preventing memory leaks.

83. Which smart pointer represents exclusive ownership?

a) std::shared_ptr
b) std::weak_ptr
c) std::unique_ptr
d) std::auto_ptr
Correct Answer: c) std::unique_ptr
Explanation:
std::unique_ptr enforces exclusive ownership, meaning only one unique_ptr can own the resource at any given time.

84. Which smart pointer represents shared ownership?

a) std::unique_ptr
b) std::shared_ptr
c) std::weak_ptr
d) std::auto_ptr
Correct Answer: b) std::shared_ptr
Explanation:
std::shared_ptr allows multiple pointers to share ownership of a resource using reference counting.

85. Which smart pointer is used to break cyclic references?

a) std::unique_ptr
b) std::shared_ptr
c) std::weak_ptr
d) std::auto_ptr
Correct Answer: c) std::weak_ptr
Explanation:
std::weak_ptr holds a non-owning reference to a resource managed by a shared_ptr, which prevents reference cycles that can cause memory leaks.

86. What is `std::make_unique` used for?

a) To create a shared_ptr
b) To create a unique_ptr safely
c) To create a weak_ptr
d) To delete a unique_ptr
Correct Answer: b) To create a unique_ptr safely
Explanation:
std::make_unique is a factory function that safely creates a std::unique_ptr, ensuring exception safety and avoiding code duplication.

87. What is `std::make_shared` used for?

a) To create a unique_ptr
b) To create a shared_ptr safely and efficiently
c) To create a weak_ptr
d) To delete a shared_ptr
Correct Answer: b) To create a shared_ptr safely and efficiently
Explanation:
std::make_shared creates a shared_ptr and allocates the object and control block in a single memory allocation, improving performance and safety.

88. Can `std::unique_ptr` be copied?

a) Yes
b) No, it can only be moved
c) Only if it is const
d) Only if it is null
Correct Answer: b) No, it can only be moved
Explanation:
std::unique_ptr disables its copy constructor and copy assignment operator to enforce exclusive ownership; it can only be transferred via move semantics.

89. Can `std::shared_ptr` be copied?

a) Yes
b) No, it can only be moved
c) Only if it is const
d) Only if it is null
Correct Answer: a) Yes
Explanation:
std::shared_ptr can be copied; each copy increments the reference count, sharing ownership of the managed resource.

90. What happens to the managed object when a `std::unique_ptr` goes out of scope?

a) It is copied
b) It is moved
c) It is deleted
d) It becomes a dangling pointer
Correct Answer: c) It is deleted
Explanation:
When a std::unique_ptr goes out of scope, its destructor is called, which automatically deletes the managed object and frees the memory.

91. What is a pointer to a class member?

a) A pointer to an object of the class
b) A pointer that stores the offset of a member within a class
c) A pointer to a static member
d) A pointer to a class method
Correct Answer: b) A pointer that stores the offset of a member within a class
Explanation:
A pointer to a non-static class member does not store an absolute address, but rather the offset of that member within the class layout.

92. How do you declare a pointer to an integer member of class `MyClass`?

a) int* MyClass::*ptr;
b) int MyClass::*ptr;
c) int *MyClass ptr;
d) MyClass::int* ptr;
Correct Answer: b) int MyClass::*ptr;
Explanation:
The syntax for a pointer to a data member is `Type ClassName::*pointerName`.

93. How do you access a non-static member using a pointer to member and an object `obj`?

a) obj.*ptr
b) obj->*ptr
c) *obj.ptr
d) obj.ptr
Correct Answer: a) obj.*ptr
Explanation:
The .* operator is used to access a member of an object using a pointer to that member.

94. What is the `.*` operator used for?

a) To access a member via a pointer to member using an object
b) To access a member via a pointer to member using a pointer to an object
c) To dereference a pointer
d) To get the address of a member
Correct Answer: a) To access a member via a pointer to member using an object
Explanation:
The .* operator combines an object and a pointer-to-member to access the specific member of that object.

95. What is the `->*` operator used for?

a) To access a member via a pointer to member using an object
b) To access a member via a pointer to member using a pointer to an object
c) To dereference a pointer
d) To get the address of a member
Correct Answer: b) To access a member via a pointer to member using a pointer to an object
Explanation:
The ->* operator is used when you have a pointer to an object and a pointer to a member, allowing you to access the member through the object pointer.

96. What is the type of the `this` pointer in a non-const member function?

a) MyClass* const
b) const MyClass*
c) MyClass*
d) const MyClass* const
Correct Answer: a) MyClass* const
Explanation:
In a non-const member function, the `this` pointer is a constant pointer to a non-constant object, meaning the pointer itself cannot be changed, but the object can be.

97. What is the type of the `this` pointer in a const member function?

a) MyClass* const
b) const MyClass* const
c) const MyClass*
d) MyClass*
Correct Answer: b) const MyClass* const
Explanation:
In a const member function, the `this` pointer is a constant pointer to a constant object, meaning neither the pointer nor the object can be modified.

98. What happens when you apply the `&` operator to a reference?

a) It returns the address of the reference itself
b) It returns the address of the object the reference refers to
c) It causes a compilation error
d) It returns null
Correct Answer: b) It returns the address of the object the reference refers to
Explanation:
Applying the address-of operator to a reference yields the address of the underlying object it is bound to, not the reference itself.

99. What does taking the address of a reference actually return?

a) The address of the reference variable
b) The address of the object being referenced
c) A null pointer
d) An integer value
Correct Answer: b) The address of the object being referenced
Explanation:
Since a reference is an alias, taking its address is identical to taking the address of the original variable it refers to.

100. What is the output of `sizeof` applied to a reference?

a) The size of the reference itself
b) The size of the object being referenced
c) 0
d) 8 bytes always
Correct Answer: b) The size of the object being referenced
Explanation:
The sizeof operator applied to a reference returns the size of the underlying object type, not the size of the reference implementation.
← Previous: C++ OOP MCQs Object-Oriented Programming
Next →: C++ STL MCQs (Standard Template Library)
New
No Image

C++ Arrays / Lists - MCQs

1. What is a pointer in C++? a) A variable that stores the memory address of another variable b) A…

By MCQs Generator
NewC++ Classes and Objects - MCQs

C++ Classes and Objects - MCQs

In C++, a class is a fundamental building block of Object-Oriented Programming (OOP) that acts as a user-defined blueprint for…

By MCQs Generator
NewLatest C++ Basics MCQs

Latest C++ Basics MCQs

Created by Bjarne Stroustrup as an extension of the C programming language, C++ is a powerful, high-performance, statically typed language…

By MCQs Generator