Difficulty: Easy
Correct Answer: NSArray is immutable while NSMutableArray is mutable and allows adding, removing, and reordering elements.
Explanation:
Introduction / Context:
Collections are central to Cocoa and Cocoa Touch development. Objective C developers frequently use NSArray and NSMutableArray to store ordered lists of objects. This question checks whether you understand the key difference between these two classes and the implications for modifying collection contents at runtime.
Given Data / Assumptions:
Concept / Approach:
NSArray represents an immutable array. Once it is created, you cannot add, remove, or reorder elements. Any attempt to call methods that would modify contents produces either a compile time warning or a runtime exception. NSMutableArray is a subclass of NSArray that adds mutating methods, such as addObject, removeObjectAtIndex, insertObject, and sortUsingComparator. This class allows dynamic changes to the collection while preserving the same general interface for enumeration and indexing.
Step-by-Step Solution:
Step 1: Recall that immutable Foundation classes are usually prefixed with NS and have mutable subclasses, such as NSString versus NSMutableString.Step 2: Apply this pattern to arrays, where NSArray is immutable and NSMutableArray allows modification.Step 3: Recognize that both classes can hold heterogeneous Objective C objects and do not restrict types by default.Step 4: Understand that mutability is about whether the contents can change, not about where the object is allocated or which object types it stores.Step 5: Therefore the correct statement is option A, which states that NSArray is immutable and NSMutableArray is mutable.
Verification / Alternative check:
Apple documentation clearly defines NSArray as an immutable ordered collection and NSMutableArray as a dynamic version. Code examples show that you must use NSMutableArray to add or remove elements. The naming convention across Foundation classes reinforces this behavior, validating that option A accurately describes the distinction.
Why Other Options Are Wrong:
Option B is wrong because neither class restricts the element types to NSString or NSNumber; they can hold any Objective C object pointers. Option C is incorrect because performance depends on operations and implementation details, not a blanket rule that one class is always slower. Option D misstates memory allocation rules; both NSArray and NSMutableArray instances are normally allocated on the heap through standard Objective C allocation patterns.
Common Pitfalls:
A frequent mistake is to use NSArray when mutable behavior is required, leading to runtime errors when calling mutating methods. Another pitfall is unnecessary use of NSMutableArray where immutability would be safer and simpler. Choosing the appropriate class based on whether the collection should change helps improve code clarity and avoids bugs related to unintended modification.
Final Answer:
NSArray is immutable while NSMutableArray is mutable and allows adding, removing, and reordering elements.
Discussion & Comments