Apex Basics and Collections Q&A
1. What are the OOPS Concepts?
Any language which supports the following concepts are called OOPs based languages.
- Encapsulation – Class and Interface – a way to bundle data and capabilities to operate on that data.
- Abstraction – providing the appropriate level of information and data at the requested level, with the help of access specifiers (i.e. private, protected, public and global)
- Polymorphism – it allows you to define ways of dealing with data without knowing exactly how that data will be defined in the future.
- Inheritance – the capability of a class or object to have certain methods and attributes passed down to them through a hierarchy, this we will achieve by using extends and implements keywords.Apex supports all the above mentioned concepts so Apex is OOPs based language.
- Note: Apex is Case Insensitive.
2.What is the difference between non-static and static?
- By default all the variables and methods are non-static.
- Scope of the non-static variables or methods is within the scope of the same object.
- We can declare variables and methods as static by using static keyword.
- Scope of the static variables and methods is throughout the transaction.
- Static variables and methods, we can directly call with class name (we cannot access static variables and methods with object name).
3.What are the types of Collections available in Apex?
- List – ordered and allow duplicates
- Set – unordered and won’t allow duplicates
- Map – Key and value pair
4. What is the difference between List and Set?
||Doesn’t allow duplicates
||Can access elements with index
||Cannot be accessed with index
||We can sort list elements with sort method – default sorting order is ascending
||Sort method is not available
||Contains method is not available
||Contains method is available, to search for a particular element
||Can process records which are stored in list using DML statements (insert, update, delete and undelete)
||Cannot process records which are stored in set using DML statement
5.What is the maximum size of the list?
No limit for the size of a list. It only depends on the heap size which is 6 MB (Synchronous) and 12 MB (Asynchronous).
6.What are the map methods available in Apex?
Map holds key and value pair. Map keys are case-sensitive.
Map<Datatype, Datatype> mapName = new Map<Datatype, Datatype> ();
keyset(): To fetch only keys from the map.
values(): To fetch only values from the map.
containsKey(value): To search a key from the map.
get(key): By supplying the key we can fetch the value.
put(key,value): To add key and value in a map.