CSA Apex Programming 5 — Questions and Answers
Question 1: Which Apex interface must be implemented to use Database.executeBatch()?
- Schedulable
- Queueable
- Database.Batchable (Correct answer)
- Database.Stateful
Correct answer: Database.Batchable
A class must implement Database.Batchable<sObject> to be executed with Database.executeBatch().
Question 2: What is the purpose of the @testSetup annotation in Apex test classes?
- Marks a test as requiring a sandbox
- Creates test data once and rolls it back after each test method (Correct answer)
- Marks a method to run before each test method individually
- Loads static resources into the test context
Correct answer: Creates test data once and rolls it back after each test method
@testSetup creates test data once for the class and rolls back changes between individual test methods to improve test performance.
Question 3: What does the 'without sharing' keyword do when applied to an Apex class?
- Restricts the class to system admin use
- Runs the class in system mode ignoring the user's sharing rules (Correct answer)
- Prevents sharing rule enforcement at the field level
- Inherits the sharing context from the calling class
Correct answer: Runs the class in system mode ignoring the user's sharing rules
'without sharing' runs the class in system mode, ignoring the running user's sharing rules and accessing all records.
Question 4: Which Apex data type is used to store a reference to a Salesforce record ID?
- String
- Integer
- Id (Correct answer)
- Long
Correct answer: Id
The Id data type is a specialized 18-character string type designed to store Salesforce record IDs with built-in validation.
Question 5: What is the correct way to prevent a SOQL query from being inside a for loop in a trigger?
- Use a List to collect IDs, then query outside the loop (Correct answer)
- Add a LIMIT clause to the query
- Use Database.query() instead of inline SOQL
- Use @future method to defer the query
Correct answer: Use a List to collect IDs, then query outside the loop
Best practice is to collect all needed IDs into a collection first, then execute a single SOQL query outside the loop.
Question 6: Which method in the Database.Batchable interface defines the records to process?
- execute()
- finish()
- start() (Correct answer)
- query()
Correct answer: start()
The start() method returns a QueryLocator or Iterable that defines the scope of records the batch job will process.
Question 7: What happens when you call System.enqueueJob() from within a Queueable class's execute method?
- It throws a LimitException immediately
- It chains a new Queueable job to run after the current one finishes (Correct answer)
- It runs the new job synchronously in the same transaction
- It schedules the job for the next maintenance window
Correct answer: It chains a new Queueable job to run after the current one finishes
Calling System.enqueueJob() from inside a Queueable execute() method chains a new job that runs after the current job completes.
Which Apex interface must be implemented to use Database.executeBatch()?