Databricks Certified Associate Developer for Apache Spark Practice Test Questions and Exam Dumps Part2 Q21-40

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

 

Question 21.

Which DataFrame method is most appropriate for renaming an existing column?

  1. withColumnRenamed()
    2. repartition()
    3. persist()
    4. orderBy()

Correct Answer: 1. withColumnRenamed()

Explanation:

withColumnRenamed() is used to rename an existing DataFrame column while returning a new DataFrame. Because Spark DataFrames are immutable, the original DataFrame is not changed in place. repartition() changes the partitioning of data, persist() stores computed partitions for reuse, and orderBy() sorts rows. Renaming columns is commonly useful after joins, schema cleanup, or when preparing data to match downstream naming conventions.

Question 22.

Which function is commonly used to test whether a column contains a null value?

  1. between()
    2. isNull()
    3. alias()
    4. asc()

Correct Answer: 2. isNull()

Explanation:

isNull() creates a Boolean expression that evaluates whether a column value is null. It is often used inside filter() or when() expressions for data-quality checks and null handling. between() tests whether values fall within a range, alias() assigns an alternate name to a column expression, and asc() specifies ascending sort order. Proper null handling is important because standard equality comparisons do not treat null values like ordinary values.

Question 23.

Which Spark function is typically used to substitute a default value when a column is null?

  1. explode()
    2. broadcast()
    3. coalesce()
    4. rank()

Correct Answer: 3. coalesce()

Explanation:

The SQL expression function coalesce() returns the first non-null expression among its arguments. It is commonly used to replace null values with another column or a literal default. This function should not be confused with DataFrame.coalesce(), which reduces partitions. explode() expands nested collections, broadcast() supports broadcast joins, and rank() is a window function. Understanding the context in which coalesce is used is important because Spark uses the same name for two different concepts.

Question 24.

Which operation can sort a DataFrame by one or more columns?

  1. cache()
    2. union()
    3. dropDuplicates()
    4. orderBy()

Correct Answer: 4. orderBy()

Explanation:

orderBy() sorts DataFrame rows according to one or more specified columns or expressions. It can sort in ascending or descending order and may require a shuffle because Spark must establish the requested global ordering. cache() stores computed data for reuse, union() combines rows from compatible DataFrames, and dropDuplicates() removes duplicate records. Sorting should be used thoughtfully on large datasets because global ordering can be relatively expensive.

Question 25.

Which aggregation calculates the average numeric value within a group?

  1. avg()
    2. countDistinct()
    3. collect_set()
    4. first()

Correct Answer: 1. avg()

Explanation:

avg() computes the arithmetic mean of a numeric column and is frequently used with groupBy() for grouped aggregations. countDistinct() counts unique values, collect_set() creates an array of unique collected values, and first() returns the first value according to Spark’s aggregation semantics. avg() is appropriate when the goal is to calculate a mean value such as average transaction amount, average score, or average duration.

Question 26.

Which join keeps every row from the left DataFrame and matching rows from the right DataFrame?

  1. Inner join
    2. Left outer join
    3. Left anti join
    4. Cross join

Correct Answer: 2. Left outer join

Explanation:

A left outer join returns every row from the left DataFrame and matching rows from the right DataFrame. If no matching right-side record exists, the right-side columns are populated with null values. An inner join keeps only matching rows, a left anti join keeps unmatched left-side rows, and a cross join returns the Cartesian product. Left outer joins are commonly used when the left dataset must be preserved regardless of whether a match exists.

Question 27.

Which DataFrame method can remove one or more columns?

  1. selectExpr()
    2. cache()
    3. drop()
    4. describe()

Correct Answer: 3. drop()

Explanation:

drop() removes specified columns from a DataFrame and returns a new DataFrame without them. It is useful when temporary, redundant, or unwanted columns should be removed after transformations or joins. selectExpr() selects columns using SQL expressions, cache() stores data for reuse, and describe() calculates summary statistics. Since DataFrames are immutable, drop() creates a new logical DataFrame rather than changing the original object.

Question 28.

Which Spark action returns the first n rows to the driver?

  1. repartition()
    2. persist()
    3. filter()
    4. take(n)

Correct Answer: 4. take(n)

Explanation:

take(n) is an action that returns up to n rows from a distributed dataset to the driver. It is useful for quickly inspecting a small number of records without collecting the entire dataset. repartition(), persist(), and filter() are not actions that return a specified number of rows. take() should still be used with awareness that it triggers Spark computation, but it is generally safer than collect() for small exploratory checks.

Question 29.

Which function is useful for creating conditional values in a new column?

  1. when()
    2. explode()
    3. monotonically_increasing_id()
    4. array()

Correct Answer: 1. when()

Explanation:

when() creates conditional expressions similar to SQL CASE WHEN logic. It is often combined with otherwise() to produce values based on one or more conditions. For example, a numeric score could be classified into categories depending on thresholds. explode() expands arrays or maps, monotonically_increasing_id() generates unique increasing identifiers, and array() creates an array expression. when() is therefore the appropriate function for conditional column logic.

Question 30.

Which DataFrame method is commonly used to view summary statistics such as count, mean, standard deviation, minimum, and maximum?

  1. explain()
    2. describe()
    3. checkpoint()
    4. alias()

Correct Answer: 2. describe()

Explanation:

describe() produces basic summary statistics for selected numeric and string columns, including count, mean, standard deviation, minimum, and maximum where applicable. explain() displays query plans, checkpoint() truncates lineage by materializing data to checkpoint storage, and alias() assigns alternate names. describe() is useful for quick exploratory analysis, although more detailed statistics may require additional aggregation functions or summary() depending on the use case.

Question 31.

Which function returns the number of distinct values in a column?

  1. first()
    2. max()
    3. countDistinct()
    4. collect_list()

Correct Answer: 3. countDistinct()

Explanation:

countDistinct() counts the number of unique non-null values represented by the specified expression or expressions. It is commonly used in aggregations to measure cardinality, such as counting unique customers or products. first() returns a first value, max() returns the maximum, and collect_list() gathers values into an array while retaining duplicates. countDistinct() is therefore the appropriate function when the goal is to count unique values.

Question 32.

Which DataFrame method allows SQL-style expressions to be used directly in a select operation?

  1. cache()
    2. union()
    3. repartition()
    4. selectExpr()

Correct Answer: 4. selectExpr()

Explanation:

selectExpr() allows SQL expressions to be supplied as strings when selecting or deriving columns. For example, an expression such as “price * quantity AS total” can be evaluated directly. cache() stores data for reuse, union() combines rows, and repartition() changes partitioning. selectExpr() can be convenient for developers who are comfortable with SQL syntax and want to express calculations or aliases compactly.

Question 33.

Which function can create an array column from multiple existing columns?

  1. array()
    2. explode()
    3. broadcast()
    4. count()

Correct Answer: 1. array()

Explanation:

array() combines multiple column expressions into a single array column. This can be useful when related values need to be stored together or passed into functions that operate on arrays. explode() performs the opposite style of operation by expanding array elements into separate rows. broadcast() supports join optimization, while count() performs aggregation. array() is therefore the appropriate choice for constructing array-valued columns from existing expressions.

Question 34.

Which method can write a DataFrame to Parquet format?

  1. df.collect()
    2. df.write.parquet()
    3. df.explain()
    4. df.describe()

Correct Answer: 2. df.write.parquet()

Explanation:

df.write.parquet() writes the contents of a DataFrame using the Parquet file format. Parquet is a columnar format that works efficiently with Spark for analytical workloads because column pruning and predicate pushdown can reduce unnecessary I/O. collect() returns data to the driver, explain() shows execution plans, and describe() produces summary statistics. DataFrameWriter also supports options such as write modes, partitioning, and configuration settings.

Question 35.

Which write mode replaces existing data at the target location?

  1. append
    2. ignore
    3. overwrite
    4. errorIfExists

Correct Answer: 3. overwrite

Explanation:

overwrite mode replaces existing data at the target destination according to the semantics of the data source. append adds new data, ignore leaves existing data unchanged when the destination already exists, and errorIfExists raises an error rather than replacing data. overwrite should be used carefully because it can remove or replace previously stored information. The exact effect can depend on the target format and partitioning configuration.

Question 36.

Which method is commonly used to read a JSON dataset into a Spark DataFrame?

  1. spark.sql.json()
    2. spark.loadJSON()
    3. spark.createJSON()
    4. spark.read.json()

Correct Answer: 4. spark.read.json()

Explanation:

spark.read.json() uses the SparkSession DataFrameReader to load JSON data into a DataFrame. Spark can infer a schema or use a schema supplied by the developer. The other listed methods are not the standard DataFrameReader syntax for reading JSON. Similar APIs exist for formats such as CSV, Parquet, and ORC. Providing an explicit schema can often improve reliability and performance compared with repeatedly inferring the structure.

Question 37.

What is one advantage of supplying an explicit schema when reading structured data?

  1. It can avoid schema inference and enforce expected data types
    2. It forces every DataFrame into one partition
    3. It automatically caches the input
    4. It eliminates all null values

Correct Answer: 1. It can avoid schema inference and enforce expected data types

Explanation:

Providing an explicit schema lets Spark know the expected column names and types without performing schema inference. This can improve consistency and may reduce extra work during data loading. It also helps catch or control type-related issues earlier in a pipeline. An explicit schema does not automatically cache data, remove nulls, or force a particular partition count. Schema design is an important part of building predictable and maintainable Spark applications.

Question 38.

Which function is commonly used to combine string columns into a single string?

  1. collect_set()
    2. concat()
    3. explode()
    4. struct()

Correct Answer: 2. concat()

Explanation:

concat() combines multiple string, binary, or compatible array expressions into one result. It is commonly used to join string columns together when constructing labels, identifiers, or formatted output. collect_set() aggregates unique values into an array, explode() expands arrays or maps into rows, and struct() creates a nested structure. When separators are needed between strings, concat_ws() may be more convenient than concat().

Question 39.

Which Spark function creates a struct column from multiple column expressions?

  1. lit()
    2. array()
    3. struct()
    4. broadcast()

Correct Answer: 3. struct()

Explanation:

struct() combines multiple expressions into a single struct-typed column. This is useful when related fields should be nested together, particularly when preparing complex schemas or JSON-like structures. array() creates an array rather than a named nested structure, lit() creates a constant value, and broadcast() is used for join optimization. Struct columns allow Spark applications to work naturally with nested records and hierarchical data.

Question 40.

Which optimization is commonly useful when one side of a join is small enough to fit comfortably in executor memory?

  1. Collect the large dataset to the driver
    2. Force both datasets into one partition
    3. Add a global sort before the join
    4. Broadcast the smaller dataset

Correct Answer: 4. Broadcast the smaller dataset

Explanation:

Broadcasting the smaller dataset can reduce shuffle costs by distributing a copy of that dataset to executors and allowing the larger dataset to remain partitioned. This can substantially improve join performance when the broadcast side is small enough. Collecting a large dataset to the driver risks memory problems, while forcing all data into one partition destroys parallelism. A global sort also introduces additional work and is not generally required for a broadcast join.