C++ MCQs - Friend Function
This section focuses on the "Friend Function" in C++ programming langauge. These Multiple Choice Questions (MCQ) should be practiced to improve the C++ programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. A friend class can access ____________________ members of other class in which it is declared as friend.
A. private
B. protected
C. public
D. Both A and B
View Answer
Ans : D
Explanation: A friend class can access private and protected members of other class in which it is declared as friend.
2. A friend function can be
A. A method of another class
B. A global function
C. Both A and B
D. None of the above
View Answer
Ans : C
Explanation: A friend function can be:
a) A method of another class
b) A global function
3. If class A is a friend of B, then B doesn’t become a friend of A automatically.
A. TRUE
B. FALSE
C. Can be true and false
D. Can not say
View Answer
Ans : A
Explanation: Friendship is not mutual. If class A is a friend of B, then B doesn’t become a friend of A automatically.
4. Which of the following is false?
A. Friendship is not inherited
B. The concept of friends is there in Java.
C. Both A and B
D. None of the above
View Answer
Ans : B
Explanation: The concept of friends is not there in Java.
5. What will be output for the followiing code?
#include <iostream>
class A {
private:
int a;
public:
A() { a = 0; }
friend class B; // Friend Class
};
class B {
private:
int b;
public:
void showA(A& x)
{
std::cout << ""A::a="" << x.a;
}
};
int main()
{
A a;
B b;
b.showA(a);
}
A. A::a=0
B. A
C. a=0
D. A::0
View Answer
Ans : A
Explanation: the output for the foollowing code is : A::a=0
6. What will be output for the following code?
class Box
{
int capacity;
public:
void print();
friend void show();
bool compare();
friend bool lost();
};
A. 1
B. 2
C. 3
D. 4
View Answer
Ans : B
Explanation: A friend functions are not members of any class. Hence this class has only 2 member functions.
7. Which keyword is used to represent a friend function?
A. friend
B. Friend
C. friend_func
D. Friend_func
View Answer
Ans : A
Explanation: friend keyword is used to declare a friend function.
8. Which of the following is correct about friend functions?
A. Friend functions use the dot operator to access members of a class using class objects
B. Friend functions can be private or public
C. Friend cannot access the members of the class directly
D. All of the above
View Answer
Ans : D
Explanation: Friend function can be declared either in private or public part of the class. A friend function cannot access the members of the class directly. They use the dot membership operator with a member name.
9. Pick the correct statement.
A. Friend functions are in the scope of a class
B. Friend functions can be called using class objects
C. Friend functions can be invoked as a normal function
D. Friend functions can access only protected members not the private members
View Answer
Ans : C
Explanation: Friend functions are not in the scope of a class and hence cannot be called through a class object. A friend function can access all types of members of the class. They can be invoked as a normal function.
10. Where does keyword ‘friend’ should be placed?
A. function declaration
B. function definition
C. main function
D. block function
View Answer
Ans : A
Explanation: The keyword friend is placed only in the function declaration of the friend function and not in the function definition because it is used toaccess the member of a class.
Discussion