Databricks Certified Associate Developer for Apache Spark Practice Test Questions and Exam Dumps Part15 Q281-300

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

 

Question 281.

Which Spark SQL function can return the position of the first occurrence of a value inside an array?

  1. array_position()
    2. element_at()
    3. array_contains()
    4. size()

Correct Answer: 1. array_position()

Explanation:

array_position() returns the position of the first occurrence of a specified value within an array. It is useful when a developer needs to determine where an element appears rather than simply whether it exists. element_at() retrieves an element by position or map key, array_contains() returns a Boolean indicating whether an array contains a given value, and size() returns the number of elements. array_position() is therefore the most appropriate function when position information is required as part of an array-processing workflow.

Question 282.

Which Spark SQL function can test whether an array contains a specified value?

  1. array_position()
    2. array_contains()
    3. arrays_overlap()
    4. element_at()

Correct Answer: 2. array_contains()

Explanation:

array_contains() returns a Boolean value indicating whether a specified element exists within an array. It is commonly used inside filter() expressions when rows should be retained based on collection membership. array_position() returns the element’s position rather than a simple Boolean, arrays_overlap() compares two arrays for any common element, and element_at() retrieves a specific element. array_contains() is particularly useful for nested data such as tags, permissions, categories, or product lists stored directly in an array column.

Question 283.

Which Spark SQL function can sort the elements of an array?

  1. array_union()
    2. flatten()
    3. sort_array()
    4. collect_list()

Correct Answer: 3. sort_array()

Explanation:

sort_array() orders the elements inside an array, typically in ascending order by default, with an option to control ordering direction. This can be useful when deterministic array ordering is required for comparisons, display, testing, or downstream transformations. array_union() merges distinct elements from two arrays, flatten() removes one nesting level, and collect_list() gathers values across multiple rows. sort_array() affects the collection inside each DataFrame row rather than globally sorting the DataFrame itself.

Question 284.

Which Spark SQL function can remove duplicate elements from an array?

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

Correct Answer: 4. array_distinct()

Explanation:

array_distinct() returns an array with duplicate elements removed. This is different from DataFrame.distinct(), which removes duplicate rows, and dropDuplicates(), which performs row-level deduplication using all or selected columns. collect_set() aggregates unique values across multiple rows into an array. array_distinct() operates directly on an existing array column within each row, making it appropriate when a nested collection itself contains repeated values that should be eliminated.

Question 285.

A developer wants to combine two arrays without removing duplicate elements. Which Spark SQL function is most appropriate?

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

Correct Answer: 1. concat()

Explanation:

concat() can concatenate compatible array expressions while preserving their elements in sequence, including duplicates. array_union() combines two arrays but removes duplicate values using set-like semantics. array_intersect() returns only shared elements, while collect_set() aggregates unique values across rows. concat() is therefore the appropriate choice when two arrays simply need to be appended together and duplicate values should remain. The same function can also concatenate compatible string expressions depending on how it is used.

Question 286.

Which Spark SQL function can return an array with the order of its elements reversed?

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

Correct Answer: 2. reverse()

Explanation:

reverse() can reverse the order of elements in an array expression. It can also reverse characters in a string depending on the input type. sort_array() orders values according to their natural ordering rather than simply reversing their current sequence. array_position() locates an element, and shuffle() randomizes an array rather than producing its exact reverse order. reverse() is useful when array order has meaning and a developer needs to process or present the elements in the opposite sequence.

Question 287.

Which Spark SQL function can randomly reorder the elements of an array?

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

Correct Answer: 3. shuffle()

Explanation:

shuffle() returns an array whose elements are placed in randomized order. This is useful for randomized testing, experimentation, or scenarios where collection order should be intentionally mixed. reverse() simply reverses the existing sequence, sort_array() orders values deterministically, and array_repeat() creates an array containing repeated values. Because shuffle() is randomized, developers should not rely on a particular output sequence unless the surrounding application logic explicitly manages reproducibility through supported controls.

Question 288.

Which Spark SQL function can return an array slice beginning at a specified position for a specified length?

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

Correct Answer: 4. slice()

Explanation:

slice() returns a portion of an array beginning at a specified position and continuing for a specified number of elements. It is similar conceptually to substring(), but it operates on arrays rather than strings. element_at() retrieves one specific element, while array_position() locates a value. slice() is useful when only a segment of an ordered array should be retained, such as the first several events, a subset of ranked scores, or a selected range from a nested collection.

Question 289.

Which Spark SQL function can create a map from two arrays, where one array supplies keys and the other supplies values?

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

Correct Answer: 1. map_from_arrays()

Explanation:

map_from_arrays() creates a map by pairing elements from one array of keys with corresponding elements from another array of values. This is useful when upstream processing has already created aligned key and value collections. create_map() builds a map from alternating key and value expressions directly, while map_keys() and map_values() extract content from existing map columns. map_from_arrays() therefore provides a convenient way to transform parallel arrays into a structured key-value representation inside a DataFrame row.

Question 290.

Which Spark SQL function can combine multiple maps into a single map?

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

Correct Answer: 2. map_concat()

Explanation:

map_concat() combines multiple map expressions into one map. It is useful when related key-value collections are stored separately and need to be merged for downstream processing. map_values() extracts map values, map_keys() extracts keys, and create_map() constructs a map from key and value expressions. Developers should consider how duplicate keys are handled under their Spark configuration because duplicate-map-key behavior can affect results. map_concat() is therefore the appropriate function when existing maps need to be merged rather than rebuilt manually.

Question 291.

Which Spark SQL function can produce an array containing all entries from a map as key-value structs?

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

Correct Answer: 3. map_entries()

Explanation:

map_entries() converts a map into an array of structs, where each struct represents a key-value pair. This can be especially useful when a map must be processed using array functions or transformed before being reconstructed. map_keys() and map_values() separate the keys and values into different arrays, while explode() can expand a map into rows but changes the DataFrame row count. map_entries() preserves the collection inside the row and provides a structured representation of every map entry.

Question 292.

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

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

Correct Answer: 4. map_from_entries()

Explanation:

map_from_entries() converts an array of key-value structs into a map column. It is effectively complementary to map_entries(), which converts a map into an array of key-value structs. create_map() builds maps from alternating expressions, while map_from_arrays() constructs a map from separate key and value arrays. map_from_entries() is particularly useful in nested transformations where a developer first manipulates map entries as an array and then wants to reconstruct the final map without expanding the data into separate DataFrame rows.

Question 293.

Which Spark SQL function can return the number of entries in a map column?

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

Correct Answer: 1. size()

Explanation:

size() returns the number of elements in an array or the number of key-value pairs in a map. It is useful for detecting empty maps, validating collection size, or filtering rows based on nested-data complexity. length() is primarily intended for strings and binary values, count() is an aggregation across rows or values, and map_keys() returns an array of keys instead of the map’s size. size() therefore provides the most direct way to measure the number of entries in a map expression.

Question 294.

Which Spark SQL function can return a map value associated with a specific key?

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

Correct Answer: 2. element_at()

Explanation:

element_at() can retrieve a map value by supplying the desired key. It can also retrieve array elements using positional semantics. map_values() returns all values as an array, map_entries() converts the map into key-value structs, and array_position() applies to arrays rather than map-key lookup. element_at() is useful when a specific known field is stored dynamically inside a map column and the developer wants to access that value without converting or exploding the entire map.

Question 295.

Which Spark SQL function converts a struct, array, or map into a JSON-formatted string?

  1. from_json()
    2. json_tuple()
    3. to_json()
    4. get_json_object()

Correct Answer: 3. to_json()

Explanation:

to_json() serializes supported complex Spark SQL values such as structs, arrays, or maps into JSON-formatted strings. It is useful when preparing structured data for message queues, APIs, files, or systems that expect JSON text. from_json() performs the reverse operation by parsing JSON according to a schema. json_tuple() and get_json_object() extract content from JSON strings. to_json() allows developers to maintain structured processing inside Spark and serialize the final nested representation only when textual JSON output is required.

Question 296.

Which Spark SQL function parses a JSON string into a structured value according to a supplied schema?

  1. to_json()
    2. schema_of_json()
    3. get_json_object()
    4. from_json()

Correct Answer: 4. from_json()

Explanation:

from_json() parses JSON text into a structured Spark SQL value using a supplied schema. Depending on the schema, the output may be a struct, array, or other nested representation that can be accessed using normal DataFrame expressions. to_json() performs serialization in the opposite direction. schema_of_json() can help derive schema information from JSON text, while get_json_object() extracts a selected path without fully parsing the data into a typed structure. from_json() is especially valuable when JSON data arrives embedded inside a string column from logs or streaming systems.

Question 297.

Which Spark SQL function can derive a schema string from a JSON-formatted string expression?

  1. schema_of_json()
    2. from_json()
    3. to_json()
    4. struct()

Correct Answer: 1. schema_of_json()

Explanation:

schema_of_json() can infer a schema representation from a JSON-formatted string expression. This can help developers inspect the expected structure of JSON content before applying from_json(). from_json() performs actual parsing according to a schema, to_json() converts structured values into JSON text, and struct() creates a nested struct from column expressions. Although explicit production schemas are often preferred for predictable data pipelines, schema_of_json() can be useful during development, exploration, or when constructing schema information from representative JSON samples.

Question 298.

Which Spark SQL function is useful when a developer wants to extract a single value from a JSON string using a path such as $.customer.id?

  1. from_json()
    2. get_json_object()
    3. to_json()
    4. schema_of_json()

Correct Answer: 2. get_json_object()

Explanation:

get_json_object() extracts a selected portion of a JSON string using a JSONPath-style expression. It can be convenient when only one or a few fields are needed and the developer does not want to parse the entire JSON string using a complete schema. from_json() is generally preferable when the JSON should become a strongly structured column for extensive downstream processing. to_json() serializes structured values, and schema_of_json() derives schema information. get_json_object() is therefore appropriate for lightweight path-based extraction.

Question 299.

Which Spark SQL function can extract multiple top-level fields from a JSON string and return them as separate columns?

  1. explode()
    2. map_entries()
    3. json_tuple()
    4. collect_list()

Correct Answer: 3. json_tuple()

Explanation:

json_tuple() can extract multiple specified fields from a JSON string in one operation and expose them as separate output columns. This can be convenient for relatively simple JSON structures when developers need several top-level values but do not require full schema-based parsing. explode() expands collections into rows, map_entries() converts maps to arrays of structs, and collect_list() aggregates values across rows. For complex nested JSON or strongly typed processing, from_json() with an explicit schema is usually more flexible and maintainable.

Question 300.

A developer has parsed JSON into a struct column named customer and wants to select the nested field name. Which expression is appropriate?

  1. col(“customer”).explode(“name”)
    2. map_values(“customer”)
    3. array_position(“customer”, “name”)
    4. col(“customer.name”)

Correct Answer: 4. col(“customer.name”)

Explanation:

Nested struct fields can be referenced using dot notation, so col(“customer.name”) accesses the name field inside the customer struct. This allows structured nested data to be manipulated without converting it back into text or exploding it into additional rows. explode() applies to arrays and maps rather than individual struct fields, map_values() applies to map columns, and array_position() applies to arrays. Dot notation is widely used in Spark when working with nested JSON, structs, and hierarchical schemas because it provides concise access to deeply nested fields.