Self-Assessment Quiz: Error Handling and Debugging
Twenty questions to confirm you can guard, halt, debug, and defend. Aim for 16 or more. The answer key and a topic map are at the end — try the whole quiz first.
Question 1
On allocate(a(n), stat=s, errmsg=msg), the value of s after a successful allocation is:
- A. -1
- B. 0
- C. the size n
- D. processor-dependent and unknowable
Question 2
Without a stat= specifier, a failed allocate:
- A. sets the array to zeros
- B. aborts the program with a runtime error
- C. silently continues with an unallocated array
- D. returns a negative number you can test
Question 3
The difference between stop and error stop most relevant to a shell script or CI system is:
- A. error stop prints in red
- B. error stop produces a nonzero exit code (error termination); stop defaults to zero (success)
- C. stop is faster
- D. there is no difference
Question 4
A program ends with error stop 2. In a POSIX shell, echo $? afterward prints:
- A. 0
- B. 1
- C. 2
- D. nothing
Question 5
True or false, with justification: "The exact integer that stat= receives on an allocation failure is fixed
by the standard, so you may test it against a specific number."
Question 6
Which flag makes an out-of-bounds array access into a located runtime error instead of undefined behavior?
- A. -O3
- B. -fcheck=all (or -fcheck=bounds)
- C. -Wall only
- D. -ffast-math
Question 7
-ffpe-trap=invalid,zero,overflow causes the program to:
- A. round NaN to zero
- B. halt at the operation that raises the exception, instead of producing NaN/Inf silently
- C. disable floating-point arithmetic
- D. print every floating-point result
Question 8
Why should you not include underflow in a -ffpe-trap list?
- A. gfortran does not support it
- B. underflow occurs routinely in correct numerical code, so trapping it would halt healthy programs
- C. it would slow the program to a crawl
- D. underflow cannot be trapped
Question 9
For finding a memory leak or a read of freed memory, the right tool is:
- A. gdb
- B. valgrind (or -fsanitize=address)
- C. -Wall
- D. print statements
Question 10
In gdb, the command that pauses execution at a chosen source line is:
- A. run
- B. print
- C. break
- D. continue
Question 11
Fortran has no built-in assert, so an assertion is:
- A. impossible in Fortran
- B. a hand-written procedure that error stops when its condition is false
- C. the stop statement
- D. a comment
Question 12
The best description of an assertion's role (vs stat/iostat) is:
- A. it handles errors you expect from the outside world
- B. it checks a condition you believe is always true, to catch a bug in your own logic
- C. it replaces all I/O error handling
- D. it is only for parallel code
Question 13
integer :: count = 0 inside a subroutine. On the second call to that subroutine, count at entry is:
- A. 0 — reset every call
- B. whatever it was at the end of the first call — the initializer implies save
- C. undefined
- D. a compile error
Question 14
A local variable you never assign before reading it holds:
- A. zero, guaranteed
- B. whatever bits were in that memory — undefined behavior
- C. NaN, guaranteed
- D. the value from the previous run
Question 15
A default 32-bit integer computing 2000*2000*2000 gives a negative number because:
- A. of a compiler bug
- B. the product exceeds ~2.1 billion and overflows, wrapping to a negative value
- C. multiplication is undefined for large integers
- D. it should use -O0
Question 16
The portable way to compare two reals for "equality" is:
- A. a == b
- B. abs(a - b) < tol for a suitable tolerance
- C. a .eqv. b
- D. print them and compare by eye
Question 17
True or false: "Diagnostics (error messages) should be written to standard output so they appear with the results."
Question 18
Which build is appropriate for developing the solver?
- A. -O3 -march=native -flto
- B. -fcheck=all -g -O0 (plus -fbacktrace)
- C. -Ofast only
- D. no flags at all
Question 19
What does this print?
integer(int64) :: n
n = int(huge(0_int32), int64) + 1_int64
print '(i0)', n
- A.
-2147483648 - B.
2147483647 - C.
2147483648 - D.
0
Question 20
In a parallel (coarray) program, error stop:
- A. halts only the image that executes it
- B. halts every image immediately
- C. is not allowed
- D. behaves exactly like stop
Answer Key
| Q | Ans | Why |
|---|---|---|
| 1 | B | stat is 0 on success, nonzero on failure. |
| 2 | B | Unguarded allocation failure aborts the program. |
| 3 | B | error stop = nonzero exit code (failure); stop defaults to 0. |
| 4 | C | The stop code becomes the exit status: 2. |
| 5 | False | The failure value is processor-dependent; test only stat /= 0. |
| 6 | B | -fcheck=all/-fcheck=bounds adds the bounds check. |
| 7 | B | Trapping halts at the raising operation instead of producing NaN/Inf. |
| 8 | B | Underflow is routine in correct code; trapping it halts healthy programs. |
| 9 | B | valgrind (or AddressSanitizer) finds leaks and freed-memory reads. |
| 10 | C | break sets a breakpoint. |
| 11 | B | You write a procedure that error stops on a false condition. |
| 12 | B | Assertions guard "impossible" conditions — bugs in your own logic. |
| 13 | B | An initializer in a declaration confers an implicit save. |
| 14 | B | Uninitialized locals hold garbage — undefined behavior. |
| 15 | B | The product overflows 32-bit range and wraps negative. |
| 16 | B | Compare reals with a tolerance, never ==. |
| 17 | False | Diagnostics go to standard error (error_unit), not standard output. |
| 18 | B | Development wants checks and debug info; strip them for production. |
| 19 | C | 64-bit arithmetic does not overflow: 2147483648. |
| 20 | B | error stop halts all images immediately. |
Topics to review by question
- Q1–2 → §13.1 (
stat/errmsg, guardingallocate). - Q3–5, 20 → §13.2 (
stopvserror stop, exit codes, parallel semantics). - Q6–8 → §13.3 (
-fcheck,-ffpe-trap). - Q9–10 → §13.4 (gdb, valgrind).
- Q11–12, 17 → §13.5 (assertions, defensive programming,
error_unit). - Q13–16, 19 → §13.6 (implicit
save, uninitialized, overflow, precision). - Q18 → §13.3 and the Project Checkpoint (development vs production builds).
Scored below 16? Reread the sections flagged above. Error handling is the one Part II topic no track should skip — a solver that fails silently is worse than one that fails loudly.