Self-Assessment Quiz: I/O and Formatted Output

Twenty questions to confirm you can predict formatted output, use files and namelists, and handle I/O errors before moving on to modules. Aim for 16 or more. Answers and a topic map are at the end — try the whole quiz first.


Question 1

In the edit descriptor f8.2, what do the 8 and the 2 mean? - A. 8 values, 2 columns - B. A field 8 characters wide, with 2 digits after the decimal point - C. 8 decimals, rounded to 2 - D. 8 bytes, 2-byte alignment

Question 2

What does print '(f5.2)', 123.4_dp produce? - A. 123.40 - B. 123.4 - C. ***** - D. 123.4

Question 3

The difference between e12.4 and es12.4 for the same value is: - A. es uses more decimals - B. es places the mantissa in $[1,10)$ (scientific form); e uses the 0.xxxx form - C. e is for integers, es for reals - D. There is no difference

Question 4

Which produces a single leading blank in its output? - A. print '(a)', 'hi' - B. print *, 'hi' - C. Both - D. Neither

Question 5

What does newunit=u do in an open statement? - A. Creates a new file - B. Asks the runtime for an unused unit number and returns it in u - C. Opens unit number u where u must be set first - D. Numbers the records

Question 6

Which status= on open requires the file to already exist and errors if it does not? - A. 'replace' - B. 'new' - C. 'old' - D. 'scratch'

Question 7

A namelist file sets only dt. Your group /config/ nx, dt has defaults for both. After the read, nx is: - A. Zero - B. Undefined - C. Its default value (unchanged) - D. The value of dt

Question 8

A namelist group in an input file opens and closes with: - A. { and } - B. BEGIN and END - C. &groupname and / - D. [groupname] and a blank line

Question 9

After a read, an iostat value that is positive means: - A. Success - B. End of file - C. An error occurred - D. End of record

Question 10

Why test end-of-file with iostat_end rather than -1? - A. -1 is slower - B. The exact end-of-file value is processor-defined; iostat_end is the portable name - C. -1 means an error, not end-of-file - D. iostat_end is shorter to type

Question 11

Compared with writing a real(dp) as text with f8.2, writing it unformatted (write(u) x): - A. Loses more precision - B. Preserves the exact bits (no conversion) and is faster in bulk - C. Is always human-readable - D. Requires a format string

Question 12

The difference between a traditional unformatted-sequential file and a access='stream' file is: - A. Stream files are text - B. Sequential files have record markers; stream files are a pure byte stream with none - C. Stream files cannot hold reals - D. There is no difference

Question 13

For a direct-access unformatted file, the units of recl are: - A. Always bytes, on every compiler - B. Always 4-byte words - C. Processor-dependent (bytes in gfortran, words on some others) - D. Always bits

Question 14

What does print '(a, i0)', 'x=', 42 print? - A. x= 42 - B. x=42 - C. x= 42 - D. x=00042

Question 15

Which inquire specifier tells you whether a file is present before you open it? - A. size= - B. opened= - C. exist= - D. number=

Question 16

What does print '(a, /, a)', 'one', 'two' print? - A. one two on one line - B. one and two on two separate lines - C. one/two - D. onetwo

Question 17

The x (as in 5x) edit descriptor: - A. Prints the letter x five times - B. Inserts five spaces and consumes no value from the list - C. Multiplies the next value by 5 - D. Repeats the next descriptor five times

Question 18

True or false: "A raw unformatted or stream binary file records the array's shape, type, and byte order, so any program can read it correctly."

Question 19

Reading the input line New York with read *, city (list-directed, city a character variable) puts what in city? - A. New York - B. New - C. New,York - D. An error

Question 20

namelist is best described as: - A. A way to list the units your program has open - B. A built-in key/value configuration format read and written by name - C. A container type like an array - D. A debugging tool for pointers


Answer Key

Q Ans Why
1 B w.d = field width . digits after the decimal point.
2 C 123.40 needs 6 characters; in a 5-wide field it overflows to asterisks.
3 B es is scientific form (mantissa 1–10); e uses the 0.xxxxE form.
4 B List-directed print * emits one leading blank; formatted print '(a)' does not.
5 B newunit returns an unused unit number — no hard-coding.
6 C 'old' requires prior existence; 'replace'/'new' create.
7 C A namelist read assigns only the names present in the file; the rest keep their values.
8 C &groupname opens the group, / closes it.
9 C Positive = error; zero = success; negative = end-of-file/record.
10 B The end-of-file value is processor-defined; iostat_end names it portably.
11 B Unformatted transfers exact bytes with no conversion — exact and fast.
12 B Sequential unformatted wraps records in markers; stream has none.
13 C recl units are processor-dependent (bytes in gfortran).
14 B a prints x=, i0 prints 42 in minimal width → x=42.
15 C exist= reports whether the file is present.
16 B / ends the record, so one and two land on separate lines.
17 B x is a control descriptor: it inserts blanks and consumes no value.
18 False Raw binary is not self-describing; you need HDF5/NetCDF (Ch. 25) for that.
19 B List-directed input stops at the first blank; read the whole line with '(a)'.
20 B namelist is Fortran's built-in name-matched config format.

Topics to review by question

  • Q1–3, Q16, Q17 → §7.1 (edit descriptors, the overflow rule, control descriptors).
  • Q4, Q19 → §7.2 (list-directed I/O and its trade-offs).
  • Q5, Q6, Q13, Q15 → §7.3 (files, open clauses, direct access, inquire).
  • Q7, Q8, Q14, Q20 → §7.4 (namelist) and §7.1 (the i0 in Q14).
  • Q11, Q12, Q18 → §7.5 (unformatted and stream; the not-self-describing caveat).
  • Q9, Q10 → §7.6 (iostat/iomsg and iostat_end).

Scored below 16? Reread the sections flagged above — especially any §7.1 miss, since predicting formatted output to the column is the skill the rest of this chapter and Chapter 12 build on.