Apex Syntax Review

  1. An Apex interface and Apex class, namely MyInterface and MyClass respectively, exists in the org. Another class called MyExtendedClass, which should be accessible publicly, needs to be created that should implement MyInterface but also extend from MyClass to inherit its methods and properties. How should the MyExtendedClass Apex class be declared?
public class MyExtendedClass extends MyClass implements MyInterface {
    public void MyInterfaceMethod() {}
}
  1. Two Apex interfaces, namely MyInterfaceA and MyInterfaceB, exist in an org. A developer is required to create an Apex class that implements these two interfaces for a new functionality required in the org. The name of the Apex class to create is called MyClass and should be accessible globally. How should the Apex class be declared?
global class MyClass implements MyInterfaceA, MyInterfaceB {
    public void MyInterfaceAMethod() {}
    public void MyInterfaceBMethod() {}
}
  1. The following Apex code should update two account records in Salesforce. Declare a list of accounts named ‘accounts’, add the account variables named ‘acc1’ and ‘acc2’ to the list, and then update the list of accounts.
List<Account> accounts = new List<Account>();
Account acc1 = [SELECT Id, Type FROM Account WHERE Name = 'GenePoint'];
acc1.Type = 'Installation Partner';
accounts.add(acc1);
Account acc2 = [SELECT Id, Type FROM Account WHERE Name = 'Express Logistics and Transport'];
acc2.Type = 'Technology Partner';
accounts.add(acc2);
update accounts;
  1. The Apex class below assigns a value to a constant by using a static initializer method, but it’s missing the declaration of the constant. Declare the constant in place of the comment. It should be possible to access the constant from any Apex code in the application or namespace without creating an instance of the class.
public class DiscountClass {
   public static final Decimal DISCOUNT;
   public static Decimal getDiscount() {
       return 12.50;
   }
   static {
       DISCOUNT = getDiscount();
   }
}
  1. Declare a map variable named ‘countryCurrencyMap’ using a single line of code. Each key in the map should represent a unique country that maps to the country’s currency. The variable should contain the following key-value pairs: ‘US’ : ‘USD’ ‘France’ : ‘EUR’ ‘Australia’ : ‘AUD’
Map<String, String> countryCurrencyMap = new Map<String, String>{'US' => 'USD', 
                                                                 'France' => 'EUR',
                                                                 'Australia' => 'AUD'};
  1. Output the numbers 1-10 into the debug log. Use a ‘for loop’ statement with an ‘i’ counter variable that starts at 1, and an exit condition that uses the ‘lesser than or equal’ operator.
for (Integer i = 1; i <= 10; i++) {
    System.debug(i);
}
  1. An org has been having issues with governor limits related to heap size as a result of a query that returns too many records. A developer wants to write what is called a SOQL for loop to help mitigate the issue and at the same time process records in batches. If the name of the variable to use is var and the SOQL statement to execute is “SELECT Id, Name FROM Account”, how should the SOQL for loop be declared?
for (List<Account> var: [SELECT Id, Name FROM Account]) {
    System.debug(var);
}
  1. A developer is trying to update the status of leads based on their source (LeadSource). The logic for the update is as follows: If the lead source is ‘eCommerce,’ update the status to ‘MQL.’ If the lead source is ‘Referral,’ update the status to ‘SQL.’ For other lead sources, update the status to ‘Marketing Lead.’ Modify the placeholder code to satisfy the requirement.
List<Lead> leads = [SELECT Id, LeadSource, Status FROM Lead];
for (Lead myLead : leads) {
    if (myLead.LeadSource == 'eCommerce') {
        myLead.Status = 'MQL';
    } else if (myLead.LeadSource == 'Referral') {
        myLead.Status = 'SQL';
    } else {
        myLead.Status = 'Marketing Lead';
    }
}
update leads;
  1. A developer is trying to effectively use conditional branching in evaluating against the Lead record’s industry. Modify the for loop so that: If the industry is ‘Environmental’, set the lead’s rating to ‘Hot’. If the industry is ‘Retail’, set the lead’s rating to ‘Warm’. If the industry is ‘Banking’, set the lead’s rating to ‘Cold’.
List<Lead> leads = [SELECT Id, Industry, Rating FROM Lead];
for (Lead myLead : leads) {
    switch on myLead.Industry {
        when 'Environmental' {
            myLead.Rating = 'Hot';
        }
        when 'Retail' {
            myLead.Rating = 'Warm';
        }
        when 'Banking' {
            myLead.Rating = 'Cold';
        }
    }
}
update leads;
  1. A developer is working on a method to create test leads in a test data factory class. The method will accept an integer parameter called “numOfRecords” and create a corresponding number of lead records using a traditional for loop, initialized with an integer “i”. The naming format for the test leads will be “Test Lead 0”, “Test Lead 1”, “Test Lead 2”, etc. Modify the placeholder code to satisfy the requirements.
public static void createTestLeads(Integer numOfRecords) {
    List<Lead> testLeads = new List<Lead>();
    for (Integer i = 0; i < numOfRecords; i++) {
        Lead myLead = new Lead();
        myLead.FirstName = 'Test';
        myLead.LastName = 'Lead ' + i;
        myLead.Company = 'Test Company';
        testLeads.add(myLead); 
    }
    insert testLeads;
}
  1. A developer is required to create a batch Apex class to perform an update on all leads in the org. The class is called ‘LeadBatchClass’ and needs to be publicly accessible by other classes within the same namespace. The start method uses the Database.QueryLocator object to define the scope and the BatchableContext variable name is ‘BC’. Modify the code to define and complete this batch class.
public class LeadBatchClass implements Database.Batchable<sObject> {
    public Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id, 
                               Rating, 
                               Industry 
                          FROM Lead 
                         WHERE Industry = \'Environmental\'';
        return Database.getQueryLocator(query);
    }
    public void execute(Database.BatchableContext BC, List<Lead> scope) {
        for (Lead myLead : scope) {
            myLead.Rating = 'Hot';
        }
        update scope;
    }
    public void finish(Database.BatchableContext BC) {}
}
  1. A new custom Apex interface called ‘ContactDiscountInterface’ needs to be created in an org. The interface needs to be accessible globally and contains a method named ‘getContactDiscount’ that intakes an Id parameter called ‘contactId’ and returns a decimal number. Modify the code to meet the requirements.
global interface ContactDiscountInterface {
        Decimal getContactDiscount(Id contactId);
}
  1. A developer is creating a queueable Apex class named ‘MyOpportunityQueueableClass’ that takes a list of opportunities as an argument and moves their stage to ‘Closed Won.’ The queueable class will need to be publicly accessible, and the QueueableContext variable name is ‘context’. Modify the code to define and complete this queueable class.
public class MyOpportunityQueueableClass implements Queueable {
    public List<Opportunity> oppList ; 
    public MyOpportunityQueueableClass(List<Opportunity> oppList){
        this.oppList = oppList;
    }
    public void execute(QueueableContext context) {
        for(Opportunity opp : oppList){
            opp.StageName = 'Closed Won';
        }
        update oppList;
    }
}
  1. A developer is working on an Apex class called ‘MyClass’ to get the Id and Name of all contacts that the running user has access to. This class needs to be accessible publicly. Modify the code to define and complete the class.
public with sharing class MyClass {
    public List<Contact> getContacts() {
        return [SELECT Id, Name FROM Contact];
    }
}
  1. A developer is tasked with creating an Apex class named ‘MyExtensibleClass’ that needs to be accessed globally and allow extensions or overrides. The class contains a method name ‘getLeads’ that returns a list of leads, has the same access modifier as the class, and can be overridden by other classes that extend ‘MyExtensibleClass.’ Modify the code to satisfy the requirements.
global virtual class MyExtensibleClass {
    global virtual List<Lead> getLeads() {
        return [SELECT Id FROM Lead WHERE Company = 'Universal Container'];
    }
}
  1. A custom object called Commission__c has been created with a lookup relationship field named “Opportunity”, which is used to reference the opportunity that commissions are related to. A SOQL query has been created that returns the Type and Amount fields of the commission records that belongs to the current logged in user. Modify the query such that the results also include the name of the related opportunity after the Amount__c field in the SOQL statement.
List<Commission__c> records = [SELECT Type__c, Amount__c, Opportunity__r.Name 
                                 FROM Commission__c 
                                WHERE OwnerId = :UserInfo.getUserId()];
  1. A developer is trying to use Apex to create new or update existing Lead records based on an External Id field called “External_Application_Id__c”. Modify the placeholder code to meet the requirements.
List<Lead> leadsToInsertOrUpdate = new List<Lead>();
for (Integer i = 0; i < 5; i++) {
    Lead myLead = new Lead();
    myLead.FirstName = 'Test';
    myLead.LastName = 'Lead ' + i;
    myLead.Company = 'Test Corp.';
    myLead.External_Application_Id__c = 'externalapplicationid' + i;
    leadsToInsertOrUpdate.add(myLead);
}
upsert leadsToInsertOrUpdate External_Application_Id__c;
  1. A developer is trying to update the email opt-out status of leads coming from Acme. A SOQL query has been created to return the Email and Email Opt Out fields of lead records. Modify the SOQL query so that it only returns lead records with emails ending in ‘@acme.com’.
List<Lead> leads = [SELECT Id, Email, HasOptedOutOfEmail 
                      FROM Lead 
                     WHERE Email 
                      LIKE '%@acme.com'];
for (Lead myLead : leads) {
    myLead.HasOptedOutOfEmail = true;
}
update leads;
  1. The following Apex class has been created to update the owner of opportunities where the amount is greater than $5,000,000.00 to be the sales manager. Modify the placeholder code so that the Apex class can update the list of opportunities and allow partial success.
User salesManager = [SELECT Id, Name 
                       FROM User 
                      WHERE Name = 'John Doe' 
                      LIMIT 1];
List<Opportunity> opportunities = [SELECT Id, OwnerId, Amount 
                                     FROM Opportunity 
                                    WHERE Amount > 5000000];
for (Opportunity opp : opportunities) {
    opp.OwnerId = salesManager.Id;
}
Database.update(opportunities, false);