C++ Programming Questions and Answers – Friends

This section on C++ MCQs (multiple choice questions) focuses on “Friends”. One shall practice these MCQs to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our multiple choice questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C multiple choice questions on “Friends” along with answers, explanations and/or solutions:

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) both a & b
d) None of the mentioned
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:None.

3. What is the syntax of friend function?
a) friend class1 Class2;
b) friend class;
c) friend class
d) None of the mentioned
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 is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class Box
  4.     {
  5.         double width;
  6.         public:
  7.         friend void printWidth( Box box );
  8.         void setWidth( double wid );
  9.     };
  10.     void Box::setWidth( double wid )
  11.     {
  12.         width = wid;
  13.     }
  14.     void printWidth( Box box )
  15.     {
  16.         box.width = box.width * 2;
  17.         cout << "Width of box : " << box.width << endl;
  18.     }
  19.     int main( )
  20.     {
  21.         Box box;
  22.         box.setWidth(10.0);
  23.         printWidth( box );
  24.         return 0;
  25.    }

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
Output:
$ g++ friend.cpp
$ a.out
20

5. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class sample 
  4.     {
  5.         int width, height;
  6.         public:
  7.         void set_values (int, int);
  8.         int area () {return (width * height);}
  9.         friend sample duplicate (sample);
  10.     };
  11.     void sample::set_values (int a, int b) 
  12.     {
  13.         width = a;
  14.         height = b;
  15.     }
  16.     sample duplicate (sample rectparam)
  17.     {
  18.         sample rectres;
  19.         rectres.width = rectparam.width * 2;
  20.         rectres.height = rectparam.height * 2;
  21.         return (rectres);
  22.     }  
  23.     int main ()  
  24.     {
  25.         sample rect, rectb;
  26.         rect.set_values (2, 3);
  27.         rectb = duplicate (rect);
  28.         cout << rectb.area();
  29.         return 0;
  30.     }

a) 20
b) 16
c) 24
d) None of the mentioned
View Answer

Answer:c
Explanation:In this program, we are using the friend function for duplicate function and calculating the area of the rectangle.
Output:
$ g++ friend1.cpp
$ a.out
24

6. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class sample;
  4.     class sample1 
  5.     {
  6.         int width, height;
  7.         public:
  8.         int area ()
  9.         {
  10.             return (width * height);}
  11.             void convert (sample a);
  12.         };
  13.     class sample 
  14.     {
  15.         private:
  16.         int side;
  17.         public:
  18.         void set_side (int a)
  19.         { 
  20.             side = a;
  21.         }
  22.         friend class sample1;
  23.     };
  24.     void sample1::convert (sample a) 
  25.     {
  26.         width = a.side;
  27.         height = a.side;
  28.     }
  29.     int main () 
  30.     {
  31.         sample sqr;
  32.         sample1 rect;
  33.         sqr.set_side(6);
  34.         rect.convert(sqr);
  35.         cout << rect.area();
  36.         return 0;
  37.     }

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.
Output:
$ g++ friend2.cpp
$ a.out
36

7. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class base
  4.     {
  5.         int val1, val2;
  6.         public:
  7.         int get()
  8. 	{
  9.             val1 = 100;
  10.             val2 = 300;
  11. 	}
  12.         friend float mean(base ob);
  13.     };
  14.     float mean(base ob)
  15.     {
  16.         return float(ob.val1 + ob.val2) / 2;
  17.     }
  18.     int main()
  19.     {
  20.         base obj;
  21.         obj.get();
  22.         cout << mean(obj);
  23.         return 0;
  24.     }

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.
Output:
$ g++ friend3.cpp
$ a.out
200

8. What is the output of this program?

  1.     #include <iostream>
  2.     using namespace std;
  3.     class sample
  4.     {
  5.         private:
  6.         int a, b;
  7.         public:
  8.         void test()
  9.         {
  10.             a = 100;
  11.             b = 200;
  12.         }
  13.         friend int compute(sample e1);
  14.     };
  15.     int compute(sample e1)
  16.     {
  17.         return int(e1.a + e1.b) - 5;
  18.     }
  19.     int main()
  20.     {
  21.         sample e;
  22.         e.test();
  23.         cout  << compute(e);
  24.         return 0;
  25.     }

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.
Output:
$ g++ friend4.cpp
$ a.out
295

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:None.

10. Where does keyword ‘friend’ should be placed?
a) function declaration
b) function definition
c) main function
d) None of the mentioned
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.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

Here’s the list of Best Reference Books in C++ Programming Language.

To practice all features of C++ programming language, here is complete set on 1000+ Multiple Choice Questions and Answers on C++.

Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.

1000 C++ MCQs | Quiz

Types
Booleans
Character Types
Integer Types
Floating Point Types
Sizes
Void
Enumerations
Declaration
Pointers
Arrays
Pointers into Arrays
Constants
References
Pointer to Void
Structures
Operators
Statements
Comments and Indentation
Function Declarations
Argument Passing
Value Return
Overloaded Function Names
Default Arguments
Unspecified Number of Arguments
Pointer to Function
Macros
Modularization and Interfaces
Namespaces
Exceptions
Linkage
Header Files Usage
Classes
User Defined Types
Objects
Operator Functions
Complex Number Type
Conversion Operators
Friends
Large Objects
Essential Operators
Subscripting
Function Call
Dereferencing
Increment and Decrement
String Class
Derived Classes
Abstract Classes
Design of Class Hierarchies
Class Hierarchies & Abstract Classes
Simple String Template
Function Templates
Template Arguments for Policy Usage
Specialization
Derivation and Templates
Error Handling
Grouping of Exceptions
Catching Exceptions
Resource Management
Exceptions That Are Not Errors
Exception Specifications
Uncaught Exceptions
Exceptions and Efficiency
Error Handling Alternatives
Standard Exceptions
Class Hierarchies Introduction
Multiple Inheritance
Access Control
Run Time Type Information
Pointers to Members
Free Store
Standard Library Design
Container Design
Vector
Sequences
Sequence Adapters
Associative Containers
Almost Containers
Defining a New Container
Standard Library Algorithms
Sequences and Containers
Function Objects
Nonmodifying Sequence Algorithms
Modifying Sequence Algorithms
Stored Sequences
Heaps
Min and Max
Permutations
C Style Algorithms
Iterators and Sequences
Checked Iterators
Allocators
String Characters
Basic String
C Standard Library
Output Stream
Input Stream
Formatting
File Streams and String Streams
Buffering
Locale
C Input Output
Numeric Limits
Standard Mathematical Functions
Vector Arithmetic
Generalized Numeric Algorithms
Random Numbers
Subscribe Newsletter & Posts
Follow Manish & Sanfoundry