This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Friends”.
1. Which rule will not affect the friend function?
a) private and protected members of a class cannot be accessed from outside
b) private and protected member can be accessed anywhere
c) protected member can be accessed anywhere
d) private member can be accessed anywhere
View Answer
Explanation: Friend is used to access private and protected members of a class from outside the same class.
2. Which keyword is used to declare the friend function?
a) firend
b) friend
c) classfriend
d) myfriend
View Answer
Explanation: friend keyword is used to declare a friend function in C++.
3. What is the syntax of friend function?
a) friend class1 Class2;
b) friend class;
c) friend class
d) friend class()
View Answer
Explanation: In option a, the class2 is the friend of class1 and it can access all the private and protected members of class1.
4. 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) 5
c) 10
d) 20
View Answer
Explanation: We are using the friend function for printwidth and multiplied the width value by 2, So we got the output as 20
Output:
$ g++ friend.cpp $ a.out 20
5. 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) 16
c) 24
d) 18
View Answer
Explanation: In this program, we are using the friend function for duplicate function and calculating the area of the rectangle.
Output:
$ g++ friend1.cpp $ a.out 24
6. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class sample;
class sample1
{
int width, height;
public:
int area ()
{
return (width * height);}
void convert (sample a);
};
class sample
{
private:
int side;
public:
void set_side (int a)
{
side = a;
}
friend class sample1;
};
void sample1::convert (sample a)
{
width = a.side;
height = a.side;
}
int main ()
{
sample sqr;
sample1 rect;
sqr.set_side(6);
rect.convert(sqr);
cout << rect.area();
return 0;
}
a) 24
b) 35
c) 16
d) 36
View Answer
Explanation: In this program, we are using the friend for the class and calculating the area of the square.
Output:
$ g++ friend2.cpp $ a.out 36
7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class base
{
int val1, val2;
public:
int get()
{
val1 = 100;
val2 = 300;
}
friend float mean(base ob);
};
float mean(base ob)
{
return float(ob.val1 + ob.val2) / 2;
}
int main()
{
base obj;
obj.get();
cout << mean(obj);
return 0;
}
a) 200
b) 150
c) 100
d) 300
View Answer
Explanation: In this program, We are finding the mean value by declaring the function mean as a friend of class base.
Output:
$ g++ friend3.cpp $ a.out 200
8. 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) 300
d) 295
View Answer
Explanation: In this program, we are finding a value from the given function by using the friend for compute function.
Output:
$ g++ friend4.cpp $ a.out 295
9. Pick out the correct statement.
a) A friend function may be a member of another class
b) A friend function may not be a member of another class
c) A friend function may or may not be a member of another class
d) None of the mentioned
View Answer
Explanation: A friend function may or may not be a member of another class is the correct statement.
10. Where does keyword ‘friend’ should be placed?
a) function declaration
b) function definition
c) main function
d) block function
View Answer
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.
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.
- Apply for Computer Science Internship
- Practice Programming MCQs
- Check Programming Books
- Check Computer Science Books
- Practice Computer Science MCQs