SP Programming Fundamentals 2 — Questions and Answers
Question 1: Which Apex collection type enforces uniqueness of its elements?
- List
- Map
- Set (Correct answer)
- Queue
Correct answer: Set
A Set in Apex automatically enforces uniqueness, preventing duplicate values.
Question 2: What is the result of executing `Integer x = 10 / 3;` in Apex?
- 3.33
- 3 (Correct answer)
- 4
- Compile error
Correct answer: 3
Integer division in Apex truncates the decimal, so 10 / 3 equals 3.
Question 3: Which keyword makes an Apex class available for extension by other classes?
- abstract
- virtual (Correct answer)
- global
- protected
Correct answer: virtual
The `virtual` keyword allows other classes to extend and override the class's methods.
Question 4: A developer writes a SOQL query inside a `for` loop processing 300 records. What Salesforce limit does this risk violating?
- Heap size limit
- SOQL query rows limit
- SOQL queries per transaction limit (Correct answer)
- CPU time limit
Correct answer: SOQL queries per transaction limit
Running SOQL inside a loop can exceed the 100 SOQL queries per transaction governor limit.
Question 5: Which Apex access modifier restricts a method to be called only within the same class?
- public
- global
- private (Correct answer)
- protected
Correct answer: private
`private` limits access to only the defining class, the most restrictive modifier.
Question 6: What does the `@AuraEnabled` annotation do in an Apex class?
- Marks a method as a future method
- Exposes a method to Lightning components (Correct answer)
- Allows a class to be invoked from Flows
- Makes a method accessible from REST
Correct answer: Exposes a method to Lightning components
`@AuraEnabled` exposes Apex methods so they can be called from Lightning Web Components or Aura components.
Question 7: Which statement correctly declares a Map in Apex that keys Strings to Integers?
- Map<String, Integer> m = new Map<String, Integer>(); (Correct answer)
- Map(String, Integer) m = new Map();
- Map m = new Map<String, Integer>();
- Map<String:Integer> m = new Map<String:Integer>();
Correct answer: Map<String, Integer> m = new Map<String, Integer>();
Apex Map declarations use angle-bracket generic syntax: `Map<KeyType, ValueType>`.
Which Apex collection type enforces uniqueness of its elements?