Self-Assessment Quiz: Derived Types

Twenty questions to confirm you can design with types, not just read them. Aim for 16 or more. A few ask "what does this print?" — trace them by hand. Answers and a topic map are at the end; try the whole quiz first.


Question 1

A derived type is best described as: - A. A built-in Fortran type like real or integer - B. A data type you define yourself by grouping other data under one type name - C. A synonym for an allocatable array - D. A procedure that returns multiple values

Question 2

Given type(point2d) :: p, the correct way to read its x component is: - A. p.x - B. p->x - C. p%x - D. x(p)

Question 3

Which declaration correctly declares a variable of a derived type named particle? - A. particle :: p - B. type particle :: p - C. type(particle) :: p - D. derived(particle) :: p

Question 4

In a type-bound procedure, the passed-object dummy argument must be declared: - A. type(...) - B. class(...) - C. pointer - D. allocatable

Question 5

What does nopass do to a type-bound procedure? - A. Forbids the procedure from being called - B. Makes the object be passed as the last argument - C. Prevents the object from being passed automatically at all - D. Makes the procedure pure

Question 6

The default rule for which dummy argument receives the object is: - A. The last argument - B. The first argument - C. Whichever is named self - D. There is no default; you must always use pass(name)

Question 7

For a derived type with an allocatable component, the statement b = a performs a: - A. Shallow copy (b and a share storage) - B. Deep copy (b gets independent storage holding a copy of the values) - C. Move that leaves a unallocated - D. Compile error

Question 8

Which is a genuine advantage of an allocatable component over a pointer component? - A. It can point to arbitrary existing memory - B. Assignment deep-copies and it is deallocated automatically - C. It supports linked lists more naturally - D. It is required for recursion

Question 9

A parameterized derived type's kind parameter is declared inside the type with: - A. integer, len :: - B. integer, kind :: - C. integer, parameter :: - D. integer, allocatable ::

Question 10

What does this print?

type :: r
  real :: a = 2.0, b = 3.0
end type r
type(r) :: x
print '(f4.1)', x%a + x%b
  • A. 0.0
  • B. 5.0
  • C. Garbage (uninitialized)
  • D. Compile error

Question 11

Which statement builds a vec3 value with the structure constructor? - A. vec3 = (1.0, 2.0, 3.0) - B. vec3(1.0_dp, 2.0_dp, 3.0_dp) - C. new vec3(1.0, 2.0, 3.0) - D. make_vec3(1.0, 2.0, 3.0)

Question 12

The percent operator can be chained for nested types. p%vel%z means: - A. The z of p, then its velocity - B. The z component of the vel component of p - C. An error — you may only use one % - D. The product of p, vel, and z

Question 13

You call a method as c%area(). Where does the object c go inside area? - A. It is discarded - B. It is passed automatically as the passed-object dummy (e.g. self) - C. It must be passed again explicitly - D. It becomes a global variable

Question 14

True or false, with the reason: "A type-bound procedure call like c%area() is inherently slower than the plain call circle_area(c) because of dynamic dispatch."

Question 15

Which is the honest state of parameterized derived types in current compilers? - A. Unsupported by every compiler - B. Fully standard, but the corner of modern Fortran where compiler support has most lagged - C. Non-standard extensions - D. Faster than allocatable components in all cases

Question 16

An allocatable component that you never allocate and then index into (e.g. b%v(1) = 1.0) causes: - A. Automatic allocation to size 1 - B. A run-time error (referencing an unallocated array) - C. A compile error every time - D. Silent success with value 0

Question 17

A user-defined constructor is created by: - A. Naming a function constructor - B. Declaring an interface with the same name as the type, mapping to a function that returns the type - C. Using the new keyword - D. Marking the type public

Question 18

What does this print?

type :: stat
  integer :: n = 0
  real :: s = 0.0
contains
  procedure :: add => stat_add
end type stat
! stat_add does: self%n = self%n + 1; self%s = self%s + x
type(stat) :: t
call t%add(4.0); call t%add(6.0)
print '(i0,1x,f4.1)', t%n, t%s
  • A. 0 0.0
  • B. 2 10.0
  • C. 1 6.0
  • D. 2 4.0

Question 19

For the heat solver's field_t, the temperature array u is declared as an allocatable component chiefly so that: - A. It can be a different size at run time and travels bundled with the grid metadata, deep-copied and auto-freed - B. It runs faster than a fixed-size array in all cases - C. It can alias another field's memory - D. It avoids needing implicit none

Question 20

Object orientation (extends, polymorphism, select type) — the subject of the next chapter — is built on top of: - A. Pointers and targets - B. Derived types and type-bound procedures - C. Coarrays - D. COMMON blocks


Answer Key

Q Ans Why
1 B A derived type groups other data under a new type name.
2 C Fortran uses % for component access (the dot was taken by .and. etc.).
3 C type(name) :: var — the type name goes inside type( ).
4 B The passed object must be polymorphic: class(...), never type(...).
5 C nopass means the object is not passed automatically.
6 B By default the passed object is the first dummy argument.
7 B Allocatable-component assignment deep-copies into independent storage.
8 B Deep copy on assignment plus automatic deallocation — the core wins.
9 B Kind parameters are declared integer, kind ::; lengths integer, len ::.
10 B Default initializers give a=2.0, b=3.0, so 2.0 + 3.0 = 5.0.
11 B The type name acts as the structure constructor.
12 B Chained %: the z of the vel of p.
13 B The object is handed in automatically as the passed-object dummy.
14 False With no polymorphism (this chapter), the compiler resolves and inlines it — same speed as circle_area(c). Dispatch cost appears only with true polymorphism (Ch. 10).
15 B PDTs are standard but the least uniformly supported modern feature.
16 B Indexing an unallocated allocatable is a run-time error.
17 B An interface named like the type overloads its constructor.
18 B Two add calls: n=2, s=4.0+6.0=10.0.
19 A Run-time sizing, bundled with metadata, deep-copied, auto-freed.
20 B OOP extends derived types and their type-bound procedures.

Topics to review by question

  • Q1–3, 10–12 → §9.1 (type definitions, components, %, nested types, structure constructor).
  • Q4–6, 13–14, 18 → §9.2 (type-bound procedures, the class passed object, pass/nopass, cost).
  • Q9, 15 → §9.3 (parameterized derived types and their compiler-support caveat).
  • Q7–8, 16–17, 19 → §9.4 (constructors, allocatable components, deep copy vs shallow).
  • Q20 → §9.5 and What's Next (derived types as the foundation of OOP).

Scored below 16? Reread the flagged sections. Questions 4, 7, and 8 are the load-bearing ideas of the chapter — if you missed any of those, revisit §9.2 and §9.4 before Chapter 10.