SP Programming Fundamentals 3 — Questions and Answers
Question 1: Which Apex exception type is thrown when code attempts to access a List index that does not exist?
- NullPointerException
- QueryException
- ListException (Correct answer)
- TypeException
Correct answer: ListException
Accessing an out-of-bounds index on a List in Apex throws a `ListException`.
Question 2: What is the correct way to prevent a trigger from recursively firing in Apex?
- Use a static Boolean flag in a helper class (Correct answer)
- Set trigger.isExecuting to false
- Use the @NoRecursion annotation
- Add a LIMIT clause to SOQL inside the trigger
Correct answer: Use a static Boolean flag in a helper class
A static Boolean flag persists for the transaction and can be checked to skip subsequent trigger executions.
Question 3: Which Apex method converts a Datetime to a formatted String?
- Datetime.parse()
- Datetime.now().toString()
- Datetime.format() (Correct answer)
- Datetime.valueOf()
Correct answer: Datetime.format()
`Datetime.format(String)` returns a formatted string representation of the Datetime value.
Question 4: In Salesforce, what does SOSL stand for?
- Salesforce Object Search Language (Correct answer)
- Salesforce Object Selection Language
- Standard Object Structured Language
- Salesforce Online Scripting Language
Correct answer: Salesforce Object Search Language
SOSL stands for Salesforce Object Search Language, used for text-based searches across multiple objects.
Question 5: Which of the following is a valid way to iterate over all values in an Apex Map?
- for (String key : myMap) {}
- for (Integer val : myMap.values()) {} (Correct answer)
- for (Map.Entry entry : myMap) {}
- for (String key : myMap.keySet()) { Integer val = myMap[key]; }
Correct answer: for (Integer val : myMap.values()) {}
`myMap.values()` returns a collection of the map's values that can be iterated directly.
Question 6: A developer wants an Apex method to run asynchronously and make a callout. Which annotation should be used?
- @future(callout=true) (Correct answer)
- @async
- @future
- @RemoteAction(callout=true)
Correct answer: @future(callout=true)
`@future(callout=true)` is required for an asynchronous method that makes external HTTP callouts.
Question 7: What does the `instanceof` keyword do in Apex?
- Creates a new instance of a class
- Checks whether an object is an instance of a specific type (Correct answer)
- Casts an object to a specified type
- Retrieves the runtime class name of an object
Correct answer: Checks whether an object is an instance of a specific type
`instanceof` is a type-check operator that returns true if the object is an instance of the specified class or interface.
Which Apex exception type is thrown when code attempts to access a List index that does not exist?