Embercode
✕
Object-Oriented ProgrammingQuick quiz

Composition

6 questions. Covers Composition over inheritance.

Questions 1 to 5 of 6

  1. 1. Which of these is a has-a relationship, the kind that composition models?
  2. 2. This Stack is meant to be last-in, first-out. What is the design problem? ``cpp class Stack : public std::vector<int> { public: void push(int value) { push_back(value); } int pop() { int top = back(); pop_back(); return top; } }; ``
  3. 3. True or false: a Car should inherit from Engine so it can call the engine's start() method.
  4. 4. What does this program print? ``cpp #include <iostream> #include <optional> #include <utility> class Radio { public: explicit Radio(int station) : station_{station} {} void play() const { std::cout << "station " << station_ << '\n'; } private: int station_; }; class Car { public: void install_radio(Radio r) { radio_ = std::move(r); } void music() const { if (radio_) { radio_->play(); } else { std::cout << "silent\n"; } } private: std::optional<Radio> radio_; }; int main() { Car a; a.music(); a.install_radio(Radio{101}); a.music(); } ``
  5. 5. The Car below lists only three wheels. Wheel has an explicit constructor that takes a position, and no default constructor. What happens? ``cpp class Car { public: Car() : wheels_{Wheel{1}, Wheel{2}, Wheel{3}} {} private: std::array<Wheel, 4> wheels_; }; ``

5 question(s) left on this page.