Self-Assessment Quiz: Strings and Text Processing

Twenty questions to confirm you can measure, clean, search, and convert text before moving on to error handling. Aim for 16 or more. Answers and a topic map are at the end — try the whole quiz first, and work the "what does this print?" items by hand.


Question 1

Which declaration is a deferred-length string? - A. character(len=20) :: s - B. character(:), allocatable :: s - C. character(len=*), intent(in) :: s - D. character :: s(20)

Question 2

For character(len=6) :: c = 'hi', what are len(c) and len_trim(c)? - A. 6 and 6 - B. 2 and 2 - C. 6 and 2 - D. 2 and 6

Question 3

trim(s) removes: - A. Leading blanks only - B. Trailing blanks only - C. Both leading and trailing blanks - D. All blanks, everywhere in the string

Question 4

adjustl(s) for s = ' ab ' (length 6) produces: - A. 'ab' (length 2) - B. 'ab ' (length 6) - C. ' ab' (length 6) - D. ' ab' (length 4)

Question 5

index(s, sub) returns what when sub does not occur in s? - A. −1 - B. 0 - C. len(s) - D. A run-time error

Question 6

scan(string, set) returns the position of: - A. The first character of string that is in set - B. The first character of string that is not in set - C. The first character of set that is in string - D. The number of characters of string that are in set

Question 7

verify('2018', '0123456789') returns: - A. 4 - B. 1 - C. 0 - D. 10

Question 8

Compared with Python's s.find(sub), Fortran's index(s, sub) differs in two ways. They are: - A. Fortran is 0-based and returns −1 when absent - B. Fortran is 1-based and returns 0 when absent - C. Fortran is 1-based and returns −1 when absent - D. There is no difference

Question 9

If dir is character(len=20) holding 'output', then dir // '/x' is: - A. 'output/x' - B. 'output' followed by 14 blanks, then '/x' - C. A compiler error - D. '/xoutput'

Question 10

The substring s(3:5) of a string selects: - A. Characters 3 and 5 only - B. Characters 3, 4, and 5 (1-based, inclusive) - C. Characters 2, 3, and 4 (0-based) - D. The 3rd through 5th words

Question 11

An internal file is: - A. A temporary file in /tmp - B. A character variable used as the unit of a read or write - C. A binary file opened with form='unformatted' - D. A file internal to the compiler

Question 12

What does this print?

character(len=16) :: b
write(b, '(a, i5.5)') 'step', 27
print '(a)', trim(b)
  • A. step27
  • B. step 27
  • C. step00027
  • D. step*****

Question 13

write(fname, '(i4.4)') 12345 (a five-digit number in a four-wide field) produces: - A. 1234 (truncated) - B. 12345 (field widened automatically) - C. **** (field filled with asterisks) - D. 0000

Question 14

What does this print?

character(len=12) :: b
write(b, '(i0)') 255
print '(a)', '[' // trim(b) // ']'
  • A. [255]
  • B. [ 255]
  • C. [255 ]
  • D. [00000000255]

Question 15

Reading read(line, *) a, b where line = '10 20' and a, b are integers uses what as the separator? - A. Commas only - B. Blanks (and commas), via list-directed input - C. The = sign - D. Nothing — it reads 1020 into a

Question 16

True or false: assigning s = 'abc' to a character(:), allocatable :: s automatically allocates s to length 3.

Question 17

What does this print?

character(:), allocatable :: s
s = 'go'
s = s // s
print '(a, i0)', 'len = ', len(s)
  • A. len = 2
  • B. len = 4
  • C. len = 0
  • D. It does not compile

Question 18

In the chapter's tokenizer, verify(line(pos:), ' ') is used to find: - A. The end of the current token - B. The start of the next token (the first non-blank) - C. The total number of blanks - D. Whether the line is empty

Question 19

The dummy declaration character(len=*), intent(in) :: label means the argument's length is: - A. Fixed at some large number - B. Zero - C. Assumed from the actual argument the caller passes - D. Deferred and allocatable

Question 20

The single biggest thing deferred-length strings remove, versus the FORTRAN 77 fixed-length approach, is: - A. The need for the character type at all - B. The manual companion length counter you had to keep in sync - C. The ability to concatenate - D. Support for reading from files


Answer Key

Q Ans Why
1 B character(:), allocatable defers the length; it is set by allocation or assignment.
2 C len is the declared length (6); len_trim ignores the four trailing blanks (2).
3 B trim removes trailing blanks only; use trim(adjustl(s)) for both ends.
4 B adjustl moves leading blanks to the end, preserving length: 'ab '.
5 B index returns 0 (an impossible 1-based position) when the substring is absent.
6 A scan finds the first character of string that is a member of set.
7 C Every character of '2018' is a digit, so there is no non-member: verify = 0.
8 B Fortran index is 1-based and returns 0 when absent; Python find is 0-based, −1 absent.
9 B The fixed-length dir carries its 14 trailing blanks into the concatenation.
10 B Substrings are 1-based and inclusive: s(3:5) is characters 3, 4, 5.
11 B An internal file is a character variable used as an I/O unit.
12 C i5.5 zero-pads 27 to 00027; with 'step' that is step00027.
13 C The value needs five digits but the field is four wide, so it fills with *.
14 A i0 uses minimum width (255); trim leaves 255, bracketed as [255].
15 B List-directed (*) input separates values on blanks (and commas).
16 True Assignment auto-allocates a deferred-length string to the value's length (F2003+).
17 B s = 'go' (len 2), then s // s is 'gogo' (len 4), reallocated to fit.
18 B verify finds the first non-blank — where the next token begins.
19 C len=* is an assumed-length dummy: it takes the caller's length.
20 B Deferred-length strings carry their own length, killing the manual counter.

Topics to review by question

  • Q1–4, 16, 17, 19 → §12.1 (fixed vs deferred length; assumed-length dummies; growth by assignment).
  • Q5–8, 18 → §12.2 (the character intrinsics: index, scan, verify, and the Python contrast).
  • Q9–10 → §12.3 (concatenation and substrings; the trailing-blank trap).
  • Q11–15 → §12.4 (internal files; i6.6, i0, field overflow, list-directed reads).
  • Q20 → §12.6 (modern vs the FORTRAN 77 fixed-length approach).

Scored below 16? The likely culprits are scan/verify (they are duals — one finds a member, one finds a non-member) and the field-width rules for internal writes (i6.6 zero-pads; a too-narrow field prints *). Reread §12.2 and §12.4, then retry.