Home »
Aptitude Questions and Answers »
C++ Aptitude Questions and Answers
C++ Friend Function Aptitude Questions and Answers
C++ Friend Function Aptitude: This section contains C++ Friend Function Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on February 19, 2021
1) There are the following statements that are given below, which of them are correct about the friend in C++?
- A friend is a keyword in C++.
- A friend keyword is used to violate the rules of data encapsulation in C++.
- A friend keyword is used to access the private members of the class.
- A friend keyword is used to access the protected members of the class.
Options:
- A and B
- A and C
- A, B, and C
- A, B, C, and D
Correct Answer - 1
A and B
Explanation:
All given statements are correct about the friend in C++.
2) In C++, the friend can be used with?
- Member functions
- Non-member functions
- Class
- All of the above
Options:
- A and B
- A and C
- B and C
- D
Correct Answer - 4
D
Explanation:
All given options can be used with friend function in C++.
3) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
int main()
{
friend int A = 10;
cout << A << endl;
return 0;
}
Options:
- 10
- Compilation error
- Runtime error
- Linker error
Correct Answer - 2
Compilation error
Explanation:
The above code will generate a compilation error. Because we cannot create a variable as a friend
4) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
friend void fun()
{
cout << "hello world" << endl;
}
int main()
{
fun();
return 0;
}
Options:
- hello world
- Compilation error
- Runtime error
- Linker error
Correct Answer - 2
Compilation error
Explanation:
The above code will generate a compilation error. Because we cannot use "friend" keyword outside the class.
5) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
void fun();
class Sample {
private:
int var;
public:
Sample()
{
var = 10;
}
friend void fun();
};
void fun()
{
Sample S;
cout << S.var << endl;
}
int main()
{
fun();
return 0;
}
Options:
- 10
- Compilation error
- Runtime error
- Linker error
Correct Answer - 1
10
Explanation:
The above code will print "10" on the console screen.
6) What is the correct output of given code snippets in C++?
#include <iostream>
using namespace std;
class Sample {
private:
int var;
public:
Sample()
{
var = 10;
}
friend void fun();
};
void fun()
{
Sample S;
cout << S.var << endl;
}
int main()
{
fun();
return 0;
}
Options:
- 10
- Compilation error
- Runtime error
- Linker error
Correct Answer - 1
10
Explanation:
The above code will print "10" on the console screen.
7) What is the correct output of given code snippets in C++?
#include <iostream>
using namespace std;
class Sample {
private:
int var;
public:
Sample()
{
var = 10;
}
friend void fun();
};
friend void fun()
{
Sample S;
cout << S.var << endl;
}
int main()
{
fun();
return 0;
}
Options:
- 10
- Compilation error
- Runtime error
- Linker error
Correct Answer - 2
Compilation error
Explanation:
The above code will generate a compilation error, because we cannot use "friend" keyword outside the class.
8) Can we access private data members of class outside without using a friend function?
- Yes
- No
Correct Answer - 1
Yes
Explanation:
Yes, we can access private data members of class outside using pointers.
9) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class SampleA {
private:
int var;
public:
SampleA()
{
var = 10;
}
friend void fun();
};
class SampleB {
private:
int var;
public:
SampleB()
{
var = 20;
}
friend void fun();
};
void fun()
{
SampleA Ob1;
SampleB Ob2;
cout << Ob1.var << ", " << Ob2.var << endl;
}
int main()
{
fun();
return 0;
}
Options:
- 10, 20
- 10
- Garbage Value
- Compilation error
Correct Answer - 1
10, 20
Explanation:
The above code will print "10, 20" on the console screen.
10) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class SampleA {
private:
int var;
public:
SampleA()
{
var = 10;
}
friend void fun();
};
class SampleB {
private:
int var;
public:
SampleB()
{
var = 20;
}
friend void fun();
};
void fun(SampleA A, SampleB B)
{
cout << A.var << "," << B.var << endl;
}
int main()
{
SampleA Ob1;
SampleB Ob2;
fun(ob1, ob2);
return 0;
}
Options:
- 10, 20
- 10
- Garbage Value
- Compilation error
Correct Answer - 4
Compilation error
Explanation:
The above code will generate a compile-time error.
11) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
class SampleA {
private:
int var;
public:
SampleA()
{
var = 10;
}
friend class SampleB;
};
class SampleB {
private:
int var;
public:
SampleB()
{
SampleA A;
var = 20;
cout << var << ", " << A.var;
}
};
int main()
{
SampleB B;
return 0;
}
Options:
- 10, 20
- 20, 10
- Garbage Value
- Compilation error
Correct Answer - 2
20, 10
Explanation:
The above code will print "20, 10" on the console screen.
12) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class SampleA {
private:
int var;
public:
SampleA()
{
SampleB B;
var = 10;
cout << var << ", " << B.var;
}
friend class SampleB;
};
class SampleB {
private:
int var;
public:
SampleB()
{
var = 20;
}
};
int main()
{
SampleB B;
return 0;
}
Options:
- 10, 20
- 20, 10
- Garbage Value
- Compilation error
Correct Answer - 4
Compile-time error
Explanation:
The above code will generate syntax error because SampleA cannot access a private member of SampleB.
13) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
class SampleA {
private:
int var;
public:
SampleA()
{
var = 10;
}
friend void SampleB::fun();
};
class SampleB {
private:
int var;
public:
SampleB()
{
SampleA A;
var = 20;
cout << A.var << ", ";
}
void fun()
{
cout << var << endl;
}
};
int main()
{
SampleB B;
B.fun();
return 0;
}
Options:
- 10
- 20, 10
- 20
- Compilation error
Correct Answer - 4
Compilation error
Explanation:
The above code will generate syntax error.
14) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
class B;
class ABC {
public:
void fun();
};
class XYZ {
private:
int var;
public:
XYZ()
{
var = 10;
}
friend void ABC::fun();
};
void ABC::fun()
{
XYZ ob;
cout << ob.var;
}
int main()
{
ABC a;
a.fun();
return 0;
}
Options:
- 10
- 20
- Compilation error
- Runtime error
Correct Answer - 1
10
Explanation:
The above code will print "10" on the console screen.
15) Suppose if class A is a friend of class B, then class B will also be a friend of class A?
- Yes
- No
Correct Answer - 2
No
Explanation:
Suppose if class A is a friend of class B, then class B may or may not be a friend of class A.