LearnFrenzy
Sign in Take a free test

Salesforce Apex interview · Set 7 of 7

Apex Tricky and Output-Based Questions

  • 6 follow-up questions
  • 4 key points
  • Code examples
Test yourself

Platform rules hidden in simple code

Tricky Apex questions test null handling, query assignment rules, transaction-wide static state, mixed DML and read-only trigger records. Explain the rule, then the safe pattern.

  • NullsSafe navigation and single-row queries.
  • TransactionsStatic state, mixed DML and tests.
  • TriggersRead-only records and bulk chunks.

Experienced Salesforce interviewers love questions that look simple but hide a platform rule. Each one below shows the code, the result and the rule — so you can explain it confidently rather than guess.

1. What happens? (null-safe navigation)

null, not an exception. The safe navigation operator ?. returns null when the left side is null. Without it, acc.Owner.Name on a null account throws System.NullPointerException.

Account acc = null;
String ownerName = acc?.Owner?.Name;
System.debug(ownerName);

2. What happens? (querying a single record that does not exist)

A System.QueryException: List has no rows for assignment to SObject. Assigning a query directly to a single SObject requires exactly one row. Query into a list and check isEmpty(), or use ?. with a list index safely.

Account a = [SELECT Id FROM Account WHERE Name = 'Does Not Exist'];   // throws

List<Account> rows = [SELECT Id FROM Account WHERE Name = 'Does Not Exist' LIMIT 1];
Account safe = rows.isEmpty() ? null : rows[0];

3. What is printed? (Set of Strings and case)

2. Set and Map keys of type String are case-sensitive in Apex, so “Pune” and “pune” are different keys. Normalise with toLowerCase() when comparing user input.

Set<String> cities = new Set<String>{ 'Pune', 'pune' };
System.debug(cities.size());

4. What is printed? (static variable across trigger chunks)

Static variables live for the whole transaction, shared by every chunk of a bulk operation. A static Boolean alreadyRan set in the first chunk makes the trigger skip records 201–400 in the same transaction — a classic bulk bug.

public class Guard { public static Boolean alreadyRan = false; }

trigger AccountTrigger on Account (after update) {
    if (Guard.alreadyRan) return;      // second chunk of 200 is silently skipped!
    Guard.alreadyRan = true;
    // ...
}

5. Why does this throw MIXED_DML_OPERATION?

The code updates a setup object (User) and a non-setup object (Account) in the same transaction. Separate them — move one to asynchronous Apex — or in tests wrap setup DML in System.runAs.

update new User(Id = UserInfo.getUserId(), Title = 'Manager');
insert new Account(Name = 'Acme');      // MIXED_DML_OPERATION

6. What happens? (modifying Trigger.new in an after trigger)

System.FinalException: Record is read-only. In after triggers the records in Trigger.new are locked. Set fields in a before trigger, or update copies created with the record Id.

trigger OppAfter on Opportunity (after insert) {
    for (Opportunity o : Trigger.new) {
        o.Description = 'Created';      // FinalException
    }
}

7. What is printed? (Integer division and Decimal)

2 then 2.5. Integer division truncates in Apex; use Decimal for fractional results and setScale for rounding money.

Integer a = 5, b = 2;
System.debug(a / b);
Decimal c = 5;
System.debug(c / 2);

8. Does a SOQL query in a test see records created in another test method?

No. Each test method runs in its own transaction that is rolled back afterwards; data from @TestSetup is recreated (rolled back to a snapshot) for each method, and data from one test method is never visible to another.


Remember

Key points

  • Query into lists

    Avoid “List has no rows” exceptions.

  • String keys are case-sensitive

    Normalise before comparing.

  • Statics last the transaction

    Across all trigger chunks.

  • Setup + non-setup DML

    Must be split across transactions.

Interview drill

Follow-up questions

What the interviewer asks after your first answer. Turn on practice mode, answer each one aloud, then reveal.

  1. 1 What does String.isBlank(' ') return compared with isEmpty? Basic

    isBlank returns true for null, empty and whitespace-only strings; isEmpty returns false for " " because it contains characters.

  2. 2 Is Apex pass-by-value or pass-by-reference? Intermediate

    Primitives are passed by value; objects (including SObjects and collections) pass a copy of the reference — you can change their fields, but reassigning the parameter does not affect the caller.

  3. 3 What happens if you call update on a list with duplicate Ids? Intermediate

    It fails with “Duplicate id in list”. De-duplicate with a Map<Id, SObject> before updating.

  4. 4 Can you use a SOQL query in a constructor of a Visualforce/LWC controller safely? Basic

    Yes, it counts against the same transaction limits; keep it bulk-safe and enforce user mode.

  5. 5 What happens to a DML in a method that later throws an uncaught exception? Intermediate

    The whole transaction rolls back, including earlier successful DML, unless you used Database methods with savepoints and handled the error.

  6. 6 What is Database.setSavepoint used for? Advanced

    To roll back part of a transaction with Database.rollback(sp) when a later step fails, while keeping the rest of the logic in control.

Preparing with friends?

Share this page with your study group or placement batch.

Search LearnFrenzy

Esc

Quick links