Self-Assessment Quiz: Procedures

Twenty questions to confirm you can build and call procedures safely before the I/O of Chapter 7. Aim for 16 or more. Answers and a topic map are at the end — try the whole quiz first, and predict every "what prints" by hand.


Question 1

Which statement is true of a function but not a subroutine? - A. It is invoked with the call keyword - B. It returns a single value and is used inside an expression - C. It can modify its arguments - D. It cannot have an explicit interface

Question 2

You invoke a subroutine named solve with: - A. x = solve(a, b) - B. call solve(a, b, x) - C. use solve - D. solve x

Question 3

intent(in) on a dummy argument means the procedure may: - A. Read the argument but not modify it - B. Modify the argument but not read it - C. Neither read nor modify it - D. Reallocate it

Question 4

An intent(out) argument, on entry to the procedure, is: - A. Guaranteed to hold the caller's value - B. Set to zero - C. Undefined — the caller's previous value is not available - D. A copy that is discarded on return

Question 5

If you declare an argument intent(in) and then assign to it in the procedure body, the result is: - A. A run-time crash - B. A silent wrong answer - C. A compile-time error - D. Undefined behavior that sometimes works

Question 6

Inside a procedure, present(factor) returns .true. when: - A. factor is nonzero - B. factor was supplied by the caller - C. factor is intent(in) - D. factor has been allocated

Question 7

At a call site, once you pass one argument by keyword (name=value), the arguments that follow it must: - A. Also be passed by keyword - B. Be positional - C. Be optional - D. Be omitted

Question 8

Marking a procedure pure promises that it: - A. Runs faster than any other procedure - B. Has no side effects — no I/O, no modifying global state, only its declared outputs - C. Takes only scalar arguments - D. Cannot be called recursively

Question 9

An elemental function: - A. Must be declared recursive - B. Is written for scalar arguments but may be applied element-by-element to arrays; it is automatically pure - C. Can only operate on arrays - D. Modifies its arguments in place

Question 10

Which feature requires the procedure to have an explicit interface (i.e., be internal or in a module)? - A. Optional arguments - B. Keyword arguments - C. Assumed-shape array arguments - D. All of the above

Question 11

The modern, preferred way to declare a 2-D array dummy argument whose shape comes from the caller is: - A. real(dp) :: a(n, n) - B. real(dp) :: a(:,:) - C. real(dp) :: a(*) - D. real(dp) :: a(100, 100)

Question 12

The legacy assumed-size array declaration real :: a(*) is dangerous because: - A. It uses too much memory - B. The procedure cannot know the array's true extent — size and whole-array operations do not work, and bounds checking is defeated - C. It is not valid Fortran - D. It forces single precision

Question 13

What does this print? print '(f6.2)', twice(triple(2.0_dp)), where triple(x) = 3*x and twice(x) = 2*x. - A. 6.00 - B. 12.00 - C. 10.00 - D. 5.00

Question 14

A recursive function must use a result clause because: - A. Recursion is slower otherwise - B. Without it, the function's own name in the body is ambiguous between "the return value" and "call myself again" - C. The standard forbids naming recursive functions - D. result allocates the stack

Question 15

True or false, with one sentence: "An internal procedure (after contains) can access the variables of its host program unit without them being passed as arguments."

Question 16

True or false, with one sentence: "A Fortran function can return several values at once, like a Python tuple."

Question 17

Given relax(x_new, x_old, goal, factor) with factor optional and defaulted to 1.0 via present, what is a after call relax(a, 100.0_dp, 0.0_dp) (factor omitted)? - A. 100.00 - B. 50.00 - C. 0.00 - D. Undefined

Question 18

"Almost always the right choice" for passing arrays to procedures in modern Fortran is: - A. Explicit-shape, passing the sizes as extra arguments - B. Assumed-size (a(*)) - C. Assumed-shape (a(:,:)) - D. A COMMON block

Question 19

Internal procedures are written: - A. Before the first executable statement - B. After a contains statement inside a program, module, or procedure - C. In a separate file with no interface - D. Inside a COMMON block

Question 20

sigmoid is elemental and returns 1/(1+exp(-x)). What is the shape of sigmoid([-1.0_dp, 0.0_dp, 1.0_dp])? - A. A scalar - B. A 3-element array (the function applied element-by-element) - C. A compile error (elemental functions reject arrays) - D. A 1-element array


Answer Key

Q Ans Why
1 B A function returns one value and appears in an expression; a subroutine is called.
2 B Subroutines are invoked with call, results flowing through arguments.
3 A intent(in) = read-only.
4 C An intent(out) argument arrives undefined; the incoming value is gone.
5 C The compiler enforces intent(in) — assigning to it is a compile-time error.
6 B present tests whether the caller supplied the optional argument.
7 A Positional arguments must precede keyword ones; after a keyword, all must be keyword.
8 B pure = no side effects; all function args intent(in).
9 B elemental is scalar-written, array-applicable, and implies pure.
10 D Optional, keyword, and assumed-shape all need an explicit interface.
11 B a(:,:) is assumed-shape — the shape travels with the argument.
12 B Assumed-size hides the true extent; size/array ops/bounds-checking fail.
13 B triple(2)=6, twice(6)=12f6.2 prints 12.00.
14 B result disambiguates the return value from a recursive self-call.
15 True That is host association — internal procedures see the host's variables.
16 False A function returns exactly one object; multiple outputs use subroutine arguments.
17 C factor absent → f = 1.0; x_new = 100 + 1*(0-100) = 0.00.
18 C Assumed-shape is the safe, self-describing modern default.
19 B Internal procedures live after contains.
20 B Elemental applied to a 3-element array yields a 3-element array.

Topics to review by question

  • Q1–2 → §6.1 (subroutine vs function).
  • Q3–5, Q17 → §6.2 (intent and its enforcement).
  • Q6–7, Q17 → §6.3 (optional and keyword arguments).
  • Q8–9, Q20 → §6.4 (pure and elemental).
  • Q10, Q14–15, Q19 → §6.5 (internal procedures, explicit interfaces, recursion).
  • Q11–12, Q18 → §6.6 (assumed-shape vs explicit-shape vs assumed-size).
  • Q13, Q16 → §6.1 (functions in expressions; single return value).

Scored below 16? The likeliest gaps are intent (Q3–5) and the array-argument kinds (Q11–12, Q18) — reread §6.2 and §6.6, because every later chapter leans on both.