Chapter 9 Quiz: Arrays: Fixed Collections and Multidimensional Data

Test your understanding of array concepts, syntax, and algorithms. Answers are provided at the end.


Question 1. Which of the following best describes an array?

(a) A collection of variables of different types stored in sequence (b) A fixed-size collection of elements of the same type with indexed access (c) A data structure that can only be accessed sequentially (d) A variable-length list that automatically resizes


Question 2. Given the declaration var temps: array[1..365] of Real;, what is the valid range of indices?

(a) 0 to 364 (b) 0 to 365 (c) 1 to 365 (d) 1 to 366


Question 3. What is the output of the following code?

var a: array[1..4] of Integer = (10, 20, 30, 40);
begin
  WriteLn(a[2] + a[4]);
end.

(a) 30 (b) 50 (c) 60 (d) 70


Question 4. Which declaration uses a character-indexed array?

(a) var counts: array[1..26] of Integer; (b) var counts: array['A'..'Z'] of Integer; (c) var counts: array[Char] of Integer; (d) Both (b) and (c)


Question 5. What happens when you access an array element with an out-of-bounds index in Free Pascal with {$R+} enabled?

(a) The program silently reads garbage data (b) The compiler issues a warning but the program runs (c) A runtime error (range check error) occurs (d) The index wraps around to a valid position


Question 6. Which parameter passing mode should you use when a function needs to read an array but not modify it?

(a) var (b) const (c) No keyword (pass by value) (d) out


Question 7. In an open array parameter (array of Integer), what does Low(arr) always return?

(a) 1 (b) The first element's value (c) 0 (d) It depends on the caller's array


Question 8. Consider the following declarations:

type
  TArrayA = array[1..10] of Integer;
  TArrayB = array[1..10] of Integer;

Can a variable of type TArrayA be assigned to a variable of type TArrayB?

(a) Yes, because they have the same structure (b) Yes, but only if they have the same number of elements (c) No, because they are different named types (d) No, because Pascal does not support array assignment


Question 9. What does the following function return when called with data = [3, 7, 2, 9, 1]?

function Mystery(const arr: array of Integer): Integer;
var i, result: Integer;
begin
  result := arr[0];
  for i := 1 to High(arr) do
    if arr[i] > result then
      result := arr[i];
  Mystery := result;
end;

(a) 1 (b) 3 (c) 9 (d) 22


Question 10. What is the time complexity of linear search on an array of n elements in the worst case?

(a) O(1) (b) O(log n) (c) O(n) (d) O(n^2)


Question 11. What does the SetLength procedure do for a dynamic array?

(a) Returns the current length of the array (b) Sets the maximum possible length at compile time (c) Allocates or reallocates the array to a specified number of elements at runtime (d) Fixes the length so it cannot be changed again


Question 12. Dynamic arrays in Free Pascal are always indexed starting from:

(a) 1 (b) 0 (c) The value specified by the programmer (d) The value of Low


Question 13. What is the correct way to access element at row 3, column 5 of a 2D static array grid?

(a) grid(3, 5) (b) grid[3, 5] (c) grid{3, 5} (d) grid.3.5


Question 14. In row-major storage of a 2D array, which elements are stored consecutively in memory?

(a) Elements in the same column (b) Elements in the same row (c) Diagonal elements (d) Elements are stored randomly


Question 15. Why is calling SetLength(data, Length(data) + 1) inside a loop (adding one element at a time) inefficient for large datasets?

(a) SetLength cannot increase the length by 1 (b) Each call may copy the entire array, leading to O(n^2) total work (c) Dynamic arrays cannot be resized more than once (d) It causes memory leaks


Question 16. What is the purpose of defining a constant like MAX_STUDENTS = 30 and using it in an array declaration?

(a) It makes the array dynamic (b) It allows changing the array size by modifying one line (c) It is required by Pascal syntax (d) It makes the program run faster


Question 17. Given:

var
  matrix: array[1..3, 1..4] of Integer;

How many total elements does matrix contain?

(a) 3 (b) 4 (c) 7 (d) 12


Question 18. Which of the following is a limitation of arrays that records can address?

(a) Arrays cannot be passed to procedures (b) Arrays cannot store more than 100 elements (c) All elements in an array must be the same type (d) Arrays cannot be indexed by integers


Question 19. What is the output of this code?

var
  counts: array['a'..'e'] of Integer;
  ch: Char;
begin
  for ch := 'a' to 'e' do
    counts[ch] := Ord(ch) - Ord('a') + 1;
  WriteLn(counts['c']);
end.

(a) 1 (b) 2 (c) 3 (d) 99


Question 20. In the parallel arrays approach used in GradeBook Pro, what is the primary risk when sorting one of the arrays?

(a) The sorted array may exceed its bounds (b) The other parallel arrays become desynchronized (c) Pascal does not allow sorting arrays (d) The program will crash with a type mismatch


Answer Key

  1. (b) — An array is a fixed-size, homogeneous, indexed collection.

  2. (c) — The declaration array[1..365] means valid indices are 1 through 365.

  3. (c)a[2] is 20 and a[4] is 40; their sum is 60.

  4. (d) — Both (b) and (c) use character-based indices. (b) uses a subrange of characters; (c) uses the full Char type, which also works as an array index.

  5. (c) — With range checking enabled ({$R+}), an out-of-bounds access triggers a runtime error 201.

  6. (b)const prevents modification and may avoid copying. var allows modification. Pass by value copies the entire array, which is wasteful for read-only access.

  7. (c) — Open array parameters are always zero-indexed in Pascal, so Low always returns 0.

  8. (c) — Pascal uses name equivalence for type compatibility. Even though TArrayA and TArrayB have identical structures, they are different types and cannot be assigned to each other.

  9. (c) — The function finds the maximum value. It starts with 3, then updates to 7, then to 9. It returns 9.

  10. (c) — Linear search examines each element one at a time. In the worst case (target not found or at the end), it examines all n elements.

  11. (c)SetLength allocates or reallocates a dynamic array at runtime to hold the specified number of elements.

  12. (b) — Dynamic arrays in Free Pascal (and Delphi) always start at index 0.

  13. (b) — Pascal uses square brackets for array access: grid[3, 5].

  14. (b) — Row-major order stores all elements of the same row in consecutive memory locations.

  15. (b) — Each SetLength may allocate new memory and copy all existing elements. Growing by one each time results in 1+2+3+...+n = O(n^2) copy operations total.

  16. (b) — Using a named constant means the array size can be changed by modifying a single line, making the code more maintainable.

  17. (d) — 3 rows times 4 columns = 12 total elements.

  18. (c) — Arrays require all elements to be the same type. Records allow grouping different types together.

  19. (c)Ord('c') - Ord('a') + 1 = 99 - 97 + 1 = 3.

  20. (b) — Sorting one parallel array without applying the same reordering to the others causes the arrays to lose their correspondence. Index i in one array no longer matches index i in the others.