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
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
#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: "<
A. Value of capacity is: 10
B. Value of capacity is: 100
C. Error
D. Segmentation fault
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.
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: "<
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. 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
Explanation:
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: "<
A. value of b is: 10
B. value of b is: 12345435
C. error
D. segmentation fault
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
#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: "<
A. value of b is: 10
B. value of b is: 12345435
C. error
D. segmentation fault
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
#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: "<
A. value of b is: 10
B. value of b is: 12345435
C. error
D. segmentation fault
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
#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: "<
A. value of b is: 10
B. value of b is: 12345435
C. error
D. segmentation fault
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
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
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
Answer: A
Explanation:
friend keyword is used to declare a friend function.