Skip to content
QuiznExam

Friend Functions in C++

Test your knowledge of friend functions in C++, including friend declarations, private and protected member access, syntax, classes, and common use cases.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. What is a friend function in C++?

    • A. A function that can access private and protected members of a class
    • B. A function that can only access public members
    • C. A function that must be a class member
    • D. A function that cannot access class data
    Show answer

    A. A function that can access private and protected members of a class

    A friend function is not a member of the class, but it is granted access to the class's private and protected members.

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

    • A. friend
    • B. private
    • C. access
    • D. trusted
    Show answer

    A. friend

    The friend keyword is used inside a class declaration to grant a function or another class access to private and protected members.

  3. 3. Where is a friend function typically declared?

    • A. Inside the class declaration
    • B. Only inside main()
    • C. Only inside a namespace
    • D. Inside a header file only
    Show answer

    A. Inside the class declaration

    A friend function is declared inside the class using the friend keyword, although its definition is normally outside the class.

  4. 4. Is a friend function a member function of the class that declares it?

    • A. No
    • B. Yes, always
    • C. Only if it is private
    • D. Only if it is static
    Show answer

    A. No

    A friend function is a non-member function that has been granted special access to the class.

  5. 5. Which access levels can a friend function access directly?

    • A. Private and protected
    • B. Public only
    • C. Protected only
    • D. Private only
    Show answer

    A. Private and protected

    Friend functions can directly access private and protected members of the class that grants friendship.

  6. 6. Which of the following is a correct friend function declaration?

    • A. friend void display();
    • B. friendly void display();
    • C. friendship void display();
    • D. private void display();
    Show answer

    A. friend void display();

    The friend keyword followed by the function declaration correctly declares a friend function inside a class.

  7. 7. How is a friend function normally called?

    • A. Like a regular non-member function
    • B. Only using the dot operator
    • C. Only using the arrow operator
    • D. Only through the class constructor
    Show answer

    A. Like a regular non-member function

    Because a friend function is not a member function, it is normally called like an ordinary function rather than through an object using the dot operator.

  8. 8. Can a friend function be declared in more than one class?

    • A. Yes, the same function can be declared as a friend of multiple classes
    • B. No, it can only belong to one class
    • C. Only if all classes are derived classes
    • D. Only if the function is static
    Show answer

    A. Yes, the same function can be declared as a friend of multiple classes

    A non-member function can be declared as a friend by multiple classes, allowing it to access the private and protected members of each class.

  9. 9. Which statement about friendship in C++ is correct?

    • A. Friendship is not automatically inherited
    • B. Friendship is always inherited
    • C. Friendship is automatically transferred to every derived class
    • D. Friendship applies to all classes in the program
    Show answer

    A. Friendship is not automatically inherited

    Friendship is granted explicitly and is not automatically inherited by derived classes.

  10. 10. Is friendship in C++ automatically mutual?

    • A. No
    • B. Yes, always
    • C. Only between derived classes
    • D. Only for constructors
    Show answer

    A. No

    If class A declares class B as a friend, that does not automatically make A a friend of B.

  11. 11. Can a friend function be declared as a member of another class?

    • A. Yes, a member function of another class can be declared as a friend
    • B. No, friend functions must always be global functions
    • C. Only if both classes have the same name
    • D. Only if both classes are abstract
    Show answer

    A. Yes, a member function of another class can be declared as a friend

    C++ allows a specific member function of another class to be declared as a friend.

  12. 12. What is one common use of friend functions?

    • A. Allowing closely related external functions to access private data
    • B. Preventing all access to class members
    • C. Replacing every constructor
    • D. Creating namespaces automatically
    Show answer

    A. Allowing closely related external functions to access private data

    Friend functions are useful when an external function needs controlled access to a class's private or protected data.

  13. 13. Which operator is commonly overloaded using a friend function because the left operand may not be an object of the class?

    • A. operator<<
    • B. operator.
    • C. operator::
    • D. operator?
    Show answer

    A. operator<<

    The stream insertion operator is commonly implemented as a friend function so it can access private data while supporting expressions such as std::cout << object.

  14. 14. Which statement about a friend function is correct?

    • A. It does not have a this pointer
    • B. It always has a this pointer
    • C. It must be declared static
    • D. It must be declared private
    Show answer

    A. It does not have a this pointer

    A friend function is not a member function, so it does not have the implicit this pointer.

  15. 15. Which statement best describes the purpose of the friend keyword in C++?

    • A. It grants specific non-member functions or classes access to private and protected members
    • B. It makes every member public
    • C. It creates an inheritance relationship
    • D. It prevents a class from being instantiated
    Show answer

    A. It grants specific non-member functions or classes access to private and protected members

    The friend keyword provides selective access to private and protected members without making the function or class a member of the granting class.