Salesforce Developer – Apex Basics Questions

Apex is the backbone of custom development in Salesforce, providing a powerful programming language for implementing complex business logic. These questions cover Apex characteristics, execution context and governor limits, class implementation syntax, code invocation methods, exception handling, static variables usage, unit testing approaches, collection types, recursive trigger prevention, DML operations, custom exception implementation, transaction control methods, debugging techniques, bulk-safe coding practices, and comparisons with other programming languages. Mastering Apex basics is essential for any Salesforce Developer.

Apex Basics - Q&A

  1. Q1. What are the key characteristics of Apex as a programming language?
    Ans:
    - Strongly-typed: Requires explicit variable declaration
    - Object-oriented: Supports classes, interfaces, inheritance
    - Governor limits: Runtime restrictions (e.g., 100 SOQL queries/transaction)
    - On-demand: Executes when invoked (triggers, controllers, etc.)
  2. Q2. Explain the Apex execution context and governor limits.
    Ans: Apex runs in a multi-tenant environment with strict limits:
    - CPU timeout: 10,000ms synchronous, 60,000ms async
    - Heap size: 6MB synchronous, 12MB async
    - SOQL queries: 100 synchronous/transaction
    Pro Tip: Always bulkify code to avoid limits.
  3. Q3. How do you implement a basic Apex class with proper syntax?
    Ans: Example:
    public class AccountProcessor {
      // Class variable
      private static Integer count = 0;
      
      // Method with parameters
      public static void updateAccountStatus(List accounts) {
        for(Account acc : accounts) {
          acc.Status__c = 'Active';
        }
        update accounts; // DML operation
      }
    }
  4. Q4. What are the different ways to invoke Apex code?
    Ans:
    - Triggers: On record events (insert/update)
    - Visualforce controllers: Page interactions
    - Lightning Web Components: @AuraEnabled methods
    - Scheduled/Batch: Asynchronous execution
    - Anonymous Apex: Developer Console execution
  5. Q5. How do you handle exceptions in Apex?
    Ans: Using try-catch blocks:
    try {
      Account acc = [SELECT Id FROM Account WHERE Name = 'Nonexistent'];
    } catch(QueryException qe) {
      System.debug('Error: ' + qe.getMessage());
    } finally {
      // Cleanup code
    }
  6. Q6. What are static variables in Apex and when would you use them?
    Ans: Static variables:
    - Maintain state across transaction boundaries
    - Shared across all class instances
    - Common uses:
    * Counters in triggers
    * Flags to prevent recursive triggers
    * Caching frequently used data
  7. Q7. How do you write a unit test for a simple Apex class?
    Ans: Example test class:
    @isTest
    private class AccountProcessorTest {
      @isTest static void testUpdateAccountStatus() {
        // Setup test data
        Account testAcc = new Account(Name='Test');
        insert testAcc;
        
        // Execute method
        Test.startTest();
        AccountProcessor.updateAccountStatus(new List{testAcc});
        Test.stopTest();
        
        // Verify results
        Account updatedAcc = [SELECT Status__c FROM Account WHERE Id = :testAcc.Id];
        System.assertEquals('Active', updatedAcc.Status__c);
      }
    }
  8. Q8. What are the different types of collections in Apex?
    Ans:
    - Lists: Ordered, allows duplicates (most common)
    - Sets: Unordered, unique elements (good for deduplication)
    - Maps: Key-value pairs (fast lookup by key)
    Example: Map accountMap = new Map([SELECT Id FROM Account]);
  9. Q9. How do you prevent recursive trigger execution?
    Ans: Common patterns:
    1. Static variable flag:
    public class TriggerHelper {
      public static Boolean isFirstRun = true;
    }
    2. Handler pattern: Centralized trigger logic with execution control
  10. Q10. What are the different DML operations in Apex?
    Ans:
    - insert, update, upsert, delete, undelete, merge
    - Database methods (better for error handling):
    Database.insert(records, allOrNone)
    - Best practice: Always use bulk DML operations
  11. Q11. How do you implement custom exceptions in Apex?
    Ans: Create custom exception classes:
    public class MyCustomException extends Exception {}
                  
    // Usage:
    if(someCondition) {
      throw new MyCustomException('Error message');
    }
  12. Q12. What are transaction control methods in Apex?
    Ans:
    - Savepoint: Create rollback points
    Savepoint sp = Database.setSavepoint();
    - Rollback: Revert to savepoint
    Database.rollback(sp);
  13. Q13. How do you debug Apex code effectively?
    Ans:
    - System.debug() statements (view in Developer Console)
    - Debug logs with appropriate log levels
    - Checkpoints in Salesforce DX
    - Anonymous Apex for quick testing
  14. Q14. What are the best practices for writing bulk-safe Apex?
    Ans:
    - Process records in collections (not single-record)
    - Move queries/DML outside loops
    - Use maps for efficient lookups
    - Implement error handling for partial successes
  15. Q15. How do you compare Apex with other programming languages?
    Ans:
    - Similar to Java: Syntax, OOP concepts
    - Unique aspects:
    * Governor limits
    * Tight integration with Salesforce data model
    * No file system access
    * Automatic transaction management

Back to Developer Home