250+ TOP MCQs on Friend Function and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “Friend Function”.

1. What is a friend function in C++?
a) A function which can access all the private, protected and public members of a class
b) A function which is not allowed to access any member of any class
c) A function which is allowed to access public and protected members of a class
d) A function which is allowed to access only public members of a class
Answer: a
Clarification: Friend function in C++ is a function which can access all the private, protected and public members of a class.

2. What will be the output of the following C++ code?

#include 
#include 
using namespace std;
class Box
{
	int capacity;
    public:
	Box(int cap){
		capacity = cap;
	}
 
	friend void show();
};
 
void show()
{	
	Box b(10);
	cout<<"Value of capacity is: "<<b.capacity<<endl;
}
 
int main(int argc, char const *argv[])
{
	show();
	return 0;
}

a) Value of capacity is: 10
b) Value of capacity is: 100
c) Error
d) Segmentation fault
Answer: a
Clarification: As show() is a friend function of class Box hence any object from this function can access the private member of the class Box.

3. What will be the output of the following C++ code?

#include 
#include 
using namespace std;
class Box
{
	int capacity;
    public:
	Box(int cap){
		capacity = cap;
	}
	friend void show();
};
 
void Box::show()
{	
	Box b(10);
	cout<<"Value of capacity is: "<<b.capacity<<endl;
}
 
int main(int argc, char const *argv[])
{
	show();
	return 0;
}

a) Value of capacity is: 10
b) Value of capacity is: 100
c) Error
d) Segmentation fault
Answer: c
Clarification: Though it is used to declare the friend functions inside classes they are not members of any class therefore when we giving the definition to friend function show() we should not use Box::show() way of defining it.

4. How many member functions are there in this C++ class excluding constructors and destructors?

class Box
{
	int capacity;
   public:
	void print();
	friend void show();
	bool compare();
	friend bool lost();
};

a) 1
b) 2
c) 3
d) 4
Answer: b
Clarification: A friend functions are not members of any class. Hence this class has only 2 member functions.

5. What will be the output of the following C++ code?

#include 
#include 
using namespace std;
class B
{
	int b;
   public:
	B(int i){
		b = i;
	}
};
 
class C
{
	B b;
    public:
	C(int i){
		b = B(i);
	}
	friend void show();
};
 
void show()
{
	C c(10);
	cout<<"value of b is: "<<c.b.b<<endl;
}
 
int main(int argc, char const *argv[])
{
	show();
	return 0;
}

a) value of b is: 10
b) value of b is: 12345435
c) error
d) segmentation fault
Answer: c
Clarification: There is two error in the program. First the program doesn’t have a default constructor for the class B which is used when the object of B is declared inside the class C. Second show() is friend function of class C therefore it can access only private member of class C, not B therefore when we are doing c.b.b here the last b is private member of class B which is not accessible.

6. What will be the output of the following C++ code?

#include 
#include 
using namespace std;
class B
{
	int b;
     public:
	B(){}
	B(int i){
		b = i;
	}
	int show(){
		return b;
	}
};
 
class C
{
	B b;
     public:
	C(int i){
		b = B(i);
	}
	friend void show();
};
 
void show()
{
	C c(10);
	cout<<"value of b is: "<<c.b.show()<<endl;
}
 
int main(int argc, char const *argv[])
{
	show();
	return 0;
}

a) value of b is: 10
b) value of b is: 12345435
c) error
d) segmentation fault
Answer: a
Clarification: The program follows correct syntax and semantics hence no errors.

7. What will be the output of the following C++ code?

#include 
#include 
using namespace std;
class B
{
	int b;
   public:
	B(){}
	B(int i){
		b = i;
	}
	int show(){
		return b;
	}
};
 
class C
{
	B b;
    public:
	C(int i){
		b = B(i);
	}
	friend void show(){
 
		C c(10);
		cout<<"value of b is: "<<c.b.show()<<endl;
	}
};
 
int main(int argc, char const *argv[])
{
	show();
	return 0;
}

a) value of b is: 10
b) value of b is: 12345435
c) error
d) segmentation fault
Answer: c
Clarification: No function show() is defined in the scope of main() function.

8. What will be the output of the following C++ code?

#include 
#include 
using namespace std;
class B
{
	int b;
    public:
	B(){}
	B(int i){
		b = i;
	}
	int show(){
		return b;
	}
};
 
class C
{
	B b;
    public:
	C(int i){
		b = B(i);
	}
	friend void show(){
 
		C c(10);
		cout<<"value of b is: "<<c.b.show()<<endl;
	}
};
 
int main(int argc, char const *argv[])
{
	C c(1);
	c.show();
	return 0;
}

a) value of b is: 10
b) value of b is: 12345435
c) error
d) segmentation fault
Answer: c
Clarification: Friend functions are not members of any class therefore they should not be called using class objects.

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
Answer: c
Clarification: 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. 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 mentioned
Answer: d
Clarification: 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.

11. Which keyword is used to represent a friend function?
a) friend
b) Friend
c) friend_func
d) Friend_func
Answer: a
Clarification: friend keyword is used to declare a friend function.

Leave a Reply

Your email address will not be published. Required fields are marked *