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

1

Which operator is used to get the memory address of a variable in C++?

Correct Answer: B) & (ampersand)

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.

2

What is the output of this code?
int x = 5; int* ptr = &x; cout << *ptr;

Correct Answer: B) 5

The pointer ptr stores the address of x. The dereference operator (*) is used to access the value at that address, which is 5.

3

Which keyword is used to allocate memory dynamically in C++?

Correct Answer: B) new

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.

4

What is the correct way to deallocate memory that was allocated with 'new'?

Correct Answer: B) delete ptr

The 'delete' operator is used to deallocate memory that was previously allocated with 'new'. Using 'free()' is for memory allocated with 'malloc()' in C.

5

What is a dangling pointer?

Correct Answer: B) A pointer that points to deallocated memory

A dangling pointer is a pointer that points to memory that has been freed or deallocated. Accessing such pointers leads to undefined behavior.

6

What is the difference between a pointer and a reference in C++?

Correct Answer: D) All of the above

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.

7

What is the purpose of the 'const' keyword when used with pointers?

Correct Answer: C) Both A and B depending on placement

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.

8

What is the output of this code?
int arr[3] = {10, 20, 30}; int* p = arr; cout << *(p+1);

Correct Answer: B) 20

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.

9

What is a memory leak in C++?

Correct Answer: B) When memory is allocated but not deallocated

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.

10

What is the purpose of the 'nullptr' keyword introduced in C++11?

Correct Answer: D) All of the above

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

11

What is the difference between 'delete' and 'delete[]' in C++?

Correct Answer: A) delete is for single objects, delete[] for arrays

'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.

12

What is a smart pointer in C++?

Correct Answer: A) A pointer that automatically deallocates memory

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.

13

What is the output of this code?
int x = 5; int& ref = x; ref = 10; cout << x;

Correct Answer: B) 10

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.

14

What is the purpose of the 'void*' pointer in C++?

Correct Answer: B) To point to any data type

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.

15

What is a function pointer in C++?

Correct Answer: A) A pointer that points to executable code

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.

Your score: 0/15