LearnFrenzy
Sign in Take a free test

Encapsulation · Exercise 1 of 4

Encapsulation Encapsulation MCQ

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

Twenty multiple-choice questions on encapsulation in C++: data hiding, access rules, invariants, getters and setters, leaking internals, const-correctness, immutability, private inheritance and pimpl. Pick an option to check your answer instantly.

Question 11 Medium

Which member is a good candidate for mutable?

A The account balance
B A cache of a computed value or a call counter that does not change the object's observable value
C The object's ID
D A const data member
Question page & discussion
Question 12 Medium

What is an important advantage of immutable objects?

A They use less memory
B They can be shared freely, including between threads, because no one can change them after construction
C They can be modified faster
D They do not need constructors
Question page & discussion
Question 13 Hard

A class has a const int id_; member. What happens to its copy assignment operator?

A It works normally
B The implicit copy assignment is deleted, because id_ cannot be assigned
C It assigns only non-const members
D It throws an exception
Question page & discussion
Question 14 Medium

Why do many guidelines prefer private data with protected member functions over protected data members?

A Protected data is slower
B Every derived class – including ones written later – can modify protected data, so the base class cannot guarantee its invariants
C Protected data cannot be initialized
D Protected data is not inherited
Question page & discussion
Question 15 Hard

What relationship does class Stack : private std::vector<int> express?

A Stack IS-A vector
B "Implemented in terms of": Stack reuses vector's implementation but hides its interface
C vector IS-A Stack
D Stack HAS-MANY vectors
Question page & discussion
Question 16 Hard

What is the main purpose of the pimpl idiom?

A Faster function calls
B Hiding private members from the header, so changing them does not force users to recompile and the binary interface stays stable
C Allowing multiple inheritance
D Avoiding constructors
Question page & discussion
Question 17 Hard

A class holds std::unique_ptr<Impl> where Impl is only forward-declared in the header. Why must the destructor be defined in the .cpp file?

A Destructors are always in .cpp files
B unique_ptr needs the complete Impl type to delete it, and Impl is complete only in the .cpp file
C To make it virtual
D It doesn't matter
Question page & discussion
Question 18 Medium

Which pairing is correct?

A Encapsulation: interfaces; abstraction: private data
B Encapsulation: hiding data and protecting state; abstraction: exposing only essential behaviour and hiding implementation complexity
C They are the same thing
D Encapsulation is only for structs
Question page & discussion
Question 19 Easy

When is it reasonable to use a struct with all-public data members?

A Never
B For simple data aggregates with no invariants, such as a Point {x, y} or a configuration record
C For bank accounts
D Whenever performance matters
Question page & discussion
Question 20 Medium

Which return type gives callers read-only, no-copy access to a private std::vector<double>?

A std::vector&lt;double&gt;
B std::span&lt;const double&gt;
C double*
D std::vector&lt;double&gt;&amp;
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