Friend Function In C++ Mcq Exercise 1 - C++ Programming Tutorial
C++ Course / MCQ & Exercises / Friend Function In C++ Mcq Exercise 1

Friend Function In C++ Mcq Exercise 1

BLUF: Mastering Friend Function In C++ Mcq Exercise 1 is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Friend Function In C++ Mcq Exercise 1

C++ is renowned for its efficiency. Learn how Friend Function In C++ Mcq Exercise 1 enables low-level control and high-performance computing in the tutorial below.

  1. What do you mean by friend function in C++?
  • A function that belongs to a class.
  • A function which can access both private members and public members of a class.
  • A function which is defined inside the class.
  • A function that cannot access the members of a class.

Explanation:

The correct answer is option (b). A friend function is a non-member function, which is not part of a class but has 'special rights' of having access to a class and modifying the class's private or protected members.

  1. In C++, How can you declare a function as Friend function?
  • friend function functionName;
  • void friend functionName;
  • friend void functionName;
  • void functionName friend;

Explanation:

The correct answer is option (c). Friend keyword in C++ is mainly utilized to declare a function as friend function in a class and there also should be a return type for the function following functions name.

  1. What are the implications that occur as a result of one class declaring another class as a friend?
  • It can access all members of the declaring class in the friend class.
  • A friend class can only use the public functions and data members of the class that declared it as a friend.
  • The friend class is also allowed to access the members that are declared as private in the declaring class.
  • The friend class cannot access any members of the declaring class.

Explanation:

The correct answer is option (a). If a class is declared as friend to some other, all the functions members of the declaring class will gain the access to the private and protected members.

  1. Which of the following statement is correct about the friend function in C++?
  • Friend functions have the same access rights as member functions.
  • Friend functions can only access public members of the class.
  • Friend functions can access private and protected members of the class.
  • Friend functions must be defined inside the class.

Explanation:

The correct answer is option (c). This is because C++ provides the use of the friend keyword to allow the functions that require access to private and protected members of the function into it.

  1. Which of the following statement is not characteristic of friend functions:
  • They can access private and protected members of the class.
  • Friend functions are defined inside the class.
  • They are not members of the class.
  • They are declared anywhere in the class.

Explanation:

The accurate choice is alternative (b). Friend functions are not defined within the class but are solely declared inside the class.

  1. What will be the result of the C++ code snippet provided below?
Example

#include<iostream>

using namespace std;

class A 

{

    int x;

public:

    A() : x(10) {}

    friend void show(A &a);

};

void show(A &a) 

{

    cout << a.x;

}

int main()

{

    A obj;

    show(obj);

    return 0;

}
  • Garbage value
  • Compilation error

Explanation:

The accurate choice is option (b). Within the provided code snippet, the show function acts as a friend function with specified access to the private member x within class A. Consequently, it will display the value 10 as the output.

  1. What is the output of the subsequent C++ code?
Example

#include<iostream>

using namespace std;

class A 

{

    int x;

public:

    A(int val) : x(val) {}

    friend class B;

};

class B 

{

public:

    void display(A &a) {

        cout << a.x;

    }

};

int main()

{

    A obj(20);

    B b;

    b.display(obj);

    return 0;

}
  • Garbage value
  • Compilation error

Explanation:

The accurate choice is alternative (b). In the given illustration, class B serves as a friend function to class A, enabling the member function display within class B to access the private members of class A.

  1. What will be the result of the following C++ code snippet?
Example

#include<iostream>

using namespace std;

class A 

{

    int x;

protected:

    int y;

public:

    A() : x(10), y(20) {}

    friend void show(A &a);

};

void show(A &a) 

{

cout<<a.x<<a.y;

}

int main()

{

    A obj;

    show(obj);

    return 0;

}
  • 10 0
  • 0 20
  • 10 20
  • Compilation error

Explanation:

The accurate choice is alternative (c). Within the provided illustration, the friend function display is capable of accessing both the private attribute x and the protected attribute y within class A.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience