Databricks Certified Associate Developer for Apache Spark Practice Test Questions and Exam Dumps Part17 Q321-340

View Full Databricks Certified Associate Developer for Apache Spark Exam Dumps  and Practice Test Dumps

 

Question 321.

Which higher-order Spark SQL function can reduce an array to a single accumulated result?

  1. aggregate()
    2. transform()
    3. filter()
    4. exists()

Correct Answer: 1. aggregate()

Explanation:

aggregate() processes the elements of an array using an accumulator and returns a single result. The function is useful when a developer needs custom reduction logic that goes beyond built-in functions such as sum() or max(). For example, an array of numbers can be combined into a running total, or an array of structs can be reduced into a custom summary structure. transform() returns a new array after modifying each element, filter() keeps selected elements, and exists() returns a Boolean indicating whether any element satisfies a condition. aggregate() is therefore the correct choice when many array values must become one derived value.

Question 322.

A developer wants to determine whether every number in an array is greater than zero. Which function is most appropriate?

  1. exists()
    2. forall()
    3. array_contains()
    4. filter()

Correct Answer: 2. forall()

Explanation:

forall() evaluates a Boolean expression against every element in an array and returns true only if all applicable elements satisfy the condition. In this example, the developer can test whether every number is greater than zero without exploding the array into separate rows. exists() requires only one matching element, array_contains() looks for a specific literal value, and filter() returns an array containing only matching elements. forall() is particularly useful for nested-data validation, such as confirming that every score is valid or that every nested object satisfies a required business rule.

Question 323.

Which Spark SQL function returns true when at least one element in an array satisfies a supplied condition?

  1. transform()
    2. forall()
    3. exists()
    4. aggregate()

Correct Answer: 3. exists()

Explanation:

exists() is a higher-order function that tests each element of an array and returns true when at least one element satisfies the provided predicate. It is useful when the condition involves more than simple literal membership, such as checking whether any transaction exceeds a threshold or whether any nested struct has a specific property. forall() requires all elements to satisfy the condition, transform() modifies every element, and aggregate() reduces the collection into one result. exists() avoids the need to explode the array and perform a row-level aggregation simply to answer a Boolean question.

Question 324.

Which Spark SQL function converts an array of key-value structs into a map?

  1. map_entries()
    2. create_map()
    3. map_from_arrays()
    4. map_from_entries()

Correct Answer: 4. map_from_entries()

Explanation:

map_from_entries() accepts an array of structs representing key-value pairs and converts that array into a map. It is especially useful after using map_entries() and applying transformations to the resulting array before reconstructing the map. create_map() constructs maps from alternating key and value expressions, while map_from_arrays() uses one array of keys and another array of values. map_entries() performs the opposite conversion by turning a map into an array of key-value structs. map_from_entries() therefore fits workflows where map contents need to be manipulated using array-oriented higher-order functions.

Question 325.

Which Spark SQL function converts a map into an array of key-value structs?

  1. map_entries()
    2. map_keys()
    3. map_values()
    4. explode()

Correct Answer: 1. map_entries()

Explanation:

map_entries() transforms a map into an array of structs, where each struct contains a key and its corresponding value. This representation can be useful when developers want to apply array functions such as transform(), filter(), or sort operations to map entries without immediately creating additional rows. map_keys() returns only the keys, map_values() returns only the values, and explode() changes the row count by expanding entries. map_entries() preserves the collection inside the current row and provides a structured representation that can later be converted back into a map if necessary.

Question 326.

Which Spark SQL function builds a map from one array of keys and a second array of corresponding values?

  1. create_map()
    2. map_from_arrays()
    3. map_concat()
    4. map_entries()

Correct Answer: 2. map_from_arrays()

Explanation:

map_from_arrays() creates a map by pairing each element from a key array with the element at the corresponding position in a value array. It is useful when a pipeline naturally produces parallel key and value collections. create_map() builds a map directly from alternating expressions rather than arrays, map_concat() combines existing maps, and map_entries() converts a map into an array of structs. map_from_arrays() is therefore the appropriate operation when aligned key and value arrays already exist and should be represented as a single structured map column.

Question 327.

Which Spark SQL function is used to merge multiple map expressions into one map?

  1. map_values()
    2. map_from_arrays()
    3. map_concat()
    4. create_map()

Correct Answer: 3. map_concat()

Explanation:

map_concat() combines multiple map expressions into a single resulting map. This is useful when attributes or key-value collections come from different parts of a transformation and need to be represented together. map_values() extracts only values, map_from_arrays() constructs a map from paired arrays, and create_map() constructs a new map from alternating key and value expressions. When duplicate keys occur, behavior can depend on Spark configuration, so developers should understand duplicate-key handling before relying on a particular outcome. map_concat() is the direct function for merging already existing map columns.

Question 328.

Which Spark SQL function creates a map directly from alternating key and value expressions?

  1. map_entries()
    2. map_values()
    3. map_from_arrays()
    4. create_map()

Correct Answer: 4. create_map()

Explanation:

create_map() constructs a map using alternating key and value expressions. For example, a developer can create a map containing keys such as “city” and “country” paired with values from existing columns. map_from_arrays() instead requires separate arrays of keys and values, map_entries() converts a map into key-value structs, and map_values() extracts only the values from an existing map. create_map() is convenient when a small known set of key-value pairs should be assembled directly from DataFrame expressions without first building arrays.

Question 329.

Which Spark SQL function returns all keys from a map as an array?

  1. map_keys()
    2. map_values()
    3. map_entries()
    4. element_at()

Correct Answer: 1. map_keys()

Explanation:

map_keys() returns an array containing the keys of a map column. It is useful when the keys themselves need to be inspected, sorted, filtered, exploded, or otherwise processed. map_values() returns the values, map_entries() returns key-value structs, and element_at() retrieves a single value associated with a particular key. map_keys() is especially helpful in semi-structured datasets where the set of keys can vary across records and developers need to inspect or transform the available map fields dynamically.

Question 330.

Which Spark SQL function returns all values from a map as an array?

  1. element_at()
    2. map_values()
    3. map_keys()
    4. map_entries()

Correct Answer: 2. map_values()

Explanation:

map_values() returns an array containing all values from a map expression. Once converted to an array, those values can be processed with functions such as size(), transform(), filter(), or explode(). map_keys() returns the map’s keys, element_at() retrieves a single value by key, and map_entries() converts the map into key-value structs. map_values() is appropriate when the analytical focus is on the map contents rather than the associated keys, such as calculating metrics or applying conditions across all stored values.

Question 331.

Which Spark SQL function can retrieve the value associated with a specified key in a map?

  1. map_values()
    2. map_keys()
    3. element_at()
    4. array_position()

Correct Answer: 3. element_at()

Explanation:

element_at() can retrieve the value associated with a specified key from a map column. The same function can also retrieve elements from arrays using positional semantics. map_values() returns every value, map_keys() returns every key, and array_position() is used to locate a value inside an array. element_at() is therefore the appropriate function when a developer knows the desired map key and needs the corresponding value directly without expanding or otherwise transforming the complete map.

Question 332.

Which Spark SQL function returns the number of entries in a map or elements in an array?

  1. length()
    2. count()
    3. cardinalityBy()
    4. size()

Correct Answer: 4. size()

Explanation:

size() returns the number of elements in an array or the number of key-value entries in a map. It is commonly used for data-quality checks, detecting empty collections, filtering records based on collection length, or understanding nested-data complexity. length() usually applies to string or binary expressions, while count() aggregates records or values across DataFrame rows. size() operates within each individual row and therefore provides the correct mechanism for measuring the cardinality of nested array or map values.

Question 333.

Which Spark SQL function is most appropriate for returning a deterministic sorted version of an array?

  1. sort_array()
    2. shuffle()
    3. reverse()
    4. array_distinct()

Correct Answer: 1. sort_array()

Explanation:

sort_array() orders elements inside an array using the requested sort direction. This can provide deterministic collection ordering, which is useful for comparisons, testing, standardized output, or further processing. shuffle() randomizes the sequence, reverse() simply reverses the current order without sorting by value, and array_distinct() removes duplicates but does not primarily perform sorting. sort_array() affects the nested collection within each DataFrame row and should not be confused with orderBy(), which sorts DataFrame rows globally.

Question 334.

A developer wants to randomly rearrange the elements inside each array while leaving the number of DataFrame rows unchanged. Which function should be used?

  1. rand()
    2. shuffle()
    3. sample()
    4. repartition()

Correct Answer: 2. shuffle()

Explanation:

shuffle() randomly rearranges the elements inside an array and returns another array. It does not randomly reorder DataFrame rows or modify the partition count. rand() creates pseudo-random numeric values, sample() selects a probabilistic subset of DataFrame rows, and repartition() changes physical data distribution. shuffle() is therefore the correct nested-data function when array order should be randomized while keeping each collection within its original record.

Question 335.

Which Spark SQL function removes duplicate values from an existing array?

  1. dropDuplicates()
    2. distinct()
    3. array_distinct()
    4. collect_set()

Correct Answer: 3. array_distinct()

Explanation:

array_distinct() removes duplicate elements inside an array and returns the resulting array. dropDuplicates() and distinct() operate at the DataFrame row level rather than within a nested collection. collect_set() can aggregate unique values from multiple rows into an array, but it is not used to directly deduplicate a pre-existing array column. array_distinct() is therefore the best fit when one row already contains an array such as repeated tags or IDs and duplicate elements should be eliminated while preserving the row structure.

Question 336.

Which Spark SQL function concatenates two arrays while preserving duplicate elements rather than applying set semantics?

  1. array_union()
    2. array_intersect()
    3. flatten()
    4. concat()

Correct Answer: 4. concat()

Explanation:

concat() can combine compatible array expressions by appending the elements of one array to another while preserving duplicates. array_union() instead performs set-like union semantics and removes duplicate values. array_intersect() returns shared elements, and flatten() collapses one level of nested arrays. concat() is therefore the appropriate function when array contents should simply be appended in sequence. The same function can also concatenate strings, so developers should interpret its behavior according to the input data types.

Question 337.

Which Spark SQL function can return only a selected contiguous section of an array?

  1. slice()
    2. substring()
    3. array_position()
    4. element_at()

Correct Answer: 1. slice()

Explanation:

slice() returns part of an array based on a starting position and a specified length. It is useful when only a section of an ordered collection should be retained, such as a subset of top results, the latest several items, or a fixed range of nested values. substring() performs similar position-based extraction on strings, array_position() locates an element, and element_at() retrieves one specific array element. slice() allows the collection to remain nested and avoids generating additional rows.

Question 338.

Which Spark SQL function is best for generating an array of sequential date values between a starting and ending date?

  1. array_repeat()
    2. sequence()
    3. range()
    4. collect_list()

Correct Answer: 2. sequence()

Explanation:

sequence() can generate an array containing a progression from a starting value to an ending value and supports suitable numeric and temporal types. For dates, it can be used to create a calendar sequence that can later be exploded into one row per date if required. array_repeat() duplicates one value, range() typically generates a DataFrame rather than a nested per-row array, and collect_list() aggregates existing row values. sequence() is therefore a powerful function for calendar generation and interval expansion.

Question 339.

Which Spark SQL function can repeat a single value multiple times inside an array?

  1. repeat()
    2. sequence()
    3. array_repeat()
    4. collect_list()

Correct Answer: 3. array_repeat()

Explanation:

array_repeat() produces an array containing a specified value repeated a specified number of times. This should not be confused with repeat(), which generally repeats a string. sequence() creates progressing values from a start to a stop, while collect_list() gathers values from multiple rows. array_repeat() is useful when constructing fixed-length nested structures, generating test arrays, or populating placeholder collection values directly inside a DataFrame transformation.

Question 340.

Which Spark SQL function returns the elements of an array in the opposite order from their current sequence?

  1. sort_array()
    2. shuffle()
    3. array_position()
    4. reverse()

Correct Answer: 4. reverse()

Explanation:

reverse() reverses the current order of an array’s elements without sorting them according to their values. If the array is [1, 3, 2], reverse() produces [2, 3, 1]. sort_array() would instead order the values according to sort semantics, and shuffle() would randomize them. array_position() locates the position of a selected value. reverse() is therefore appropriate when sequence order matters and the developer simply wants the same elements presented or processed in the opposite direction.