Skip to content
main-logo
  • +91 637-050-2482
  • santuitreturns@gmail.com
Menu
Menu
  • Home
  • Income Tax
    • Income From Salary
    • Profit or gain from Business/Profession.
    • Capital Gain
    • Income From Other Sources
    • 80C to 80U
    • TDS & TCS
    • ITR FORMS
  • International Taxation
    • Transfer Pricing
    • Non-Resident Taxation
    • Foreign Tax Credit (FTC)
    • Model Tax Convention
    • Base Erosion and Profit Shifting (BEPS)
  • GST
  • Accounting
  • MCQs
    • NEET
    • NEET QUIZ TEST
    • NEET PG MCQ’s
    • NEET PG QUIZ TEST
    • Civil Engineering
    • Mechanical Engineering MCQs
    • CHSL EXAM
      • Logical Reasoning
  • Others
    • Job Tips
  • CA Courses
    • CA Inter/IPCC

10+ TOP C++ MCQs on Friends

Posted on September 24, 2023

1. Which rule will not affect the friend function?

A. private and protected members of a class cannot be accessed from outside
B. private and protected member can be accessed anywhere
C. protected member can be accessed anywhere
D. private member can be accessed anywhere

View Answer

Answer: A

Explanation: 

 Friend is used to access private and protected members of a class from outside the same class.

2. Which keyword is used to declare the friend function?

A. firend
B. friend
C. classfriend
D. myfriend

View Answer

Answer: B

Explanation:

 friend keyword is used to declare a friend function in C++.

3. What is the syntax of friend function?

A. friend class1 Class2;
B. friend class;
C. friend class
D. friend class()

View Answer

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. What will be the output of the following C++ code?
    #include 
    using namespace std;
    class Box
    {
        double width;
        public:
        friend void printWidth( Box box );
        void setWidth( double wid );
    };
    void Box::setWidth( double wid )
    {
        width = wid;
    }
    void printWidth( Box box )
    {
        box.width = box.width * 2;
        cout << "Width of box : " << box.width << endl;
    }
    int main( )
    {
        Box box;
        box.setWidth(10.0);
        printWidth( box );
        return 0;
   }

A. 40
B. 5
C. 10
D. 20

View Answer

Answer: D

Explanation:

We are using the friend function for printwidth and multiplied the width value by 2, So we got the output as 20

5. What will be the output of the following C++ code?
    #include 
    using namespace std;
    class sample 
    {
        int width, height;
        public:
        void set_values (int, int);
        int area () {return (width * height);}
        friend sample duplicate (sample);
    };
    void sample::set_values (int a, int B. 
    {
        width = a;
        height = b;
    }
    sample duplicate (sample rectparam)
    {
        sample rectres;
        rectres.width = rectparam.width * 2;
        rectres.height = rectparam.height * 2;
        return (rectres);
    }  
    int main ()  
    {
        sample rect, rectb;
        rect.set_values (2, 3);
        rectb = duplicate (rect);
        cout << rectb.area();
        return 0;
    }

A. 20
B. 16
C. 24
D. 18

View Answer

Answer: C

Explanation:

In this program, we are using the friend function for duplicate function and calculating the area of the rectangle.

6. What will be the output of the following C++ code?
    #include 
    using namespace std;
    class sample;
    class sample1 
    {
        int width, height;
        public:
        int area ()
        {
            return (width * height);}
            void convert (sample A.;
        };
    class sample 
    {
        private:
        int side;
        public:
        void set_side (int A.
        { 
            side = a;
        }
        friend class sample1;
    };
    void sample1::convert (sample A. 
    {
        width = a.side;
        height = a.side;
    }
    int main () 
    {
        sample sqr;
        sample1 rect;
        sqr.set_side(6);
        rect.convert(sqr);
        cout << rect.area();
        return 0;
    }

A. 24
B. 35
C. 16
D. 36

View Answer

Answer: D

Explanation:

In this program, we are using the friend for the class and calculating the area of the square.

7. What will be the output of the following C++ code?
    #include 
    using namespace std;
    class base
    {
        int val1, val2;
        public:
        int get()
	{
            val1 = 100;
            val2 = 300;
	}
        friend float mean(base oB.;
    };
    float mean(base oB.
    {
        return float(ob.val1 + ob.val2) / 2;
    }
    int main()
    {
        base obj;
        obj.get();
        cout << mean(obj);
        return 0;
    }

A. 200
B. 150
C. 100
D. 300

View Answer

Answer: A

Explanation:

In this program, We are finding the mean value by declaring the function mean as a friend of class base.

8. What will be the output of the following C++ code?
    #include 
    using namespace std;
    class sample
    {
        private:
        int a, b;
        public:
        void test()
        {
            a = 100;
            b = 200;
        }
        friend int compute(sample e1);
    };
    int compute(sample e1)
    {
        return int(e1.a + e1.B. - 5;
    }
    int main()
    {
        sample e;
        e.test();
        cout  << compute(e);
        return 0;
    }

A. 100
B. 200
C. 300
D. 295

View Answer

Answer: D

Explanation:

In this program, we are finding a value from the given function by using the friend for compute function.

9. Pick out the correct statement.

A. A friend function may be a member of another class
B. A friend function may not be a member of another class
C. A friend function may or may not be a member of another class
D. None of the mentioned

View Answer

Answer: C

Explanation:

 A friend function may or may not be a member of another class is the correct statement.

10. Where does keyword ‘friend’ should be placed?

A. function declaration
B. function definition
C. main function
D. block function

View Answer

Answer: A

Explanation:

 The keyword friend is placed only in the function declaration of the friend function and not in the function definition because it is used toaccess the member of a class

    You May Also Like...

  • 10+ TOP MCQs on C++ Concepts
  • 10+ TOP MCQs on C++ Concepts -2
  • 10+ TOP MCQs on C++ Concepts-3
  • 10+ TOP C++ MCQs on Void
  • 10+ TOP C++ MCQs on Declaration
  • 10+ TOP C++ MCQs on Enumerations
  • 10+ TOP C++ MCQs on Pointers
  • 10+ TOP C++ MCQs on Pointer to Structures

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Quick Links

  • Home
  • About Us
  • Privacy Policy
  • Terms of Use
  • Disclaimer
  • Contact Us

Categories

  • Income Tax
  • International Taxation
  • GST
  • MCQs
  • Others
  • CA Courses

Latest Posts

  • Five changes in ITR forms of FY 2024-25 (AY 2025-26)
  • Form 10-IEA: Option to Choose Old Tax Regime
  • What is Section 54EC of the Income Tax Act?
  • What is Section 54F of the Income Tax Act?
©2025 Online Solves. All rights Reserved | Developed by AlgoPage IT Solutions Pvt. Ltd.
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT