Arrays in Java are static data structures, whereas a Python list or a Java ArrayList is dynamic.
(a) Define the term static data structure. [1]
(b) Compare dynamic and static data structures in terms of memory allocation, resizing and memory use. [3]
- (a) A data structure whose size (number of elements / memory allocated) is fixed when it is created / declared and cannot change while the program runs; accept 'size fixed at compile time'(1)
- (b) Memory allocation: a static structure's memory is allocated once, in advance (often as one contiguous block), whereas a dynamic structure's memory is allocated and released while the program runs (at run time) as elements are added or removed(1)
- (b) Resizing: a dynamic structure can grow or shrink during execution, whereas a static structure cannot change size(1)
- (b) Memory use: a static structure may waste memory when not full (or overflow when full), whereas a dynamic structure uses only (about) the memory it needs; accept: a dynamic structure may need extra memory for overhead such as references / spare capacity(1)
- Answer: (a) Size fixed when created. (b) Allocation: dynamic at run time vs static once in advance; resizing: dynamic can grow/shrink, static cannot; memory use: static may waste unused space, dynamic uses about what it needs (with some overhead).
- Examiner tip: 'Static' does not mean the values cannot change: the contents of an array can be updated, it is the size that is fixed.