C++ MCQs - Friend Function

This section focuses on the "Friend Function" in C++ programming langauge. These Multiple Choice Questions (MCQ) should be practiced to improve the C++ programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.

1. A friend class can access ____________________ members of other class in which it is declared as friend.

A. private
B. protected
C. public
D. Both A and B

View Answer


2. A friend function can be

A. A method of another class
B. A global function
C. Both A and B
D. None of the above

View Answer


3. If class A is a friend of B, then B doesn’t become a friend of A automatically.

A. TRUE
B. FALSE
C. Can be true and false
D. Can not say

View Answer


4. Which of the following is false?

A. Friendship is not inherited
B. The concept of friends is there in Java.
C. Both A and B
D. None of the above

View Answer


5. What will be output for the followiing code?

#include <iostream>
class A { 
private: 
    int a; 
  
public: 
    A() { a = 0; } 
    friend class B; // Friend Class 
}; 
  
class B { 
private: 
    int b; 
  
public: 
    void showA(A& x) 
    { 
         
        std::cout << ""A::a="" << x.a; 
    } 
}; 
  
int main() 
{ 
    A a; 
    B b; 
    b.showA(a); 
  
 }

A. A::a=0
B. A
C. a=0
D. A::0

View Answer


6. What will be output for the following code?

class Box
{
 int capacity;
   public:
 void print();
 friend void show();
 bool compare();
 friend bool lost();
};

A. 1
B. 2
C. 3
D. 4

View Answer


7. Which keyword is used to represent a friend function?

A. friend
B. Friend
C. friend_func
D. Friend_func

View Answer


8. Which of the following is correct about friend functions?

A. Friend functions use the dot operator to access members of a class using class objects
B. Friend functions can be private or public
C. Friend cannot access the members of the class directly
D. All of the above

View Answer


9. Pick the correct statement.

A. Friend functions are in the scope of a class
B. Friend functions can be called using class objects
C. Friend functions can be invoked as a normal function
D. Friend functions can access only protected members not the private members

View Answer


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

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

View Answer





Discussion



* You must be logged in to add comment.