LearnFrenzy
Sign in Take a free test

Static and Friend Members · Exercise 1 of 4

Static and Friend Members Static and Friend MCQ

  • 20 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Twenty multiple-choice questions on static data members and functions, inline static, statics in inheritance and templates, singletons, friend functions, friend classes, friend operators and the rules of friendship. Pick an option to check your answer instantly.

Question 11 Medium

Which combination implements the Meyers singleton?

A A public constructor and a global object
B A private constructor, deleted copy operations, and a static function returning a reference to a function-local static object
C A static pointer created with new in main
D A struct with only static members
Question page & discussion
Question 12 Hard

What is the "static initialization order fiasco"?

A Static members are initialized after main
B The initialization order of global/static objects in different source files is unspecified, so one may be used before it is constructed
C Static locals are initialized twice
D Static members cannot be initialized
Question page & discussion
Question 13 Easy

Which statement about a friend function of class X is true?

A It is a member function of X
B It is not a member, but it may access X's private and protected members
C It can only access public members
D It must be static
Question page & discussion
Question 14 Medium

Does it matter whether a friend declaration is placed in the public, protected or private section?

A Yes – only public friends work
B No – the friend declaration has the same effect in any section
C Private friends can access only private members
D Protected friends are inherited
Question page & discussion
Question 15 Easy

Class A declares friend class B;. Can member functions of A access B's private members?

A Yes, friendship is mutual
B No – only B gained access to A; A would need to be declared a friend of B
C Only static members
D Only if they are in the same file
Question page & discussion
Question 16 Medium

B is a friend of A, and C derives from B. Can C's member functions access A's private members?

A Yes, friendship is inherited
B No – friendship is not inherited
C Only protected members
D Only if C is also derived from A
Question page & discussion
Question 17 Medium

Money has an implicit constructor from int. Why is friend Money operator+(const Money&, const Money&) better than a member operator+?

A It is faster
B Both operands can then be converted implicitly, so 50 + price compiles as well as price + 50
C Member operators cannot return objects
D Friends can be virtual
Question page & discussion
Question 18 Medium

Why is operator<< for printing your class written as a non-member (often a friend)?

A Members cannot return references
B In std::cout << obj the left operand is the stream; a member operator would need your object on the left (obj << std::cout)
C It must be static
D Streams cannot be passed to members
Question page & discussion
Question 19 Hard

What is required before writing friend void Mechanic::tune(Engine&); inside class Engine?

A Nothing
B Mechanic must be fully defined (with tune declared) before that point; a forward declaration is not enough
C tune must be static
D Engine must be derived from Mechanic
Question page & discussion
Question 20 Hard

A friend function is defined inside the class body (a "hidden friend"). How is it found?

A By ordinary unqualified lookup anywhere
B Only through argument-dependent lookup – when an argument has the class type
C Only with ClassName::f()
D It cannot be called
Question page & discussion
Up next · Exercise 2Predict the Output20 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links