LakshyaEducation.in

VEDIC MATHS Video Series
  • Home
  • Video Series
    • Vedic Maths Videos
    • Quantitative Aptitude Videos
    • Class 8 Maths Videos
    • Class 9 Maths Videos
    • Class 10 Maths Videos
  • Quiz & Solutions
  • Blog
  • Store
  • Login
  • Contact Us
  • Home
  • Topic
  • C++ Programming
  • Friends

C++ Programming

FRIENDS MCQs

Total Questions : 10

Question 1.

Where does keyword ‘friend’ should be placed?


  1.    function declaration
  2.    function definition
  3.    main function
  4.    None of the mentioned
 Discuss Question
Answer is Option A. -> function declaration

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.

Question 2.

Pick out the correct statement.


  1.    A friend function may be a member of another class.
  2.    A friend function may not be a member of another class.
  3.    A friend function may or may not be a member of another class.
  4.    None of the mentioned
 Discuss Question
Answer is Option C. -> A friend function may or may not be a member of another class.

None.

Question 3.


What is the output of this program?



1.
#include
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
  1.    100
  2.    200
  3.    300
  4.    295
 Discuss Question
Answer is Option D. -> 295

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

Question 4.


What is the output of this program?


1.
#include
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.
}

  1.    200
  2.    150
  3.    100
  4.    300
 Discuss Question
Answer is Option A. -> 200

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

Question 5.


What is the output of this program?



1.
#include
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
  1.    24
  2.    35
  3.    16
  4.    36
 Discuss Question
Answer is Option D. -> 36

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

Question 6.


What is the output of this program?



1.
#include
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
  1.    20
  2.    16
  3.    24
  4.    None of the mentioned
 Discuss Question
Answer is Option C. -> 24

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

Question 7.

Which keyword is used to declare the friend function?


  1.    firend
  2.    friend
  3.    classfriend
  4.    myfriend
 Discuss Question
Answer is Option B. -> friend

None.

Question 8.

What is the syntax of friend function?


  1.    friend class1 Class2;
  2.    friend class;
  3.    friend class
  4.    None of the mentioned
 Discuss Question
Answer is Option A. -> friend class1 Class2;

In option a, the class2 is the friend of class1 and it can access all the private and protected 

members of class1.

Question 9.

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
 Discuss Question
Answer is Option A. -> private and protected members of a class cannot be accessed from outside

Friend is used to access private and protected members of a class from outside the same class.

Question 10.


What is the output of this program?



1.
#include
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
  1.    40
  2.    5
  3.    10
  4.    20
 Discuss Question
Answer is Option D. -> 20

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

  • Share on Facebook!
  • Share on Pinterest!

Sub Topics

  • Abstract Classes
  • Access Control
  • Argument Passing
  • Arrays
  • Booleans
  • C++ Concepts
  • Catching Exceptions
  • Character Types
  • Class Hierarchies And Abstract Classes
  • Class Hierarchies Introduction
  • Classes
  • Comments And Indentation
  • Complex Number Type
  • Constants
  • Constructors And Destructors
  • Container Design
  • Conversion Operators
  • Declaration
  • Default Arguments
  • Dereferencing
  • Derivation And Templates
  • Derived Classes
  • Design Of Class Hierarchies
  • Enumerations
  • Error Handling
  • Error Handling Alternatives
  • Essential Operators
  • Exception Specifications
  • Exceptions
  • Exceptions And Efficiency
  • Exceptions That Are Not Errors
  • Floating Point Types
  • Free Store
  • Friends
  • Function Call
  • Function Declarations
  • Function Templates
  • Functions
  • Grouping Of Exceptions
  • Header Files Usage
  • Increment And Decrement
  • Integer Types
  • Large Objects
  • Linkage
  • Macros
  • Modularization And Interfaces
  • Multiple Inheritance
  • Namespaces
  • Objects
  • Objects And Classes
  • Oops Concepts
  • Operator Functions
  • Operators
  • Overloaded Function Names
  • Pointer To Function
  • Pointer To Void
  • Pointers
  • Pointers Into Arrays
  • Pointers To Members
  • References
  • Resource Management
  • Run Time Type Information
  • Sequence Adapters
  • Sequences
  • Simple String Template
  • Sizes
  • Specialization
  • Standard Exceptions
  • Standard Library Design
  • Statements
  • String Class
  • Structures
  • Subscripting
  • Template Arguments To Specify Policy Usage
  • Types
  • Uncaught Exceptions
  • Unspecified Number Of Arguments
  • User Defined Types
  • Value Return
  • Vector
  • Void

Recent Posts

  • Sail E0 Exam Results Cancelled - Exam Will Be Rescheduled
  • Best Ssc Exam For Girls
  • Sail E0 Results 2022
  • Ssc Exam Guide Book
  • Vedic Maths Faq
  • Quantitative Aptitude Faqs
  • Is Ssc Difficult Than Upsc?

Recent Questions

Q.   The Enzymes That Catalyze The Reactions Of The Krebs Cycle A....

Q.   IC Counters Are

Q.   Synonym Of PROMULGATE

Q.   ______bridge Measures The Unknown Inductance By Comparing It....

Q.   An Imaginary Number Is One That Exists Only In The Mind Of T....

Q.   The Following System Is Not Generally Used

Q.   Exposure To Chemicals Having Carcinogenic Properties Cause

Q.   Determine Output: Void Main() { Int I=0, J=1, K=2, M; M = I+....

Q.   Sulfanilamide Was Synthesized By Which Of The Following Rese....

Q.   The Personnel Which Deal With The Computer And Its Managemen....

Q.   What Is The Detention Time Considered For The Contact Zone W....

Q.   A, B Can Do A Piece Of Work In 30 Days , While B Can C Do Th....

Q.   Identify The Exact Answer For The Blank (2)?

Q.   Thermosetting Polymers As Compared To Thermoplastic Polymers....

Q.   Where Did Last World Cup Soccer Tournament Took Place (2010)....

Q.   Recently, The Lancet Medical Journal Has Published Its New R....

Q.    what Is The Length Of A Square I Its Perimeter Is 160 Cm. ....

Q.   Which Of These Methods Of Httpd Class Is Used To Read Data F....

Q.   Coolant Present In The Primary Circuit Of A Pressurised Wate....

Q.   What Is The Father Of A Calf Called?

Topics

Computer Aptitude
SAIL Junior Officer (E-0)
10th Grade
11th Grade
12th Grade
4th Grade
5th Grade
6th Grade
7th Grade
8th Grade
9th Grade
NCERT
Cat
Commerce
Computer Science
Engineering
English
General Knowledge
Ias
Management
Quantitative Aptitude
Reasoning Aptitude
General Studies (Finance And Economics)
Vedic Maths
Analytical Instrumentation
Biochemistry
Bioinformatics
Biology
Biotechnology
Bitsat
Business Statistics
C Programming
C++ Programming
Cell Biology
Chemistry
Cost Accounting
Drug And Pharmaceutical Biotechnology
Electrical Measurement And Instrumentation
Environment Management
Environmental Biotechnology
Enzyme Technology
Financial Management And Financial Markets
Gate
General Science
Geography
Heat Transfer
History And National Movements
Human Anatomy And Physiology
Human And Cultural Diversity
Human Resource Management
Indian Economy
Indian Geography
Indian History
Indian Polity
Instrumentation Transducers
International Relations
Life Sciences
Marketing And Marketing Management
Mass Transfer
Mechanics Of Materials
Microbiology
Neet
Professional Communication
Renewable Energy
Sociology
Surveying
Total Quality Management
Uidai Aadhaar Supervisor Certification
Virology
LakshyaEducation.in
Lakshya Education
Bhilai,Chattisgarh,India
Email: admin@lakshyaeducation.in Phone: 07893519977 (WhatsApp)

Quick Links

  • Vedic Maths
  • Quantitative Aptitude
  • Class – IX Maths
  • Class – X Maths
  • Blog

Our Services

  • About us
  • Privacy
  • TOS
  • Refund / Cancellation
  • Contact
  • Affiliate Program
  • Copyright © 2022 All Right Reserved | Lakshya Education     ( )
    Login / Register

    Your Account will be created automatically when you click the below Google or Facebook Login Button.
    •   Login With Facebook
    •  Login With Google
     Login With Email/Password