Friend Function In C++ Mcq Exercise 5

  1. Can a friend function be declared as virtual?
  • Both
  • None

Explanation:

The correct answer is option (b). The Friend function can not be declared as virtual because it is not a member of the class; thus, it is not in the virtual table.

  1. Can a friend function access the static members of the class?
  • Both
  • None

Explanation:

The correct answer is option (a). Friend functions can access static members of the class, just as they can access private and protected non-static members.

  1. Can a friend function be declared in a namespace?
  • Both
  • None

Explanation:

The correct answer is option (a). Friend functions can be declared within a namespace, and they can prove to be a friend of those classes which are declared either in the same or in a different namespace.

  1. Pick the true statements from the following that would justify the correct definition for a friend function?
  • Friend functions should be declared inside the class.
  • Friend functions should be defined outside of the class.
  • The friend functions can be defined both inside and outside of the class.
  • Friend functions cannot be defined outside the class.

Explanation:

The correct answer is option (b). Friend functions are declared inside the class but must be defined outside the class.

  1. Which of the following statement define is a valid way to declare a friend function in multiple classes?
  • Declaring the function in each class individually.
  • Declaring the function in one class and inheriting it.
  • Declaring the function in one class and using the friend keyword in other classes.
  • Globally using friend keyword.

Explanation:

The correct answer is option (a). In order to declare a friend function in multiple classes, it must be declared individually in each class using the friend keyword.

  1. In how many classes can a function be declared as friend function?
  • Multiple
  • None

Explanation:

The correct answer is option (c). A single friend function can be declared as a friend in multiple classes.

Input Required

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