C++ Pointers & Dynamic Memory MCQ Quiz
Test your knowledge of C++ pointers, references, and dynamic memory management with this interactive quiz. Select the correct answer for each question and see immediate feedback.
Medium Level Questions Medium
Which operator is used to get the memory address of a variable in C++?
The address-of operator (&) returns the memory address of a variable. The asterisk (*) is used for dereferencing pointers, the arrow (->) for accessing members through pointers, and :: for scope resolution.
What is the output of this code?
int x = 5; int* ptr = &x; cout << *ptr;
The pointer ptr stores the address of x. The dereference operator (*) is used to access the value at that address, which is 5.
Which keyword is used to allocate memory dynamically in C++?
The 'new' operator is used for dynamic memory allocation in C++. It allocates memory on the heap and returns a pointer to the allocated memory.
What is the correct way to deallocate memory that was allocated with 'new'?
The 'delete' operator is used to deallocate memory that was previously allocated with 'new'. Using 'free()' is for memory allocated with 'malloc()' in C.
What is a dangling pointer?
A dangling pointer is a pointer that points to memory that has been freed or deallocated. Accessing such pointers leads to undefined behavior.
What is the difference between a pointer and a reference in C++?
All statements are correct differences between pointers and references: References must be initialized when declared, cannot be reassigned to refer to a different object, and should not be null, whereas pointers have none of these restrictions.
What is the purpose of the 'const' keyword when used with pointers?
The placement of 'const' determines its meaning: 'const int* ptr' means the pointed value is constant, 'int* const ptr' means the pointer itself is constant, and 'const int* const ptr' means both are constant.
What is the output of this code?
int arr[3] = {10, 20, 30}; int* p = arr; cout << *(p+1);
Pointer arithmetic: p points to arr[0], so p+1 points to arr[1]. Dereferencing *(p+1) gives the value at arr[1], which is 20.
What is a memory leak in C++?
A memory leak occurs when dynamically allocated memory is not freed (deallocated) after it is no longer needed, causing the program to consume more and more memory over time.
What is the purpose of the 'nullptr' keyword introduced in C++11?
nullptr was introduced to address issues with NULL: it's not implicitly convertible to integral types, provides type safety, and avoids ambiguity with integer 0.
Advanced Level Questions Advanced
What is the difference between 'delete' and 'delete[]' in C++?
'delete' is used to deallocate memory for a single object created with 'new', while 'delete[]' is used to deallocate memory for an array of objects created with 'new[]'. Using the wrong form leads to undefined behavior.
What is a smart pointer in C++?
Smart pointers are objects that manage the lifetime of dynamically allocated memory. They automatically deallocate the memory when it's no longer needed, helping prevent memory leaks.
What is the output of this code?
int x = 5; int& ref = x; ref = 10; cout << x;
A reference is an alias for a variable. Changing the value through the reference (ref) changes the original variable (x). So after 'ref = 10', x becomes 10.
What is the purpose of the 'void*' pointer in C++?
A void* (void pointer) can hold the address of any type of object, but it cannot be dereferenced directly without explicit casting to the appropriate pointer type.
What is a function pointer in C++?
A function pointer stores the address of a function, allowing the function to be called indirectly through the pointer. This is useful for callback mechanisms and implementing function tables.