Skip to main content
Module 5 · Dijkstra's Algorithm

Lesson 6 · Testing and Swapping — Knowledge ChecksAnswer key

Correct answers are marked and the explanation follows each question.

  1. On a clear grid, why check the step count rather than the exact path?

    1. A.The exact path is impossible to compute
    2. B.There can be several shortest paths of the same length — Dijkstra may pick a different valid one
    3. C.Step count is easier to type
    4. D.The path is always the same

    Multiple routes can tie for shortest. What must match is the optimal length; the specific cells may legitimately differ between the two algorithms.

  2. In a for/else loop, when does the else block run?

    1. A.Every time, always
    2. B.Only if the loop finishes without hitting break
    3. C.Only if break is hit
    4. D.Never

    The else clause of a for loop runs only when the loop completes normally. A break skips it — perfect for 'passed unless we found a problem' checks.

  3. How many lines of the Navigator's own code change to use Dijkstra instead of Manhattan?

    1. A.Dozens — you rewrite drive_path
    2. B.Zero — only the import and constructor change; the Navigator code is untouched
    3. C.Every line that mentions a path
    4. D.You must build a new Navigator class

    The Navigator calls compute_path the same way regardless. Only the two setup lines (import + constructor) change — the whole point of a shared interface.

  4. The swap works because Dijkstra is 'better' than Manhattan. True or false?

    1. A.True — better classes are always swappable
    2. B.False — the swap works because they share the same interface, not because of quality
    3. C.True — Manhattan is broken
    4. D.False — you cannot swap classes in Python

    Swappability comes from interface compatibility (same method name, params, return type), not from which class is 'better.' A worse class with the same interface would swap just as cleanly.