Important Introduction to Programming Short Questions
Chapter: 29
Question 1:
What are the possible drawbacks of using friend functions?
Answer:
Friend functions can violate the encapsulation principle, making it harder to maintain and secure the code.
Chapter: 29
Question 2:
How do you pass an object to a friend function in C++?
Answer:
An object is typically passed to a friend function through its parameter list.
Chapter: 29
Question 3:
What is the purpose of the addBoth function in Sample Program 2?
Answer:
The addBoth function adds the private data members of two different classes and displays the result.
Chapter: 29
Question 4:
Can a class have multiple friend functions?
Answer:
Yes, a class can have multiple friend functions, each with different purposes and access to private data members.
Chapter: 29
Question 5:
In Sample Program 3, what operation does the mulBoth function perform?
Answer:
The mulBoth function multiplies the private data members of two classes and returns the result.
Chapter: 29
Question 6:
Can the same private data member name be used in two different classes?
Answer:
Yes, the same private data member name can be used in different classes as each class has its own scope.
Chapter: 29
Question 7:
Why might friend functions be rarely used in practice?
Answer:
Friend functions might be rarely used because they can break encapsulation, making the code harder to maintain and more error-prone.
Chapter: 30
Question 8:
What is a reference in C++?
Answer:
A reference in C++ is an alias or synonym for another variable. It allows two names to refer to the same variable without creating a new variable.
Chapter: 30
Question 9:
How is a reference declared in C++?
Answer:
A reference is declared using the & operator, e.g., int &i where i is a reference to an integer.
Chapter: 30
Question 10:
Can a reference be re-assigned to another variable after initialization?
Answer:
No, once a reference is initialized, it cannot be re-assigned to another variable; it always refers to the original variable it was assigned to.