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
Answer: A
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
Answer: B
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()
Answer: A
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
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
Answer: D
Explanation:
We are using the friend function for printwidth and multiplied the width value by 2, So we got the output as 20
5. What will be the output of the following C++ code?
#include
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
Answer: C
Explanation:
In this program, we are using the friend function for duplicate function and calculating the area of the rectangle.
6. What will be the output of the following C++ code?
#include
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
Answer: D
Explanation:
In this program, we are using the friend for the class and calculating the area of the square.
7. What will be the output of the following C++ code?
#include
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
Answer: A
Explanation:
In this program, We are finding the mean value by declaring the function mean as a friend of class base.
8. What will be the output of the following C++ code?
#include
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
Answer: D
Explanation:
In this program, we are finding a value from the given function by using the friend for compute function.
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
Answer: C
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
Answer: 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