Self-Assessment Quiz: Arrays
Twenty questions to confirm the chapter stuck before you move on to procedures. Aim for 16 or more. Answers and a topic map are at the end — do the whole quiz first, and for the "what does this print?" items, work them out by hand before checking.
Question 1
What is the rank of an array declared real(dp) :: a(3, 4, 5)?
- A. 3
- B. 12
- C. 60
- D. 5
Question 2
For a default array real(dp) :: v(10), the first element is:
- A. v(0)
- B. v(1)
- C. v(-1)
- D. v(10)
Question 3
What does size(a) return for integer :: a(3, 4)?
- A. 3
- B. 4
- C. 7
- D. 12
Question 4
The section a(:, 2) of a rank-2 array selects:
- A. Row 2
- B. Column 2
- C. The element a(2,2)
- D. The first two columns
Question 5
The section v(1:10:2) selects the elements at indices:
- A. 1, 2, 3, …, 10
- B. 1, 3, 5, 7, 9
- C. 2, 4, 6, 8, 10
- D. 1 and 10 only
Question 6
For two conformable arrays a and b, the expression a * b computes:
- A. The matrix product
- B. The dot product
- C. The elementwise product a(i)*b(i)
- D. A compile error
Question 7
Which computes the true matrix product of two rank-2 arrays?
- A. a * b
- B. matmul(a, b)
- C. dot_product(a, b)
- D. product(a, b)
Question 8
True or false, with justification: an array constructor like [1, 2, 3, 4] can be turned into a 2×2 array.
Question 9
What is dot_product([1, 1, 1], [2, 3, 4])?
- A. 24
- B. 9
- C. [2, 3, 4]
- D. 3
Question 10
For real(dp), allocatable :: a(:) that has been declared but not yet allocated, allocated(a) returns:
- A. .true.
- B. .false.
- C. 0
- D. It is a run-time error to ask
Question 11
When a local allocatable array goes out of scope at the end of a procedure, it is:
- A. Leaked until the program ends
- B. Automatically deallocated by the language
- C. Left dangling and unsafe to use
- D. Converted to a pointer
Question 12
Fortran stores a multidimensional array in memory in: - A. Row-major order (last index fastest) - B. Column-major order (first index fastest) - C. Whatever order the compiler picks - D. Random order
Question 13
To sweep a large a(n, n) fast, the inner loop should run over:
- A. The second index (columns)
- B. The first index (rows)
- C. Either — it makes no difference
- D. Neither; use a where instead
Question 14
True or false: NumPy's default array order matches Fortran's, so no conversion is ever needed when sharing arrays.
Question 15
What does print *, sum([2, 4, 6]) print (ignoring the leading blank)?
- A. 6
- B. 12
- C. [2, 4, 6]
- D. 48
Question 16
After integer :: m(2,2); m = reshape([1, 2, 3, 4], [2,2]) (no order argument), what is m(1, 2)?
- A. 2
- B. 3
- C. 4
- D. 1
Question 17
The construct where (x < 0.0_dp) x = 0.0_dp does what?
- A. Stops the program if any element is negative
- B. Sets every element that is currently negative to zero, leaving the rest alone
- C. Sets the whole array to zero
- D. Returns the count of negative elements
Question 18
count(v > 5) returns:
- A. The largest element greater than 5
- B. The number of elements greater than 5
- C. .true. if any element exceeds 5
- D. The positions of elements greater than 5
Question 19
transpose(A) for A = [[1, 2], [3, 4]] (row by row) is:
- A. [[1, 2], [3, 4]]
- B. [[4, 3], [2, 1]]
- C. [[1, 3], [2, 4]]
- D. [[2, 4], [1, 3]]
Question 20
What is maxloc([3, 9, 2, 9], dim=1)?
- A. 9
- B. 2
- C. 4
- D. [2, 4]
Answer Key
| Q | Ans | Why |
|---|---|---|
| 1 | A | Rank is the number of dimensions; a(3,4,5) has three. (Its size is 60.) |
| 2 | B | Default arrays are one-based; the first element is v(1). |
| 3 | D | size is the total element count: $3 \times 4 = 12$. |
| 4 | B | A colon takes the whole extent; a(:, 2) is all rows of column 2. |
| 5 | B | Triplet 1:10:2 is start 1, stop 10, stride 2 → 1, 3, 5, 7, 9. |
| 6 | C | * on arrays is elementwise, never a matrix or dot product. |
| 7 | B | matmul is the matrix product; * is elementwise. |
| 8 | True | reshape([1,2,3,4], [2,2]) builds a 2×2 array (fills column-major). |
| 9 | B | $1\cdot2 + 1\cdot3 + 1\cdot4 = 9$. |
| 10 | B | Declaration reserves the name/rank only; allocated is false until allocate. |
| 11 | B | Local allocatables auto-deallocate on scope exit — no leak. |
| 12 | B | Column-major: the first index varies fastest through memory. |
| 13 | B | Inner loop over the first index walks adjacent memory (cache-friendly). |
| 14 | False | NumPy defaults to row-major (C order); sharing needs order='F'. |
| 15 | B | sum([2,4,6]) is 12. |
| 16 | B | Column-major fill: (1,1)=1, (2,1)=2, (1,2)=3, (2,2)=4 → m(1,2)=3. |
| 17 | B | where applies the assignment only where the mask is true. |
| 18 | B | count tallies how many elements satisfy the mask (an integer). |
| 19 | C | Transpose swaps rows and columns: [[1,3],[2,4]]. |
| 20 | B | maxloc returns the position of the first maximum; 9 first appears at index 2. |
Topics to review by question
- Q1–3 → §5.1–5.2 (rank, shape, size, declaration, 1-based indexing).
- Q4–5 → §5.2 (array sections and subscript triplets).
- Q6–7, 9, 19 → §5.3–5.4 (elementwise vs
matmul/dot_product/transpose). - Q8, 16 → §5.3–5.4 (constructors and
reshape— and column-major fill order). - Q10–11 → §5.5 (allocatable arrays and automatic deallocation).
- Q12–14 → §5.6 (column-major layout and loop order).
- Q15, 18, 20 → §5.4 (reductions and locations).
- Q17 → §5.7 (
whereand masks).
Scored below 16? The likeliest gaps are the elementwise-vs-matmul distinction (Q6–7) and column-major
order (Q12–13, Q16) — reread §5.4 and §5.6, because both echo through every performance chapter later.