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

Friend Function In C++ Mcq Exercise 3

BLUF: Mastering Friend Function In C++ Mcq Exercise 3 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 3

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

In the C++ code provided below, what will be the resulting output?

Example

#include<iostream>

using namespace std;

class A 

{

    int x;

public:

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

    friend void modify(A &a);

};

void modify(A &a) 

{

    a.x += 5;

}

int main() 

{

    A obj1(10), obj2(20);

    modify(obj1);

    modify(obj2);

cout<<obj1.x<<""<<obj2.x;

    return 0;

}
  • 15 25
  • 10 20
  • Compilation error
  • Runtime error

Explanation:

The correct answer is option (c). Since the friend function modifies the private member x of class A, the private member x cannot be immediately accessed from the main function.

  1. What can be the right way of declaring a friend function in C++ that takes the object of class A as a parameter?
  • friend void function(A);
  • friend void function(A &);
  • friend void function(A *);
  • All of the above are correct.

Explanation:

The correct answer is option (d). Friend functions can take objects of a class by value, reference, or pointer.

  1. Can the friend function of the base class access the private members of the derived class?
  • Both
  • None

Explanation:

The accurate choice is alternative (b). The Friend function within the base class can access both private and protected members of the base class, but not those of the derived class.

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(30) {}

    friend void setX(A &a, int val);

};

void setX(A &a, int val) 

{

    a.x = val;

}

int main() 

{

    A obj;

    setX(obj, 50);

    cout << obj.x;

    return 0;

}
  • Compilation error

Explanation:

The answer you're looking for is choice (d). The private data member "x" within the class "A" is not directly accessible in the main function. Trying to output obj.x using cout will result in a compilation error.

Now, let's determine the output of the C++ code provided below.

Example

#include<iostream>

using namespace std;

class A 

{

    int x;

public:

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

    friend void show(const A &a);

};

void show(const A &a) {

    cout << a.x;

}

int main()

{

    A obj1(10);

    const A obj2(20);

    show(obj1);

    show(obj2);

    return 0;

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

Explanation:

The correct answer is option (c). The shows friend function can access the private member x of both non-const and const objects of class A. After that, it prints their values sequentially.

  1. Which of the following statements about friend functions is incorrect?
  • Friend functions are not members of a class.
  • Friend functions can access all members of a class.
  • Friend functions in C++ can only be declared inside the class.
  • Friend functions can be friends of multiple classes.

Explanation:

The accurate choice is alternative (c). The assertion provided in option c is incorrect as friend functions are declared externally to the class but are not defined within the class.

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

#include<iostream>

using namespace std;

class A

{

    int x;

public:

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

    friend void doubleValue(A &a);

};

void doubleValue(A &a) 

{

    a.x *= 2;

    cout << a.x;

}

int main() 

{

    A obj(15);

    doubleValue(obj);

    return 0;

}
  • Compilation error

Explanation:

The accurate choice is alternative (b). The friend function doubleValue multiplies the value of the private member x in class A by 2 and displays the outcome, resulting in 30.

  1. If the friend keyword is eliminated during the function declaration in the following C++ code, what might be the output?
Example

#include<iostream>

using namespace std;

class A 

{

    int x;

public:

    A() : x(30) {}

    void setX(int val);

};

void setX(int val) 

{

    x = val;

}

int main() 

{

    A obj;

    obj.setX(50);

    return 0;

}
  • Compilation error

Explanation:

If the friend keyword is omitted from the function declaration, specifically in the scenario of 'setX', it implies that the function will be unable to access the private or protected members within class A. Consequently, the code will fail to compile.

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