PD1 Apex Programming 2 — Questions and Answers
Question 1: Which Apex collection type preserves insertion order and allows duplicate values?
- Set
- Map
- List (Correct answer)
- Queue
Correct answer: List
A List in Apex is an ordered collection that allows duplicates, unlike Sets which are unordered and unique.
Question 2: What happens when an Apex trigger calls a future method?
- It executes synchronously in the same transaction
- It is queued and runs asynchronously after the transaction commits (Correct answer)
- It throws a compile-time error because future methods cannot be called from triggers
- It runs immediately in a separate thread
Correct answer: It is queued and runs asynchronously after the transaction commits
Future methods are asynchronous and are enqueued to run after the current transaction successfully commits.
Question 3: Which keyword is used in Apex to prevent a class from being instantiated directly?
- abstract (Correct answer)
- virtual
- static
- protected
Correct answer: abstract
The `abstract` keyword marks a class that cannot be instantiated and must be extended by a concrete subclass.
Question 4: What is the maximum number of SOQL queries allowed per Apex transaction in a synchronous context?
- 50
- 100 (Correct answer)
- 150
- 200
Correct answer: 100
Salesforce enforces a governor limit of 100 SOQL queries per synchronous Apex transaction.
Question 5: Which exception type is thrown when a DML operation violates a required field or validation rule?
- DmlException (Correct answer)
- SObjectException
- TypeException
- QueryException
Correct answer: DmlException
A DmlException is thrown when a DML operation fails due to validation rules, required fields, or other data issues.
Question 6: In Apex, what does the `with sharing` keyword enforce?
- Field-level security for all field reads
- Object-level CRUD permissions for the running user
- Record-level access based on sharing rules for the running user (Correct answer)
- System-level access ignoring all security
Correct answer: Record-level access based on sharing rules for the running user
`with sharing` enforces the sharing rules of the running user, restricting record access to what they are permitted to see.
Question 7: Which Apex method is used to convert a JSON string into an Apex object?
- JSON.serialize()
- JSON.deserialize() (Correct answer)
- JSON.parse()
- JSON.convert()
Correct answer: JSON.deserialize()
JSON.deserialize() converts a JSON string into an Apex type, while JSON.serialize() does the opposite.
Which Apex collection type preserves insertion order and allows duplicate values?