Function Questions with Answers

8 practice questions on Function (C++) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

Sample questions

Question 1Medium

What is the output of this program...?

    #include <iostream>
    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;
    }
  1. 100
  2. 200
  3. 300
  4. 295

Answer: D. 295

Explanation

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

Output:
295

Step-by-step solution

  1. The friend function compute can read the private members a and b of sample e1.
  2. After e.test() runs, a is 100 and b is 200.
  3. Compute returns (100 + 200) − 5, which is 300 − 5.
  4. So the output is 295.

Why the other options are wrong

  • A. 100 is only the value of a, and b is not added.
  • B. 200 is only the value of b.
  • C. 300 is the sum, but the − 5 in the return is missed.

Tip: Trace the values through each call, and remember a friend can read private members.

Question 2Medium

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

  1. firend
  2. friend
  3. classfriend
  4. myfriend

Answer: B. friend

Explanation

To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows:

class Box
{
   double width;
   double length;
   friend void printWidth( Box box );
   void setWidth( double wid );
};

To declare all member functions of class ClassTwo as friends of class ClassOne, place a following declaration in the definition of class ClassOne: friend class ClassTwo;

Step-by-step solution

  1. The keyword used to declare a friend function is 'friend'.
  2. This keyword precedes the function prototype within the class definition.
  3. For example, 'friend void printWidth( Box box );' declares a friend function.

Why the other options are wrong

  • A. The correct keyword is 'friend', not 'firend'.
  • C. There's no such keyword as 'classfriend'. It's just 'friend'.
  • D. 'myfriend' is not a valid keyword for declaring friend functions.

Tip: Remember: 'friend' is the correct keyword, not 'firend', 'classfriend', or 'myfriend'.

Question 3Medium

What is the output of this program?

#include <iostream>
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;
}
  1. 24
  2. 35
  3. 16
  4. 36

Answer: D. 36

Explanation

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

Output:
36

Step-by-step solution

  1. Sample1 is a friend of sample, so convert() can read sample's private side.
  2. sqr.set_side(6) sets side to 6, and convert() sets width = 6 and height = 6.
  3. area() returns 6 × 6 = 36, which is option D.

Why the other options are wrong

  • A. 24 is 4 × 6, which would need different width and height.
  • B. 35 is not a product of the values set here.
  • C. 16 is 4 × 4, which is not what convert() stores.

Tip: A friend class can read private members of the class that names it as a friend.

Question 4Medium

In C++, where is the keyword "friend" placed?

  1. function declaration
  2. function definition
  3. main function
  4. None of the mentioned

Answer: A. function declaration

Explanation

The keyword friend goes only in the friend function's declaration inside the class, not in its definition outside the class. The declaration is what grants the function access to the class's private members.

Step-by-step solution

  1. The friend keyword is written inside the class body, in front of a function declaration.
  2. This tells the compiler that the named function can access the class's private members.
  3. The keyword is not written in the function definition or in main.
  4. The answer is function declaration.

Why the other options are wrong

  • B. Friend goes in the declaration inside the class, not in the definition.
  • C. Main has no special role with friend; friend is for a function that is a friend of a class.
  • D. Option A is correct, so none of the mentioned is wrong.

Tip: Friend is written inside the class, in front of the function declaration.

Question 5Medium

What is the output of this program?

    #include<iostream>
    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;
    }
  1. 20
  2. 16
  3. 24
  4. None of the mentioned

Answer: C. 24

Explanation

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

Output:
24

Step-by-step solution

  1. set_values(2, 3) sets rect to width 2 and height 3.
  2. duplicate() returns a new object with width 2 × 2 = 4 and height 3 × 2 = 6.
  3. Rectb area is 4 × 6 = 24, which is option C.

Why the other options are wrong

  • A. 20 would be 4 × 5, which does not match the doubled values.
  • B. 16 would be 4 × 4, which uses the wrong height.
  • D. 24 is listed, so 'none of the mentioned' is wrong.

Tip: Work out each object's values step by step before taking the area.

Question 6Medium

What is the output of this program...?

    #include <iostream>
    using namespace std;
    class base
    {
        int val1, val2;
        public:
        void 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;
    }
  1. 200
  2. 150
  3. 100
  4. 300

Answer: A. 200

Explanation

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

Output:
200

Step-by-step solution

  1. get() sets val1 to 100 and val2 to 300 in obj.
  2. mean(obj) is a friend function, so it can read val1 and val2 directly.
  3. ob.val1 + ob.val2 = 100 + 300 = 400.
  4. float(400) / 2 = 200, which prints as 200.
  5. So the output is 200.

Why the other options are wrong

  • B. 150 would be the average of 100 and 200, but val2 is 300.
  • C. 100 is only val1, and the code divides the sum by 2.
  • D. 300 is only val2, not the mean of the two values.

Tip: A friend function can read private members, so trace which values were set before the call.

Question 7Medium

Which rule will not affect the friend function...?

  1. private and protected members of a class cannot be accessed from outside
  2. private and protected member can be accessed anywhere
  3. both a & b
  4. None of the mentioned

Answer: A. private and protected members of a class cannot be accessed from outside

Explanation

Friend is used to access private and protected members of a class from outside the same class.
A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class.
Even though the prototypes for friend functions appear in the class definition, friends are not member functions.
A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.

Step-by-step solution

  1. The rule being asked about is that private and protected members cannot be accessed from outside the class.
  2. A friend function is outside the class but is allowed to access those members.
  3. So this access rule does not restrict a friend function, which is option A.

Why the other options are wrong

  • B. This describes access that is not allowed, so it is not a rule that is set aside for friends.
  • C. Both a and b are not the correct rule, since option B is false.
  • D. Option A is the rule a friend is exempt from, so the answer is not none.

Tip: A friend declaration is the only exception to class access rules: it lets a named function or class reach private and protected members.

Question 8Medium

What is the output of this program...?

#include <iostream>
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;
}
  1. 40
  2. 5
  3. 10
  4. 20

Answer: D. 20

Explanation

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

Step-by-step solution

  1. The friend function printWidth receives box by value, so it works on a copy.
  2. SetWidth sets the width of the original box to 10.0.
  3. Inside printWidth, the copy's width is doubled to 20.
  4. The cout line prints the copy's width, which is 20.
  5. The output is 20.

Why the other options are wrong

  • A. 40 would need the width to be doubled twice, but it is doubled once.
  • B. 5 would need the width to be halved, but the function doubles it.
  • C. 10 is the original width, but the copy is doubled before it is printed.

Tip: Pass-by-value changes only the copy, so the original object stays the same.

Practise all 8 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C++ topics · All topics