Skip to main content

SF-0302 · Coding · Easy

Can you write a for loop to iterate a list of 10 accounts and set the Rating field to hot?

✓ Verified by Vikas Singhal · Last reviewed 5/17/2026 · Updated for Spring '26

A correct answer to this question is short. The signal an interviewer looks for is whether you bulkify — one query, loop to mutate, one DML — or whether you do a SOQL or DML inside the loop.

List<Account> accs = [SELECT Id, Rating FROM Account LIMIT 10];

for (Account a : accs) {
    a.Rating = 'Hot';
}

update accs;

Three lines of meaningful code:

  1. One SOQL query for the 10 records. The LIMIT 10 is explicit.
  2. One loop that mutates each record in-place — no SOQL, no DML inside.
  3. One DML statement outside the loop, updating the whole list.

Total: 1 SOQL, 1 DML. Under every governor limit by orders of magnitude.

Variations the interviewer may probe

”What if there are fewer than 10?”

The LIMIT 10 returns up to 10. If only 7 exist, you get 7 back. The loop and DML still work — no special case needed.

”What if there are no accounts at all?”

accs is an empty list. The loop runs zero times. update accs on an empty list is a no-op (no error). Belt-and-braces production code might add if (!accs.isEmpty()) update accs; to avoid the wasted DML statement — but it’s not a bug either way.

”Why not use a SOQL for-loop?”

You could:

for (Account a : [SELECT Id, Rating FROM Account LIMIT 10]) {
    a.Rating = 'Hot';
    update a;       // BAD — one DML per iteration
}

The first line is fine — SOQL for-loops stream 200 rows at a time and keep heap flat. The DML inside the loop is the bug: 10 DML statements when 1 will do. The right SOQL-for-loop pattern collects into a list and updates once at the end:

List<Account> toUpdate = new List<Account>();
for (Account a : [SELECT Id, Rating FROM Account LIMIT 10]) {
    a.Rating = 'Hot';
    toUpdate.add(a);
}
if (!toUpdate.isEmpty()) update toUpdate;

“What if the requirement is 10 specific accounts?”

Add a WHERE clause. The structure stays identical.

List<Account> accs = [
    SELECT Id, Rating FROM Account
    WHERE Industry = 'Technology' AND Annual_Revenue__c > 1000000
    ORDER BY Annual_Revenue__c DESC
    LIMIT 10
];

for (Account a : accs) {
    a.Rating = 'Hot';
}
update accs;

”Can I do it without the explicit loop?”

You can mutate fields and add to a list in a single SOQL for-loop traversal as above. You cannot do a LIST.map(...) in Apex — there are no functional helpers like JavaScript’s .map.

What NOT to write

// ANTIPATTERN 1 — DML inside loop
for (Integer i = 0; i < 10; i++) {
    Account a = [SELECT Id FROM Account LIMIT 1]; // 10 SOQL queries
    a.Rating = 'Hot';
    update a;                                      // 10 DML statements
}

// ANTIPATTERN 2 — Loop with index instead of for-each
for (Integer i = 0; i < accs.size(); i++) {
    accs[i].Rating = 'Hot';
}
// This works — but the for-each is idiomatic Apex and clearer.

// ANTIPATTERN 3 — Hardcoded ratings without checking
for (Account a : accs) {
    a.Rating = 'Hot';
    a.Other_Field__c = '...'; // unrelated change
}
update accs;
// Conflating multiple updates into one loop muddies intent and trigger logic.

A “complete” answer fit for production

public static void markTopTenAsHot() {
    List<Account> accs = [SELECT Id, Rating FROM Account LIMIT 10];
    if (accs.isEmpty()) return;

    for (Account a : accs) {
        a.Rating = 'Hot';
    }
    update accs;
}

This is the version you’d put in a service class. Static method, clear name, guard against empty results, idiomatic loop, single DML.

What interviewers are really looking for

A junior writes the three-line snippet. A mid-level developer writes the same snippet plus an explanation of why no SOQL or DML in the loop. A senior developer writes it as a static method on a service class with an empty-list guard and a note that the same pattern scales to thousands of records — meaning the bulkification habit is the actual interview signal, not the syntax of a for-loop.

Verified against: Apex Developer Guide — Loops, Bulk DML. Last reviewed 2026-05-17.