Show answerHide answer
Correct answer: C var count = job.Activities.countWhere(\act -> act.Priority == TC_HIGH); print(String.format("Count of High Priority Activities: %s", {count}))
Explanation
Answer Option (Which option is correct) C. var count = job.Activities.countWhere(\act -> act.Priority == TC_HIGH); print(String.format("Count of High Priority Activities: %s", {count}))Explanation:
countWhere expresses a count of elements satisfying a predicate without explicitly building a separate filtered list. In this question the predicate is the activity priority. An extra list conversion or a manual counter adds work or complexity to the same collection-level calculation.
Why the other options are incorrect:
A. var count = 0; job.Activities.each(\act -> act.Priority == TC_HIGH) { count += 1 }; print(String.format("Count of High Priority Activities: %s", {count})):
The each expression does not implement the displayed conditional counting syntax. It mixes a predicate and a following block. The shown countWhere expression states the count directly.
B. var actList = job.Activities.toList(); var count = actList.countWhere(\act -> act.Priority == TC_HIGH); print(String.format("Count of High Priority Activities: %s", {count})):
Converting to a list creates an additional collection representation. The original activity collection can be counted. That extra conversion is unnecessary here.
D. var count = 0; for (act in job.Activities) { if (act.Priority == TC_HIGH) { count += 1 } }; print(String.format("Count of High Priority Activities: %s", {count})):
A manual loop can count the matching activities. It requires an explicit mutable counter. countWhere expresses the same predicate more directly.
E. var count = 0; job.Activities.where(\act -> act.Priority == TC_HIGH) { count += 1 }; print(String.format("Count of High Priority Activities: %s", {count})):
where normally returns the filtered collection. The trailing counting block is not the shown collection-counting form. Use a count operation for the result.
References:
Gosu language project - Operators, loops, functions, and properties
Guidewire - Query-backed collections and buildBaseQuery