Apex Collections Practice Questions

Share This Post

  1. Create a list of strings and add five different city names to it. Then, remove the third city from the list and print the updated list.
  2. Create a list of integers and add ten random numbers to it. Sort the list in ascending order and print the sorted list.
  3. Create a custom object Employee with fields Name (String) and Age (Integer). Create a list of Employee objects, add five employees to the list, and sort the list by age.
  4. Create a set of integers and add ten different numbers to it, including some duplicates. Print the set and observe the behavior regarding duplicates.
  5. Create two sets of strings representing two different groups of students. Find the union, intersection, and difference of the two sets.
  6. Create a map with string keys and integer values. Add five key-value pairs to the map and print all the keys and values.
  7. Create a map where the keys are strings and the values are lists of strings. Add some data representing a department (key) and the list of employees (value) in each department. Print the map.
  8. Create a map where the keys are integers representing employee IDs and the values are Employee custom objects. Add five employees to the map and print the map.
  9. Write an Apex class that updates the Phone field for a list of Account records using a map where the key is the AccountId and the value is the new phone number.
  10. Write an Apex class that inserts a list of new Contact records for a specific Account.
  11. Write an Apex class that deletes all Opportunity records with a specific StageName using a set of OpportunityIds.
  12. Write an Apex class that inserts a list of new Contact records with specified names and associates them with a specific Account.
  13. Write an Apex class that updates the Phone field for a list of Account records using a map where the key is the AccountId and the value is the new phone number.
  14. Write an Apex class that deletes all Opportunity records with a specified StageName using a set of OpportunityIds.
  15. Create a custom object Course__c with fields Name__c (String) and Duration__c (Integer). Write an Apex class that inserts multiple Course__c records using a list.
  16. Create a custom object Student__c with fields Name__c (String) and Age__c (Integer). Write an Apex class that updates the Age__c field for a list of Student__c records using a map where the key is the StudentId and the value is the new age.
  17. Create a custom object Employee__c with fields Name__c (String) and Salary__c (Decimal). Write an Apex class that deletes all Employee__c records with a salary less than a specified amount using a set of EmployeeIds.
  18. Write an Apex class that retrieves all Contact records associated with a specific Account and stores their email addresses in a set. Print the set of email addresses.
  19. Create a custom object Project__c with fields Name__c (String) and Account__c (Lookup to Account). Write an Apex class that inserts multiple Project__c records associated with a specific Account.
  20. Create a custom object Order__c with fields Account__c (Lookup to Account) and Amount__c (Decimal). Write an Apex class that calculates the total order amount for each account and stores the results in a map where the key is the AccountId and the value is the total order amount. Print the map.
  21. Write an Apex class that updates the CloseDate of all Opportunity records related to a specific Account by setting it to 30 days from today.
  22. Write an Apex class that creates a summary report of Opportunity records grouped by OwnerId. Use a map where the key is the OwnerId and the value is a list of Opportunity records owned by that user.
  23. Write an Apex class that tracks changes in the Amount field of Opportunity records. Use a map where the key is the OpportunityId and the value is the old Amount before the update.
  24. Write an Apex class that compares the Phone numbers of Account records from two lists and updates the Phone number of the records in the first list with the values from the second list if they differ. Use maps to facilitate this comparison.
  25. Write an Apex class that removes duplicate Email addresses from a list of Contact records. Use a map where the key is the Email address and the value is the Contact record.
  26. Write an Apex class that groups Contact records by their AccountId. Use a map where the key is the AccountId and the value is a list of Contact records associated with that account.
  27. Write an Apex class that retrieves Opportunity records by StageName. Use a map where the key is the StageName and the value is a list of Opportunity Ids that have that stage.
  28. Write an Apex class that creates and inserts Contact records. Use a map where the key is the AccountId and the value is a list of Contact names. Each contact should be associated with the corresponding account.
  29. Write an Apex class that updates the Phone field of Account records. Use a map where the key is the AccountId and the value is the new phone number.
  30.  Write an Apex class that calculates the total amount of Order__c records for each Account__c. Use a map where the key is the Account__c Id and the value is the total amount of orders for that account.
  31. Create a custom object Product__c with fields Name__c (String) and Price__c (Decimal). Write an Apex class that uses a map where the key is the product name and the value is the price to insert multiple Product__c records.

  32. Delete Opportunity records based on their OpportunityId. Write an Apex class that uses a map where the key is the OpportunityId and the value is a Boolean indicating whether to delete (true) or not (false).

  33.  Create a custom object Order__c with fields Account__c (Lookup to Account) and Amount__c (Decimal). Write an Apex class that calculates the total order amount for each account and stores the results in a map where the key is the AccountId and the value is the total amount.

  34. Create a custom object Student__c with fields Name__c (String) and Grade__c (String). Write an Apex class that retrieves all Student__c records and creates a map where the key is the student name and the value is the grade.

  35. Create a custom object Employee__c with fields Name__c (String) and Salary__c (Decimal). Write an Apex class that updates the Salary__c field for Employee__c records using a map where the key is the EmployeeId and the value is the new salary.

  36. Create a custom object Project__c with fields Name__c (String) and Account__c (Lookup to Account). Write an Apex class that inserts multiple Project__c records using a map where the key is the project name and the value is the associated AccountId.

  37. Create a custom object Course__c with fields Name__c (String) and Instructor__c (String). Write an Apex class that retrieves all Course__c records and creates a map where the key is the course name and the value is the instructor’s name.

  38. Write an Apex class that finds duplicate Account records based on their Name field. Use a Set to identify duplicate account names.

  39. Write an Apex class that retrieves all Opportunity records and adds their StageName to a Set. Print the unique stage names.

  40. Write an Apex class that retrieves all Contact records and uses a Set to find those that do not have an associated Account.

  41. Write an Apex class that removes duplicate Lead records from a List based on the Email field using a Set.

  42. Write an Apex class that checks for Opportunity records where the CloseDate is in the past but does not have an associated Account. Use a Set to find those opportunities.

  43. Write an Apex class that validates whether a set of Contact IDs exist in the database and prints the IDs of those that do not exist.

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

NetSuite Salesforce Integration: An Automation Guide

NetSuite Salesforce Integration is the seamless connection between NetSuite, a leading cloud-based Enterprise Resource Planning (ERP) system, and Salesforce, a premier Customer Relationship Management (CRM) platform.