Concept quiz: requirements, principles, and patterns
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.
Your answers are saved only in this browser’s local storage, so you can come back to them. Nothing is sent to a server, and nothing is shared between devices or browsers. “Reset this quiz” below removes them, and so does the reset on the “My progress” page.
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
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
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 astd::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
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
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
Show the explained answers
- Can be defended: One
std::mutexguards 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
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
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
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.
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 insidetry_chargeanyway. - 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?
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
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 againstPlaylistwould silently stop working. - Right: It throws a kind of error that
Playlist::addnever 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.
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, aboolflag, and a sentinel value, belonging to nobody, so each change risked the other two rules.
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, whereAddressis 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. IfAddressgains 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.
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 asOrderRepositoryin 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, andmaininjects 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
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
Show the explained answers
- Can be defended: A small
Clockinterface 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
IReminderServiceinterface in front ofReminderService
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
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, aswitchis 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 aswitchthat 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.
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
Show the explained answers
- Can be defended:
check_outreturns theReceipt, 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 callscheck_outand 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 duringcheck_out. Observer earns its place when several parts the subject must not know about react, and that list changes. - Hard to defend:
check_outcalls a global printer object itself
That hides a dependency: nothing incheck_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
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 becausesearchchecks every query. - Hard to defend: A
SearchQueryBuilderwith one setter per member and a checkingbuild()
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
-1means “no maximum price”
A magic value such as-1must 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
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.
Show the explained answers
- Can be defended: Template Method: a non-virtual
writefixes 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.
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_tois 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 astd::unique_ptrin the account. The difference is who may change the stage.
Lesson: Strategy vs State vs Template Method: why the stage is not a Strategy
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 thatmaincreates and pass that, so code that uses it can be tested with a fake.
Lesson: Singleton: when a single global object is acceptable
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.
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.
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
eraseinvalidates 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
Show the explained answers
- Right: No factory: write the
std::make_uniqueinmain, 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
Your results
This summary helps you decide what to reread. It is not a certification and does not measure professional skill: a quiz checks that you remember and can apply the lessons’ rules, not that you can design a system.
Reset
Removes your saved answers for this quiz from this browser.