Salesforce Platform Developer I (legacy) — Question 24
Given:
Map<ID, Account> accountMap = new Map>ID, Account> ([SELECT Id, Name FROM Account]);
What are three valid Apex loop structures for iterating through items in the collection? (Choose three.)
Answer options
- A. for (ID accountID : accountMap.keySet()) {ג€¦}
- B. for (Account accountRecord : accountMap.values()) {ג€¦}
- C. for (Integer i=0; I < accountMap.size(); i++) {ג€¦}
- D. for (ID accountID : accountMap) {ג€¦}
- E. for (Account accountRecord : accountMap.keySet()) {ג€¦}
Correct answer: A, B, C
Explanation
The correct answers A and B utilize valid methods for iterating over keys and values in the map. Option C is also valid as it allows iteration based on the size of the map. Options D and E are incorrect because D attempts to iterate directly over a map, which is not valid, and E incorrectly tries to use keySet for values.