Base CS from zero
When a program fails: multiple-choice review
Six questions that cut across the whole unit. Each one is a small judgement call you make in front of a real failure — not a definition to recite, but a distinction to hold steady: condition versus mechanism, loud versus silent, reasoning versus guessing.
Confirm you can connect the four lessons into one picture: an error is a condition, an exception is the mechanism that unwinds the stack, the stack trace is the snapshot of that unwind, undefined behaviour is the dangerous silent case, and debugging is reasoning over a deterministic machine.
A program tries to open a file that does not exist. Which of these is the error, and which is the exception?
main calls a, a calls b, b raises an exception. Only main has a handler. How many frames does the runtime pop, and where does normal control flow resume?
A four-line stack trace reads (top to bottom): divide, getAge, loadUser, main. A new engineer fixes main first. Why is that the wrong instinct?
One program reads index 5 of a 3-element array and raises an 'index out of range' exception. Another reads the same out-of-range cell and returns whatever bits are there, with no error. Which is the more dangerous failure, and why?
In JavaScript, reading index 9 of a 3-element array gives undefined (not a crash), and then your code computes undefined + 1. What happens, and why does it resemble undefined behaviour even though each step is defined?
A program returns a wrong total. An engineer starts flipping comparisons and re-running until the number looks right. What is the principled objection, grounded in how the machine works?
The unit is one chain: an error is the condition, an exception is the mechanism that stops normal flow and unwinds the call stack frame by frame looking for a handler, and the stack trace is that stack frozen at the throw — read top-down, throw site first. Undefined behaviour is the dangerous case where no error is raised at all and a garbage value travels silently. And because the machine is deterministic, the response to any failure is reasoning, not guessing: form a testable hypothesis, narrow the range, read the trace, check state, then fix the step you have proven is wrong.