We have already covered C++ Friend Functions questions in the last set of MCQs. There, we have discussed some good questions. This set of MCQ(multiple choice questions focuses) on the C++ Friend Functions questions.
You should practice these interview questions to improve C++ programming skills needed for various interviews (like company interview, campus interview, walk-in interview), entrance exams, placements and other competitive exams. All the questions in this particular section are based on only “C++ Friend Functions MCQ“.
Instructions to be followed:-
- Read all the questions carefully.
- Out of the 4 given options for each questions, only ONE option will be correct.
- Choose the appropriate option.
- After attempting all the questions, click on “Finish” button to check the score.
- Share your result on social media handles available.
If you are not satisfied with your result or wants to attempt the Quiz again, don’t worry, you can try it again at the end (by refreshing the page). You can use Previous and Next button to switch to a different set of questions.
C++ MCQ | FRIEND FUNCTIONS
Now, start attempting the quiz.
C++ Friend Functions MCQ Questions
1. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Box
{
double width;
public:
friend void printWidth( Box box );
void setWidth( double wid );
};
void Box::setWidth( double wid )
{
width = wid;
}
void printWidth( Box box )
{
box.width = box.width * 2;
cout << "Width of box : " << box.width << endl;
}
int main( )
{
Box box;
box.setWidth(10.0);
printWidth( box );
return 0;
}
a) 40
b) 20
c) 5
d) 10
Answer: b) 20
Explanation: We are using the friend function for printwidth and multiplied the width value by 2, So we got the output as 20
2. 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
Answer: b) 2
Explanation: A friend functions are not members of any class. Hence this class has only 2 member functions.
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 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) Segmentation fault
d) Error
Answer: a) Value of capacity is: 10
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.
4. 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) Segmentation fault
d) Error
Answer: d) Error
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.
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class sample
{
private:
int a, b;
public:
void test()
{
a = 100;
b = 200;
}
friend int compute(sample e1);
};
int compute(sample e1)
{
return int(e1.a + e1.b) - 5;
}
int main()
{
sample e;
e.test();
cout << compute(e);
return 0;
}
a) 100
b) 200
c) 295
d) 300
Answer: c) 295
6. Predict the output of following program.
#include <iostream>
using namespace std;
class A
{
protected:
int x;
public:
A() {x = 0;}
friend void show();
};
class B: public A
{
public:
B() : y (0) {}
private:
int y;
};
void show()
{
A a;
B b;
cout << "The default value of A::x = " << a.x << " ";
cout << "The default value of B::y = " << b.y;
}
a) Compiler Error in show() because x is protected in class A
b) Compiler Error in show() because y is private in class b
c) The default value of A::x = 0 The default value of B::y = 0
d) Compiler Dependent
Answer: b)
Explanation: Please note that show() is a friend of class A, so there should not be any compiler error in accessing any member of A in show(). Class B is inherited from A, the important point to note here is friendship is not inherited. So show() doesn’t become a friend of B and therefore can’t access private members of B.
7. 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: 12345435
b) value of b is: 10
c) segmentation fault
d) error
Answer: d) Error
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.
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: 12345435
b) value of b is: 10
c) segmentation fault
d) error
Answer: d) Error
Explanation: Friend functions are not members of any class therefore they should not be called using class objects.
9. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class sample
{
int width, height;
public:
void set_values (int, int);
int area () {return (width * height);}
friend sample duplicate (sample);
};
void sample::set_values (int a, int b)
{
width = a;
height = b;
}
sample duplicate (sample rectparam)
{
sample rectres;
rectres.width = rectparam.width * 2;
rectres.height = rectparam.height * 2;
return (rectres);
}
int main ()
{
sample rect, rectb;
rect.set_values (2, 3);
rectb = duplicate (rect);
cout << rectb.area();
return 0;
}
a) 20
b) 24
c) 18
d) 16
Answer: b) 24
10. Predict the output the of following program.
#include <iostream>
using namespace std;
class B;
class A {
int a;
public:
A():a(0) { }
void show(A& x, B& y);
};
class B {
private:
int b;
public:
B():b(0) { }
friend void A::show(A& x, B& y);
};
void A::show(A& x, B& y) {
x.a = 10;
cout << "A::a=" << x.a << " B::b=" << y.b;
}
int main() {
A a;
B b;
a.show(a,b);
return 0;
}
a) Compiler Error
b) A::a=10 B::b=0
c) A::a=0 B::b=0
d) None
Answer: b) A::a=10 B::b=0
Explanation: This is simple program where a function of class A is declared as friend of class B. Since show() is friend, it can access private data members of B.
<< Prev- C++ MCQs : Friend Functions
>> Next- C++ MCQ : Pointer to Functions
Wants to learn more about C++ Programming? If yes, then don’t forget to explore all the topics of C++ programming. Here, is the complete set of Multiple Choice Questions and Answers. You should learn all the questions available there. These questions are very helpful for the preparation of placements, Competitive Exams and University level exams.
Learn about 404 ERROR page – EVERYTHING YOU NEED TO KNOW ABOUT 404 ERROR PAGE FOR SEO
For frontend projects, check and implements these beginner’s friendly projects :- TOP 7 PROJECTS FOR BEGINNERS TO PRACTICE HTML & CSS SKILLS
Learn about UML: WHAT IS UML?
ROLE OF UML IN OOAD(OBJECT-ORIENTED ANALYSIS AND DESIGN)
Do practice of HTML questions :- Multiple choice questions on HTML
Do practice of C programming :- Multiple choice questions on C programming
Want backlinks to your website? If yes, then follow this article and get to know in very easy way. How You Can Create High Quality Backlinks In 2021?
The above question set contains all the correct answers. But in any case, you find any typographical, grammatical or any other error in our site then kindly inform us. Don’t forget to provide the appropriate URL along with error description. So that we can easily correct it.
Thanks in advance.
For discussion about any question, join the below comment section. And get the solution of your query. Also, try to share your thoughts about the topics covered in this particular quiz.
Thank you for sharing with us, I believe this website really stands out : D.