Skip to content

Concept quiz: requirements, principles, and patterns

Difficulty: Intermediate

Key concepts

  • concept quiz
  • self-check
  • requirements
  • design principles
  • design patterns
  • pattern or no pattern

This quiz checks the main ideas of the requirements, principles, and patterns sections. Each question names the lesson section it comes from. Read the lessons first, or take the quiz first to find out which ones to read; both work.

Many questions here are judgement questions. Their answer depends on the facts stated in the question, and some accept more than one defensible design. Where that is so, the question says what decides between them. Several questions have “no pattern” as the best answer: a function, a value, or a plain struct can be the better design.

How to take it: choose your answer, select “Check answer”, and then read the explanation under every option, not only the one you chose. You can change an answer and check it again.

About this quiz.25 questions. Each one links to the lesson it comes from. Fact questions have one answer that follows from the lesson. Judgement questions depend on trade-offs stated in the question; where more than one answer can be defended, the question says so. This is a self-check for your own study, not a test or a certification.

Question 1 of 25FactYou are clarifying the print-release service with the facilities manager. Which of these questions follow the lesson’s advice on phrasing? Choose all that apply.

Choose all that apply.

Show the explained answers
  • Right: “How should printing be limited, if at all?”
    Right. It is open and neutral: the answer might be a page limit, a cost limit, or no limit, and the question does not suggest one.
  • Not right: “You’ll want a monthly page limit per person, right?”
    This is a leading question. People often agree with one just to be helpful, so you learn your own assumption back.
  • Not right: “Should we use a database?”
    This asks about a solution, which the facilities manager cannot answer. Ask about the need instead: “After a power cut, must held jobs still be there?”
  • Right: “Roughly how many jobs a day: hundreds, thousands, or tens of thousands?”
    Right. Asking for a range makes the question answerable by someone who has never measured it, and for design the order of magnitude is usually what matters.
  • Not right: “Who can release jobs, and at which printers, and what if the printer is busy?”
    This asks three things at once and usually gets an answer to the first part only. Ask one thing per question.

Lesson: From vague statements to clarification questions: phrase each question well

Question 2 of 25FactWhich of these is a measurable non-functional requirement?

Choose one answer.

Show the explained answers
  • Not right: “Releasing jobs must be fast.”
    “Fast” feels precise and is not. The question to ask is how long, measured from when to when, and how often it must be met.
  • Right: “From the moment the service receives a badge read until it hands the first job to the print server: at most 10 seconds for 95% of releases in a working day.”
    Right. It has all four parts: the quality (response time), the measure (seconds), the threshold (10 seconds for 95%), and the condition (measured over the part of the path the service controls, in a working day).
  • Not right: “Held jobs are stored in a std::vector.”
    This is a design decision, not a requirement. A requirement says what, not how.
  • Not right: “A user must be able to release their held jobs at any printer by badging in at it.”
    This is a functional requirement: it describes a behaviour that can be observed, not a quality such as speed, capacity, or durability.

Lesson: Functional and non-functional requirements: writing non-functional requirements

Question 3 of 25FactAssumption A8 says the print server reports each job’s page count correctly. It is probably true, but if it is false, the quota cannot be checked before printing and the quota design does not work at all. How should its risk be rated?

Choose one answer.

Show the explained answers
  • Not right: Low, because it is probably true
    Likelihood is only half of risk. An assumption that is probably true can still be high risk if being wrong is very costly.
  • Right: High, because risk combines how likely it is to be wrong and how much it costs if it is
    Right. The register rates A8 high and plans to check it, through the print server’s job data, before any quota design depends on it.
  • Not right: It needs no rating: it is a design decision, so it belongs in the design, not the register
    A design decision is a choice you control and can change, such as keeping held jobs in a std::vector. What the print server reports is a fact about an outside system that you do not control, so it is an assumption.

Lesson: Assumptions versus confirmed requirements: the assumption register

Question 4 of 25FactGiven: Kim has 20 pages left and holds a 30-page job. When: Kim badges in. Which “Then” makes the best acceptance criterion?

Choose one answer.

Show the explained answers
  • Not right: The job does not print.
    True, but incomplete. A good criterion also says what must not change. Refusals are where bugs hide: a refused job that is deleted, or a quota charged for nothing, would pass this check.
  • Right: It does not print; it is still held; Kim still has 20 pages; the panel says the quota is exceeded.
    Right. This is the lesson’s AC-2.4. It uses concrete values and the domain’s words, and it checks both the visible result and what must stay the same.
  • Not right: The held-jobs vector is not empty, and an error is returned.
    This is written in the code’s words, not the domain’s, and it is vague: which error, and is the quota unchanged? Criteria should say “held” and “pages left”, not “the vector is empty”.

Lesson: Use cases, user stories, and acceptance criteria: Given / When / Then

Question 5 of 25JudgementBadge reads from 12 printers, new jobs from the print server, and an expiry timer all change the same held jobs, at about five events a minute. Releasing a job means talking to a printer for seconds or minutes. Which designs can be defended?

Choose one answer. This is a judgement question: more than one answer can be defended.

Show the explained answers
  • Can be defended: One std::mutex guards the held jobs and quotas. A release marks the jobs as printing under the lock, talks to the printer outside it, and takes the lock again to record the result.
    Defensible: claim under the lock, call outside it, finish under the lock. The new “printing” state stops a second badge read from releasing the same job, and stops expiry from deleting it half-way.
  • Can be defended: Events go into a queue, and one worker thread applies them one at a time, so the domain classes need no locks.
    Defensible: this is the lesson’s other common choice, and the domain classes need no locks at all. The lesson spells out the slow-call problem for the lock, but it applies here too: if the one worker waits on a printer, every other event waits behind it, so the printer call still belongs outside the step that changes the state.
  • Hard to defend: One std::mutex, held for the whole release, including the call to the printer.
    Every other printer would wait: a user at 3-West could not release while a 200-page job prints at 2-East.
  • Hard to defend: No lock: at five events a minute, two events almost never happen at once.
    Two threads changing the same state with no protection is a data race, which is undefined behaviour in C++ however rarely it happens. The lesson’s point is that two people can badge in at the same moment.

The lock and the single owning thread both work at this scale; the lesson says so. In either design, what matters more is that no other event has to wait behind a slow call to a printer.

Lesson: Quality constraints: concurrency

Question 6 of 25FactA printer jams after printing 4 pages of a 10-page job. The pages were charged to the owner’s quota when the job was released. How does the lesson handle this failure?

Choose one answer.

Show the explained answers
  • Right: Keep pages 5 to 10 held, refund them, stop this release, and tell the user to try another printer
    Right. This is failure F-2: part of the work succeeded, so the rest is kept for later, and the panel tells the user what happened and what to do.
  • Not right: Retry printing on the same printer until it works
    Retrying fits a failure that is likely to be brief, when repeating is safe. A jammed printer needs a person, and the user can move to another printer.
  • Not right: Refuse the release and change nothing
    Refusing fits when going on would break an invariant and nothing has been done yet. Here four pages have already printed, so “change nothing” is no longer possible.
  • Not right: Accept and record the failure, and delete the job
    Accept and record is for failures the system cannot detect or prevent. This one is detected, and deleting the job would lose the pages not yet printed.

Lesson: Invariants and failure scenarios: choosing how to handle a failure

Question 7 of 25FactAsked “If quotas were missing on launch day, would we still launch?”, the facilities manager says yes, because uncollected printouts are the bigger problem. In MoSCoW, where does the quota requirement go?

Choose one answer.

Show the explained answers
  • Not right: Must
    The test for “must” is strict: if the answer to the launch-day question is yes, it is not a must, even though quotas were in the original request.
  • Right: Should
    Right. Quotas are important and expected, because cost is one of the goals, but the service is still useful for a while without them.
  • Not right: Won’t (this time)
    “Won’t” means agreed to be out of this release. Nobody agreed to drop quotas; they are only not needed for the launch.

Lesson: Prioritization, conflicts, and changing requirements: MoSCoW

Question 8 of 25FactReading the traceability table backwards, you find a public function with no requirement, flow, criterion, assumption, or constraint behind it. What does the lesson say to do?

Choose one answer.

Show the explained answers
  • Not right: Delete it at once: code with no justification is always a mistake
    The lesson says such code is a question, not automatically a mistake. It might implement a requirement nobody wrote down.
  • Not right: Keep it, because it seemed useful when it was written
    The backward trace accepts requirements, flows, criteria, assumptions, and constraints, but not “it seemed useful”. Unrequested code must still be tested, maintained, and secured.
  • Right: Treat it as a question: add the requirement if nobody wrote it down, or remove the function (or ask) if nobody asked for it
    Right. Either way, it should not stay unexplained.

Lesson: Traceability: reading it backwards

Question 9 of 25FactThe confirmed answer is a page quota per person. A teammate proposes a PageBudget interface with a per-person and a per-team implementation, in case the answer changes. When does the lesson say such an interface earns its place?

Choose one answer.

Show the explained answers
  • Right: Only when the program must support both answers at once, for example when some departments share a budget and others do not
    Right. With only one answer in use, the interface is speculation. Build for the answer you have, keep it contained, and change it when the other answer arrives.
  • Not right: Always, because it makes the later change free
    It does not. It fixes a shape before the team design’s own questions are answered, and that design’s real differences (an outside lookup, a new failure outcome) do not fit inside try_charge anyway.
  • Not right: Never: build the per-team design instead, because it is more flexible
    Choosing a design because it is “more flexible” silently settles a stakeholder question. Facilities owns the answer, and they said per person.

Lesson: How different answers change the design: should the choice be hidden behind an interface?

Question 10 of 25FactA Timesheet stores the minutes each person worked on each day, and refuses a day longer than 24 hours. Payroll, HR, and the scheduling team all read it. Does it break the Single Responsibility Principle?

Choose one answer.

Show the explained answers
  • Not right: Yes: three actors use it, so it has three reasons to change
    A class may serve many actors if it holds only facts. A fact is true for everyone, and when it changes, every reader should see the new value.
  • Right: No: it holds facts, and the 24-hour limit is a fact about time, not a rule an actor can renegotiate
    Right. The trouble starts only when one actor’s rule, such as “Sunday minutes count as overtime”, hides inside a class that others share.
  • Not right: Yes: the 24-hour check is a business rule, so it must move to the class of whichever actor asked for it
    The lesson names this check as an invariant that is a fact about time. No actor decided it, and no actor can change it.

Lesson: The Single Responsibility Principle: shared facts are safe; shared rules are not

Question 11 of 25FactPlaylist::add adds any track whose length is under one hour, its precondition. Which of these, in a derived class’s add, break the Liskov Substitution Principle? Choose all that apply.

Choose all that apply.

Show the explained answers
  • Right: It works only if the playlist is editable, and does nothing otherwise
    Right. It asks for more before the call (“only if editable”) and promises less after it (it may do nothing). Code written against Playlist would silently stop working.
  • Right: It throws a kind of error that Playlist::add never reports
    Right. The postcondition includes which errors a call may report. A new kind of error is a weaker promise, and callers were not written to handle it.
  • Not right: It also accepts tracks of an hour or longer
    A derived class may ask for the same, or less, before a call. Every call that was valid for the base is still valid.
  • Not right: It also records when each track was added, for a feature only the derived class offers
    Doing more is allowed: the derived class still keeps every promise of the base class, and may add promises and invariants of its own.

Lesson: The Liskov Substitution Principle: the principle

Question 12 of 25FactA shop has three discounts: for members (a marketing decision), for students (a partnership with a university), and for staff (an HR benefit). Each is “10% off” today, written in three places. Should they be merged into one function?

Choose one answer.

Show the explained answers
  • Not right: Yes: there are three copies, which meets the rule of three
    Counting copies is only a heuristic. In the lesson’s story there were three copies, and they still should not have been merged. The ownership test decides: would they change together, for the same reason?
  • Right: No: they are different rules with different owners that happen to match for now
    Right. This is coincidental similarity. Merging it gives a wrong abstraction that couples rules which should change separately, and is often worse than the duplication it removed.
  • Not right: Yes, and add a parameter for each difference that appears later
    That is exactly how the lesson’s wrong abstraction grew: five parameters, a bool flag, and a sentinel value, belonging to nobody, so each change risked the other two rules.

Lesson: DRY, KISS, and YAGNI: the wrong abstraction

Question 13 of 25FactInside Dispatcher::dispatch(Order& order), which of these break the Law of Demeter? Choose all that apply.

Choose all that apply.

Show the explained answers
  • Right: order.customer().membership().add_points(20);
    Right. It changes the state of an object two steps away, and copies a rule (how points are added) that belongs to that object.
  • Right: The same call split into named reference variables, one step per line
    Right. Fewer dots per line, but it knows exactly the same things about the structure of three other classes. Count the knowledge, not the dots.
  • Not right: order.delivery_address().postcode, where Address is a plain value with public fields and no rules
    Reading the fields of a plain value is reading data that was handed to you, not reaching into an object. If Address gains rules of its own, this changes.
  • Not right: A fluent builder chain such as Request::builder().url(page).timeout(seconds).build()
    Every call returns the builder itself, so the code talks to one object the whole time. Its interface is meant to be used this way.

Lesson: The Law of Demeter: when the law does not apply

Question 14 of 25FactOrderService takes a kv::Store& in its constructor. kv::Store is the concrete database client, declared and owned by the database module. What does this design have?

Choose one answer.

Show the explained answers
  • Right: Dependency injection without dependency inversion
    Right. The collaborator arrives through the constructor, which is injection. But the orders module depends on a type the database module owns, so the source dependency still points from the rules to the detail.
  • Not right: Dependency inversion without dependency injection
    Inversion means the high-level module owns the interface, such as OrderRepository in the orders module, and the detail implements it. Here the database module owns the type.
  • Not right: Both
    The lesson’s final design has both: the rules own the interface, and main injects an adapter. This first version has only the injection.
  • Not right: Neither
    Passing a collaborator in through a constructor parameter is dependency injection.

Lesson: The Dependency Inversion Principle: inversion is not injection

Question 15 of 25JudgementA ReminderService decides whether a reminder is due by reading the current time. The tests must check what happens at 08:59 and at 09:00, without waiting. Which designs can be defended?

Choose one answer. This is a judgement question: more than one answer can be defended.

Show the explained answers
  • Can be defended: A small Clock interface passed to the constructor: the real clock in the program, a manual clock in the tests
    Defensible. Code that reads the clock is hard to test unless the test can supply its own, so this is a real variation point with two implementations: the real one and the test one.
  • Can be defended: Pass the current time in as a parameter of the function that decides
    Defensible, and the lesson notes that passing the value in is sometimes simpler still. The test just passes 08:59 or 09:00.
  • Hard to defend: Read the system clock directly, and run the tests at the right moment
    The tests would depend on when they run, and could not check both times without waiting. The time is a dependency the test needs to replace.
  • Hard to defend: An IReminderService interface in front of ReminderService
    An interface with one implementation and no test that needs a substitute only costs. It also does nothing about the time, which is what the tests need to control.

Both of the first two options let a test control the time. Passing the time in is simpler when only a few calls need the time; a clock interface suits code that reads the time in many places.

Lesson: Composition over inheritance, and programming to interfaces: interfaces with one implementation

Question 16 of 25FactIn which of these situations is a plain switch a better choice than an extension point (an interface with one class per variant)? Choose all that apply.

Choose all that apply.

Show the explained answers
  • Right: A shop with one tax region and no history of changes to its rules
    Right. With one or two variants and no history of change, a switch is clearer. The lesson shows four extension points written too early for this shop, each with one implementation.
  • Right: The four suits of a deck of playing cards
    Right. The variants are closed by nature, and a switch that the compiler checks is safer than an open set.
  • Right: A one-off migration script
    Right. Throwaway or short-lived code will not get a second variant.
  • Not right: Regions are added every few months, each with rules of its own, and each one has meant editing the same function
    This is the change that keeps coming. Once a new kind of change has actually arrived, opening the code to extension, with tests pinning the existing variants, is the step the lesson takes.

Lesson: The Open/Closed Principle: when not to apply it

Question 17 of 25FactA firmware updater is the only client of three interfaces, Named, VersionReporter, and FirmwareInstaller, with one function each. It always needs all three from the same device. What does the lesson recommend?

Choose one answer.

Show the explained answers
  • Not right: Keep the three interfaces, and give the updater three parameters
    The three parameters must refer to the same device, and nothing checks that. Interfaces split by function, rather than by client, say nothing about who uses what.
  • Right: Merge them into one role for the updater
    Right. If every client that uses one interface also uses the other, from the same object, they are one role. ISP splits by client, not by function.
  • Not right: Merge every device function, including the sensor readings, into one interface
    That goes back to the problem ISP solves: clients that read sensors and the updater need different things, so they should not depend on one large interface.

Lesson: The Interface Segregation Principle: too far, one interface per function

Question 18 of 25JudgementChecking out works out a receipt. The receipt printer is the only party interested, it wants the receipt straight away, and the code that calls check_out is the code that prints. How should the receipt reach the printer?

Choose one answer. This is a judgement question: the answer depends on the trade-offs stated.

Show the explained answers
  • Can be defended: check_out returns the Receipt, and the caller prints it
    Defensible, and no pattern is needed. A return value carries the result with no borrowed pointers, no subscription, and no order of destruction to get right. A test calls check_out and compares the receipt it returns.
  • Hard to defend: An Observer: the printer subscribes to the checkout
    With one listener that wants the result now, the design must still answer Observer’s questions: the printer must outlive its subscription, and nobody may subscribe during check_out. Observer earns its place when several parts the subject must not know about react, and that list changes.
  • Hard to defend: check_out calls a global printer object itself
    That hides a dependency: nothing in check_out’s signature says it prints, and a test cannot check the receipt without replacing a global. (The Singleton lesson explains why a global that tests want to replace should be passed in.) The caller already has the printer, so returning the value is simpler.

Lesson: Pattern, or a simple facility: Observer with one listener, or a returned value

Question 19 of 25JudgementA product search has a text, an optional maximum price, an “in stock only” flag, and a limit. Every member is independent and has a sensible default, and each query goes straight into one search function, which checks it. How should callers make a query?

Choose one answer. This is a judgement question: the answer depends on the trade-offs stated.

Show the explained answers
  • Can be defended: A struct whose members all have defaults, set with designated initializers, with std::optional<int> for the maximum price
    Defensible, and no pattern is needed. The struct is shorter, can be compared with ==, and can be copied. It has no invariant, so a bad limit is found only when the query is used, which is fine here because search checks every query.
  • Hard to defend: A SearchQueryBuilder with one setter per member and a checking build()
    A Builder earns its place when the object must be valid from the moment it exists and is stored for a long time, or when construction has steps. None of that applies to plain settings passed straight to one function.
  • Hard to defend: A constructor with four parameters, where -1 means “no maximum price”
    A magic value such as -1 must be decoded by every reader. std::optional<int> says “there may be no maximum” directly, and designated initializers name each member that is set.

Lesson: Pattern, or a simple facility: Builder, or a struct with defaults

Question 20 of 25JudgementA report exporter always writes a header, then the rows, then a footer, in that order. Only the row format varies: CSV or fixed-width columns. The format is chosen when the exporter is made and never changes. Which designs can be defended?

Choose one answer. This is a judgement question: more than one answer can be defended.

Show the explained answers
  • Can be defended: Template Method: a non-virtual write fixes the order and calls a virtual step that formats one row
    Defensible. Template Method’s real strength is the non-virtual function that fixes the order, and here only one step varies.
  • Can be defended: A Strategy for the row format, passed to the constructor and never replaced
    Defensible. A Strategy that never changes binds the format per object, and each format can be tested on its own.
  • Hard to defend: State: the exporter moves between formats as rows are written
    State is for an object that moves through stages because of what happens to it. Here the format cannot change while the exporter lives.

The first two options both bind the format once. Template Method is the shortest when the fixed order must be enforced and the variants are few and stable; prefer a Strategy if the formats should be tested alone, or may later combine with another thing that varies.

Lesson: Strategy vs State vs Template Method: choosing

Question 21 of 25FactA storage account moves from trial to active to overdue because of its own events. Someone suggests treating the stage as a Strategy, with a public set_stage. Which problems does the lesson name? Choose all that apply.

Choose all that apply.

Show the explained answers
  • Right: Every caller must know the rules for when the stage changes
    Right. The code that closes a month would have to count free months, and the payment code would have to set “overdue”, so the rules are spread across the program.
  • Right: Callers can make impossible moves, such as setting “overdue” during a trial
    Right. With State, move_to is private: only the account’s own events, through the stages, may change the stage.
  • Right: The trial stage’s count of free months has no home
    Right. In a strategy chosen from outside, the caller would have to keep the stage’s own data.
  • Not right: A Strategy’s virtual calls are too slow for billing
    Speed is not the issue: the two designs have the same shape, with an interface, a few implementations, and a std::unique_ptr in the account. The difference is who may change the stage.

Lesson: Strategy vs State vs Template Method: why the stage is not a Strategy

Question 22 of 25FactWhich of these can be acceptable as a single global object? Choose all that apply.

Choose all that apply.

Show the explained answers
  • Right: A table of constants, computed once and returned from a function-local static const
    Right. It is immutable and does not depend on configuration, so there is no state to share and no reason for a test to replace it.
  • Right: A process-wide output stream such as std::cout
    Right. It is a genuine process-wide resource, and writing to it does not change results. (Tests that check log messages will want to replace a log sink, though.)
  • Not right: The shop’s current settings, which some tests need to set differently
    The lesson’s test: would any test, now or later, want a different one? Here the answer is yes, so pass it in.
  • Not right: A hardware device that exists once, reached as a global by every class that uses it
    Even when the platform makes it one per process, the lesson says to wrap it in an object that main creates and pass that, so code that uses it can be tested with a fake.

Lesson: Singleton: when a single global object is acceptable

Question 23 of 25JudgementA wrapper around a PriceService has the same interface, and remembers each answer for five minutes, so a caller may see a price up to five minutes old. Which name can be defended?

Choose one answer. This is a judgement question: more than one answer can be defended.

Show the explained answers
  • Can be defended: A caching proxy, because it decides whether the real service is asked at all
    Defensible, and the usual name. Deciding whether the real object is reached is an access decision, which is what a proxy is for.
  • Can be defended: A decorator, because callers can notice the stale answers, so it changes results
    Defensible. A proxy gives the same answers; a cache that can return stale ones changes what callers see, like a decorator.
  • Hard to defend: An adapter
    An adapter offers a different interface from the object it wraps. This wrapper has the same interface, so it can be used wherever the price service is used.

The lesson says that teams name this case differently, and that is fine as long as the code says what it does. Whatever it is called, document how stale an answer can be.

Lesson: Decorator vs Proxy: the cases that blur

Question 24 of 25FactA feed keeps pointers to its listeners in a std::vector. During publish, one listener calls detach on itself, which erases it from the vector that publish’s for loop is walking. What does the lesson say about this?

Choose one answer.

Show the explained answers
  • Right: It is undefined behaviour, and it can print wrong output with no crash, while plain AddressSanitizer reports nothing
    Right. The erase invalidates the loop’s iterators. When the lesson ran it, one listener was told twice and the other not at all, and only a build with the library’s vector checks turned on reported a problem.
  • Not right: It is safe, because erasing from a vector keeps a running range-for loop valid
    erase invalidates iterators at and after the erased element, including the ones the range-for loop is using.
  • Not right: It always crashes, so the first test run finds it
    It did not crash when the lesson ran it. Wrong output, no crash, and most tools silent is what makes it the worst kind of bug.

Lesson: Observer: the naive version, and its two lifetime bugs

Question 25 of 25FactA program uses one exporter, and the choice is made in exactly one place, main. A teammate proposes an ExporterFactory. What does the lesson recommend?

Choose one answer.

Show the explained answers
  • Right: No factory: write the std::make_unique in main, and pass the object to the code that uses it
    Right, and no pattern is needed. When the code knows the exact class it needs, there is nothing for a factory to hide.
  • Not right: An Abstract Factory, so that exporters from other families can be added later
    An Abstract Factory is for when the caller does not know the class and several objects must come from the same family, such as a button and a checkbox that must match.
  • Not right: A Builder for the exporter
    A Builder is for objects with many optional parts, parts that accumulate, or rules that involve several parts at once. Nothing here says the exporter is hard to construct.

Lesson: Factory Method vs Abstract Factory vs Builder: choosing