C++ Programming Questions and Answers – Friend Function

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer

Answer: a
Explanation: 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 <iostream>
#include <string>
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
View Answer

Answer: a
Explanation: As show() is a friend function of class Box hence any object from this function can access the private member of the class Box.
advertisement
advertisement

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

#include <iostream>
#include <string>
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
View Answer

Answer: c
Explanation: 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.
advertisement

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
View Answer

Answer: b
Explanation: A friend functions are not members of any class. Hence this class has only 2 member functions.
advertisement

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

#include <iostream>
#include <string>
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
View Answer

Answer: c
Explanation: 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 <iostream>
#include <string>
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
View Answer

Answer: a
Explanation: The program follows correct syntax and semantics hence no errors.

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

#include <iostream>
#include <string>
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
View Answer

Answer: c
Explanation: No function show() is defined in the scope of main() function.

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

#include <iostream>
#include <string>
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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: 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. 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
View Answer

Answer: 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.

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

Answer: a
Explanation: friend keyword is used to declare a friend function.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.