Skip to main content

Important Introduction to Programming Short Questions

Chapter: 34
Question 1:
How do you initialize an array of objects at the time of declaration?
Answer: You can initialize an array of objects at declaration using a list of constructor calls, like Date yourDate[3] = { Date(10, 24, 1980), Date(06, 14, 1985), Date(07, 09, 1986) };. This initializes each element with the specified constructor arguments.
Chapter: 34
Question 2:
What happens to the remaining objects in an array when only some elements are initialized?
Answer: When only some elements of an array are initialized, the remaining elements are initialized using the default constructor. For example, Date myDate[10] = { Date(09, 03, 1970), Date(08, 23, 1974) }; initializes the first two objects with specific values and the remaining objects with default values.
Chapter: 34
Question 3:
How do you create an array of objects dynamically in C++?
Answer: An array of objects can be created dynamically using the new operator, such as Date *appointments = new Date[10];. This allocates space for the array and calls the default constructor for each element.
Chapter: 34
Question 4:
What is the correct way to delete a dynamically allocated array of objects?
Answer: The correct way to delete a dynamically allocated array of objects is to use delete[], like delete[] appointments;. This ensures that the destructor is called for each object and the memory is properly deallocated.
Chapter: 34
Question 5:
What happens if you use delete instead of delete[] for an array of objects?
Answer: Using delete instead of delete[] for an array of objects only deallocates the memory for the first object and may cause memory leaks for the remaining objects. This is because delete does not account for the entire array and does not call the destructor for all objects.
Chapter: 34
Question 6:
Can you overload the new and delete operators in C++?
Answer: Yes, you can overload new and delete operators to customize memory management. This can be done either globally or within a specific class to control how memory allocation and deallocation are handled.
Chapter: 34
Question 7:
What is the difference between overloading new and delete globally versus within a class?
Answer: Overloading new and delete globally affects all uses of new and delete throughout the program, whereas overloading them within a class affects only that specific class. Overloading globally can interfere with other parts of the program, while class-specific overloading affects only instances of that class.
Chapter: 34
Question 8:
How does overloading the new operator differ from overloading the delete operator?
Answer: Overloading the new operator changes how memory is allocated, while overloading the delete operator changes how memory is deallocated. Both operators must maintain their specific prototypes and handle memory management tasks according to their roles. Overloaded new may also initialize memory, whereas delete must properly deallocate it.
Chapter: 34
Question 9:
Why is it important to use [] with delete when deallocating an array?
Answer: It is important to use delete[] to inform the compiler that an array of objects is being deallocated, ensuring that the destructor is called for each object and that the entire array's memory is properly freed. Using delete without [] may lead to memory leaks and improper deallocation.
Chapter: 34
Question 10:
How does the overloaded [] operator help with array bounds checking?
Answer: Overloading the [] operator allows you to include bounds checking within the operator function. This ensures that any attempt to access an array element outside its valid range is caught and handled appropriately, thus preventing out-of-range errors and potential memory corruption.