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
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.
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
a) firend
b) friend
c) classfriend
d) myfriend
View Answer
Answer:b
Explanation:None.
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
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.
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?
a) 40
b) 5
c) 10
d) 20
View Answer
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
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?
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
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?
a) 24
b) 35
c) 16
d) 36
View Answer
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
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?
a) 200
b) 150
c) 100
d) 300
View Answer
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
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
a) 100
b) 200
c) 300
d) 295
View Answer
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
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
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.
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
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.
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.
0 comments :
Post a Comment