Important Introduction to Programming Short Questions
Chapter: 39
Question 1:
What happens to a pointer after delete is called?
Answer:
After delete, the pointer becomes a dangling pointer and should not be used until it is re-assigned.
Chapter: 39
Question 2:
How do you initialize a matrix with variable size in C++?
Answer:
Use a pointer to an integer and allocate memory dynamically in the constructor based on the required rows and columns.
Chapter: 39
Question 3:
Why is it necessary to write a destructor if the constructor allocates memory?
Answer:
A destructor is necessary to free memory allocated by the constructor to avoid memory leaks and ensure proper memory management.
Chapter: 39
Question 4:
What is a memory leak?
Answer:
A memory leak occurs when memory is allocated but not properly freed, leading to unreferenced blocks of memory that cannot be used.
Chapter: 39
Question 5:
How do you free dynamically allocated memory in C++?
Answer:
You use the delete[] operator followed by the pointer that points to the allocated memory, e.g., delete[] m;.
Chapter: 39
Question 6:
What is the difference between initialization and assignment in C++?
Answer:
Initialization occurs at the time of variable declaration, while assignment occurs after the variable has been declared.
Chapter: 39
Question 7:
What happens if you assign one object to another without an overloaded assignment operator?
Answer:
Without an overloaded assignment operator, the default member-to-member assignment is used, which can cause issues with shared pointers.
Chapter: 39
Question 8:
Why is it problematic if two objects have pointers pointing to the same memory?
Answer:
If one object is destroyed, the memory is freed, leaving the pointer in the other object pointing to invalid memory.
Chapter: 39
Question 9:
What does a copy constructor do?
Answer:
A copy constructor creates a new object as a copy of an existing object, ensuring that dynamically allocated memory is properly copied.
Chapter: 39
Question 10:
What is the difference between shallow copy and deep copy?
Answer:
A shallow copy copies pointer values directly, while a deep copy allocates new memory and copies the values pointed to by the pointers.