Skip to main content

Important Introduction to Programming Short Questions

Chapter: 28
Question 1:
What happens if delete is not called for dynamically allocated memory?
Answer: If delete is not called, the memory is not returned to the free store, leading to a memory leak.
Chapter: 28
Question 2:
How do new and delete relate to malloc() and free() in C?
Answer: new and delete in C++ are higher-level replacements for malloc() and free() in C, automatically handling object construction and destruction.
Chapter: 28
Question 3:
What does the new operator return when used to allocate memory for an object?
Answer: The new operator returns a pointer of the object's type, which points to the allocated memory.
Chapter: 29
Question 4:
What is a class in object-oriented programming?
Answer: A class is a user-defined data type that provides encapsulation, allowing data and functions to be grouped together, with private and public members.
Chapter: 29
Question 5:
What is the purpose of encapsulation in a class?
Answer: Encapsulation separates the interface from the implementation, hiding the internal workings of a class while exposing only the necessary parts.
Chapter: 29
Question 6:
What is a friend function?
Answer: A friend function is a non-member function that is granted access to the private data members of a class.
Chapter: 29
Question 7:
How do you declare a friend function in a class?
Answer: A friend function is declared within the class using the friend keyword followed by the function prototype.
Chapter: 29
Question 8:
Can a friend function be placed in the private section of a class?
Answer: Yes, a friend function can be declared anywhere in the class, as its access is unaffected by private or public keywords.
Chapter: 29
Question 9:
Where is the definition of a friend function written?
Answer: The definition of a friend function is written outside the class.
Chapter: 29
Question 10:
Why is it risky to declare a function as a friend?
Answer: Declaring a function as a friend can be risky as it exposes the private data members to external functions, potentially violating encapsulation.