Skip to main content

Important Introduction to Programming Short Questions

Chapter: 28
Question 1:
Can the new operator be used with class objects?
Answer: Yes, the new operator can be used to allocate memory for class objects, which automatically calls the class constructor.
Chapter: 28
Question 2:
What is the purpose of a constructor in a C++ class?
Answer: A constructor in a C++ class initializes the data members of the class when an object is created.
Chapter: 28
Question 3:
How do you allocate memory for a class object dynamically in C++?
Answer: Memory for a class object is allocated dynamically in C++ using the syntax ClassName *ptr = new ClassName;.
Chapter: 28
Question 4:
What does the sizeof operator do in C++?
Answer: The sizeof operator in C++ returns the size in bytes of a data type or object, excluding member functions.
Chapter: 28
Question 5:
How is memory for data members allocated when an object is instantiated in C++?
Answer: When an object is instantiated in C++, memory is allocated for its data members, but member functions share a common memory area.
Chapter: 28
Question 6:
What is a memory leak, and how can it occur in C++?
Answer: A memory leak occurs when dynamically allocated memory is not deallocated, which can happen if the delete operator is not called after using new.
Chapter: 28
Question 7:
What is the difference between struct and class in C++?
Answer: In C++, members of a struct are public by default, while members of a class are private by default.
Chapter: 28
Question 8:
Why is it good practice to explicitly declare public and private keywords in C++?
Answer: Explicitly declaring public and private increases code readability and avoids confusion about the visibility of class members.
Chapter: 28
Question 9:
What is the role of a destructor in a C++ class?
Answer: A destructor in a C++ class deallocates resources and performs cleanup tasks when an object is destroyed.
Chapter: 28
Question 10:
Why should a destructor be provided when using dynamic memory allocation in C++?
Answer: A destructor should be provided to ensure that dynamically allocated memory is properly deallocated, preventing memory leaks.