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++?

  1. A friend is a keyword in C++.
  2. A friend keyword is used to violate the rules of data encapsulation in C++.
  3. A friend keyword is used to access the private members of the class.
  4. A friend keyword is used to access the protected members of the class.

Options:

  1. A and B
  2. A and C
  3. A, B, and C
  4. A, B, C, and D

2) In C++, the friend can be used with?

  1. Member functions
  2. Non-member functions
  3. Class
  4. All of the above

Options:

  1. A and B
  2. A and C
  3. B and C
  4. D

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:

  1. 10
  2. Compilation error
  3. Runtime error
  4. Linker error

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:

  1. hello world
  2. Compilation error
  3. Runtime error
  4. Linker error

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:

  1. 10
  2. Compilation error
  3. Runtime error
  4. Linker error

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:

  1. 10
  2. Compilation error
  3. Runtime error
  4. Linker error

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:

  1. 10
  2. Compilation error
  3. Runtime error
  4. Linker error

8) Can we access private data members of class outside without using a friend function?

  1. Yes
  2. No

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:

  1. 10, 20
  2. 10
  3. Garbage Value
  4. Compilation error

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:

  1. 10, 20
  2. 10
  3. Garbage Value
  4. Compilation 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:

  1. 10, 20
  2. 20, 10
  3. Garbage Value
  4. Compilation error

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:

  1. 10, 20
  2. 20, 10
  3. Garbage Value
  4. Compilation error

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:

  1. 10
  2. 20, 10
  3. 20
  4. Compilation 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:

  1. 10
  2. 20
  3. Compilation error
  4. Runtime error

15) Suppose if class A is a friend of class B, then class B will also be a friend of class A?

  1. Yes
  2. No





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.