Pages

Saturday, August 1, 2026

Apex Interview Questions — Part 1 (Beginner Basics)

 

1. What is Apex and how is it different from Java?

Apex is Salesforce's proprietary, strongly-typed, object-oriented programming language used to execute flow and transaction control statements on the Salesforce platform. It's syntactically similar to Java, but it runs on Salesforce's multi-tenant servers, is tightly coupled with the database (native DML and SOQL support), and is governed by strict limits (governor limits) that don't exist in standard Java since Salesforce's infrastructure is shared across all customers.

2. What are the primitive data types supported in Apex?

Apex supports primitive types including Integer, Long, Double, Decimal, String, Boolean, Date, Datetime, Time, ID, and Blob. These behave similarly to primitives in other languages but ID is Salesforce-specific and represents an 18-character record identifier.

3. What is the difference between a List, Set, and Map in Apex?

  • List: an ordered collection that allows duplicate values, accessed by index.
  • Set: an unordered collection of unique values — no duplicates allowed.
  • Map: a collection of key-value pairs where each key is unique, used heavily for quick lookups (e.g., mapping an Id to an sObject).
apex
List<String> names = new List<String>{'Alice', 'Bob'};
Set<Id> accountIds = new Set<Id>();
Map<Id, Account> idToAccount = new Map<Id, Account>();

4. What is an sObject in Apex?

An sObject is a generic representation of any Salesforce object (standard or custom) — like Account, Contact, or MyCustomObject__c. Every Salesforce record you query, insert, update, or delete in Apex is handled as an sObject or a specific subtype of it.

5. How do you declare and use a variable in Apex?

Variables are declared with a type followed by a name, optionally initialized with a value:

apex
Integer counter = 0;
String greeting = 'Hello';
Account acc = new Account(Name = 'Acme Corp');

6. What is the difference between == and .equals() in Apex?

In Apex, == compares value equality for primitives (unlike Java, where == compares object references for non-primitives). For strings and primitive types, == and .equals() behave the same way in Apex — this is a key difference from Java that trips up developers coming from a Java background.

7. What are Apex collections and why are they important?

Collections (List, Set, Map) let you work with multiple records or values efficiently in memory, instead of processing them one at a time. They're essential for bulkification — processing many records in a single operation instead of looping DML or SOQL calls, which helps avoid hitting governor limits.

8. What is a Trigger in Apex?

A trigger is a piece of Apex code that executes automatically before or after specific DML events (insert, update, delete, undelete) occur on a Salesforce object. Triggers are commonly used to enforce business logic, validate data, or update related records automatically.

apex
trigger AccountTrigger on Account (before insert, after update) {
    // logic here
}

9. What are the different Trigger events (before insert, after update, etc.)?

Apex triggers support: before insert, after insert, before update, after update, before delete, after delete, and after undelete. "Before" events are used to modify field values on the record before it's saved; "after" events are used when you need the record's Id or need to affect related records.

10. What is the difference between trigger.new and trigger.old?

Trigger.new contains the new versions of the records (available in insert/update/undelete triggers), while Trigger.old contains the old versions of the records before the change (available in update/delete triggers). Comparing them lets you detect exactly which fields changed.

11. What is SOQL and how is it different from SQL?

SOQL (Salesforce Object Query Language) is used to query data from Salesforce's database. Unlike standard SQL, SOQL only supports querying (not full DDL/DML), and it automatically understands relationships between Salesforce objects — but it doesn't support arbitrary joins the way SQL does.

apex
List<Account> accs = [SELECT Id, Name FROM Account WHERE Industry = 'Technology'];

12. What is the difference between SOQL and SOSL?

SOQL queries one object (and its related objects) at a time and is best when you know which object(s) to search. SOSL (Salesforce Object Search Language) searches text across multiple objects simultaneously, similar to a full-text search — useful when you don't know which object contains the match.

13. What are governor limits in Salesforce, and why do they exist?

Governor limits are runtime limits enforced by Salesforce (e.g., max 100 SOQL queries or 150 DML statements per transaction) to ensure no single customer's code monopolizes shared, multi-tenant server resources. Exceeding a limit throws a runtime exception that can't be caught with normal try-catch for certain limit types.

14. What is bulkification and why does it matter in Apex?

Bulkification means writing Apex so it correctly handles multiple records at once (e.g., 200 records triggered by a bulk data load), instead of assuming only one record will ever be processed. This typically means moving SOQL/DML operations outside of loops.

apex
// Bad: DML inside a loop
for (Account a : accounts) {
    update a;
}

// Good: bulkified
update accounts;

15. What is a static variable in Apex, and when would you use one?

A static variable belongs to the class itself rather than any specific instance, and its value persists for the duration of a single transaction. It's commonly used to prevent recursive trigger execution by acting as a "flag" that's checked and set once per transaction.

apex
public class TriggerHandler {
    public static Boolean hasRun = false;
}

16. What is the difference between a class variable and an instance variable?

A class (static) variable is shared across all instances of a class and tied to the transaction, while an instance variable belongs to a specific object instance created with new and can hold different values for each instance.

17. What is an Apex constructor, and how do you define one?

A constructor is a special method that runs when an object is instantiated with new, used to set up initial state. It shares the class's name and has no return type.

apex
public class Employee {
    String name;
    public Employee(String name) {
        this.name = name;
    }
}

18. What is the difference between public, private, and global access modifiers?

  • private: accessible only within the same class (default if unspecified).
  • public: accessible within the same application/namespace.
  • global: accessible from anywhere, including other namespaces and via API — required for Apex used in managed packages or exposed externally.

19. What is a DML statement, and what are the common DML operations in Apex?

DML (Data Manipulation Language) statements modify records in the database. The common Apex DML operations are insert, update, delete, undelete, and upsert (which inserts or updates depending on whether the record already exists).

20. What is a try-catch block, and why is exception handling important in Apex?

A try-catch block lets you gracefully handle runtime errors instead of letting them crash the transaction with an unhandled exception. It's especially important in Apex because unhandled exceptions in triggers or Apex-called-from-LWC can surface confusing errors to end users if not caught and re-thrown with a clear message.

apex
try {
    insert new Account(Name = 'Test');
} catch (DmlException e) {
    System.debug('Insert failed: ' + e.getMessage());
}