Process Automation & Logic: Basic Apex 2 Flashcards

Conditionals | Loops | Switch | Apex Control Flow (31 cards)

1
Q

What are the different conditional statements?

A

IF, IF-ELSE, ELSE-IF

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the different looping constructs?

A

Traditional For loop
List/Set For loop
SOQL For Loop
While Loop
Do-While Loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Repeated _________ statements can be used, and each condition will be evaluated sequentially until one of the expressions evaluates to true.

A

else if

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Using else if statements allow using as many code blocks as needed. True or False?

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

A _____________ executes a block of code repeatedly based on initialization, exit condition, and increment.

A

traditional for loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

A traditional for loop uses the following syntax:

A

for (initialization; condition; increment) { code_block }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The List or Set iteration loop uses the following syntax:

A

for (type variable: listOrSet)
{
code_block
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The variable data type should be of the same data type contained in the list or set collection. True or False?

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Are you allowed to add or remove items from a collection while iterating through the collection?

A

No, this is not allowed and will throw an exception.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

An __________ processes the results of an SOQL query that is defined in the loop.

A

SOQL for loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Query results are processed in batches of ___ records or one record at a time depending on the variable data type used

A

200

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The _______ is used to execute a code block repeatedly as long as a particular condition is true.

A

while loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

The ________ can be used when the loop needs to be executed at least once.

A

do-while loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

____________ can be defined to create a custom set of instructions when traversing a List collection.

A

Custom iterators

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are some considerations for using custom iterators?

A

The methods in the custom iterator must be defined as global or public. Custom iterators can be used in while loops only. To use custom iterators in for loops, the Iterable interface can be used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Custom iterators can be used to process data that come from __________, or it can also be used to traverse complex data such as queries with multiple SELECT statements.

A

outside Salesforce

17
Q

To create a custom iterator, the Iterator interface is implemented that uses _________ for determining if there is still an item in the collection to traverse and _______ for retrieving the next item.

A

hasNext(), next()

18
Q

The _______ statement can be used conditionally within a procedural loop statement to exit the entire loop.

19
Q

The _________ statement can be used conditionally within a procedural loop statement to skip the current iteration and jump to the next iteration.

20
Q

A ________ can be used to determine if an expression matches one of several values and branches.

A

switch statement

21
Q

The keyword _____ is used to match values. The _______ value can be a single value, multiple values, or sObject types.

22
Q

If there is no matching value, the when else code block is executed, which must be the last block in the switch statement. If there is no when else block, no action is taken. True or False?

23
Q

It is possible to nest Apex switch statements to provide multiple execution paths within a when clause. True or False?

24
Q

Each when value must be unique, and it is possible for a value to be null. True or False?

25
A when block can use multiple, comma-separated literal and enum values, but not both simultaneously. True or False?
True
26
Only one sObject type can be used per when block. True or False?
True
27
A developer is asked to write an application for calculating house rental subsidy for its employees. The conditions are such that if an employee's salary is lesser or equal than a certain minimum amount, the rate of 7.5% is used in calculating the subsidy amount.If the salary is between the minimum and a certain maximum amount, 5% is used. If greater than the maximum amount, the rate is 2.5%.However, if the employee has been with the company for more than a certain number of years, 10% is applied regardless of the salary amount earned by the employee.
If, for instance, the minimum amount is 1,000 and the maximum amount is 2,000, the logic can be executed using if-else statements.
28
A store sells accessories for parties and special events. It prices its balloons based on their colors as there are colors that are more popular, and others that are harder to find, or more expensive to buy from its suppliers. A default price is applied for the rest of the colors. How can this scenario be translated using only one type of control flow in Apex?
Provided that the colors of the balloons and their prices are known, the scenario can be interpreted using a switch statement with multiple literal string values. An when else block is added to the statement to catch the rest of the values. Note that if-else statements can also be used in the scenario above. The multiple-value capability of the switch statement makes the code look cleaner and easier to maintain.
29
A method returns a list of sObjects which object types are unknown beforehand as the object type is only determined during run-time. Once the type is determined, a field is updated for all the records. How can an Apex control flow be used to handle this requirement?
A for-loop statement can be used in combination with a switch statement. An if-else statement can also be used instead of a switch statement by using the instance of keyword to identify the object type.
30
A developer needs to query a large number of records from the database for processing. How can the records be queried from the database that it avoids potential heap size error and DML governor limits when updating?
The SOQL for-loop can be used to query results that return many records and avoid the limit on heap size. In this type of loop, each iteration will process records in batches of 200.
31
A feature in an application needs to perform a process that may fail or succeed. If it succeeds, it will exit the loop and move on. If it fails, it will try again. However, it will stop trying after failing a maximum number of times.
The do-while loop is most suitable in this requirement.