View Full Databricks Certified Associate Developer for Apache Spark Exam Dumps and Practice Test Dumps
Question 201.
A developer needs to calculate the Pearson correlation coefficient between two numeric columns in a Spark DataFrame. Which DataFrame method is most appropriate?
- approxQuantile()
2. corr()
3. covariance()
4. describe()
Correct Answer: 2. corr()
Explanation:
corr() calculates the Pearson correlation coefficient between two numeric columns in a DataFrame. The returned value typically ranges from -1 to 1. A value close to 1 indicates a strong positive linear relationship, a value close to -1 indicates a strong negative linear relationship, and a value near 0 suggests little linear correlation. approxQuantile() estimates quantiles, while describe() provides basic summary statistics. Correlation is particularly useful during exploratory analysis when developers want to understand whether changes in one numeric variable tend to correspond with changes in another.
Question 202.
Which DataFrame method is used to calculate covariance between two numeric columns?
- cov()
2. countDistinct()
3. summary()
4. rank()
Correct Answer: 1. cov()
Explanation:
cov() calculates the sample covariance between two numeric DataFrame columns. Covariance indicates whether two variables tend to move in the same or opposite directions. A positive covariance suggests that they generally increase or decrease together, while a negative covariance suggests an inverse relationship. Unlike correlation, covariance is not normalized to a fixed range, so its magnitude depends on the scale of the variables. countDistinct() counts unique values, summary() produces descriptive statistics, and rank() is used in window calculations. cov() is therefore the appropriate choice for covariance analysis.
Question 203.
A developer wants to estimate the 25th, 50th, and 75th percentiles of a numeric column without requiring exact percentile computation. Which method should be used?
- describe()
2. collect()
3. approxQuantile()
4. sample()
Correct Answer: 3. approxQuantile()
Explanation:
approxQuantile() is designed to calculate approximate quantiles efficiently for large datasets. The developer supplies the target probabilities, such as 0.25, 0.50, and 0.75, along with a relative error parameter that controls the tradeoff between precision and computation cost. This approach is especially useful in distributed systems because exact percentile computation can require expensive sorting and data movement. describe() provides basic statistics but does not offer arbitrary quantile control, collect() transfers data to the driver, and sample() selects a subset rather than computing percentiles.
Question 204.
Which Spark DataFrame method returns a frequency table for combinations of two specified columns?
- describe()
2. groupBy()
3. summary()
4. crosstab()
Correct Answer: 4. crosstab()
Explanation:
crosstab() computes a contingency table showing the frequency of combinations between two columns. It is useful for exploring relationships between categorical values, such as product category versus region or device type versus operating system. groupBy() can also be used to construct similar results manually, but crosstab() provides a convenient built-in method for two-dimensional frequency analysis. describe() and summary() focus on descriptive statistics rather than pairwise categorical frequency tables. Crosstabulation can be helpful when profiling categorical distributions before performing deeper analytics.
Question 205.
Which DataFrame method can calculate frequently occurring values for one or more columns?
- freqItems()
2. count()
3. distinct()
4. approxQuantile()
Correct Answer: 1. freqItems()
Explanation:
freqItems() identifies frequently occurring values in specified columns using an approximate algorithm. It can be helpful when profiling large datasets where developers want to identify dominant categories, repeated identifiers, or commonly occurring values without performing expensive exact frequency calculations. count() returns a row count, distinct() removes duplicate rows, and approxQuantile() estimates quantile values for numeric columns. Because freqItems() is approximate, its results should be interpreted according to the configured support threshold and the intended analytical purpose.
Question 206.
A developer wants to calculate the average, minimum, and maximum salary for each department. Which DataFrame pattern is most appropriate?
- select(“department”, “salary”)
2. groupBy(“department”).agg(avg(“salary”), min(“salary”), max(“salary”))
3. orderBy(“department”, “salary”)
4. repartition(“department”)
Correct Answer: 2. groupBy(“department”).agg(avg(“salary”), min(“salary”), max(“salary”))
Explanation:
groupBy(“department”).agg(…) groups records by department and applies multiple aggregation functions to each group. This is the standard DataFrame approach when several summary metrics are required for the same grouping key. select() merely projects columns, orderBy() sorts rows, and repartition() changes physical data distribution without calculating statistics. The agg() method can combine many aggregation expressions in one operation, making it useful for producing compact summary tables containing counts, averages, sums, minimums, maximums, or other grouped metrics.
Question 207.
Which Spark SQL function is most appropriate for counting unique customer IDs within each region?
- count()
2. size()
3. countDistinct()
4. length()
Correct Answer: 3. countDistinct()
Explanation:
countDistinct() calculates the number of unique values in a column or combination of expressions. When used after groupBy(“region”), it can count the number of distinct customer IDs within each region. count() counts rows or non-null values depending on usage but does not automatically remove duplicates. size() is used primarily for arrays and maps, while length() applies to strings or binary values. countDistinct() is therefore the correct function when repeated customer IDs must be counted only once within each group.
Question 208.
Which aggregation function can collect all values from a group into an array while preserving duplicates?
- collect_set()
2. array()
3. explode()
4. collect_list()
Correct Answer: 4. collect_list()
Explanation:
collect_list() gathers values from multiple rows into an array and preserves duplicate values. It is commonly combined with groupBy() when developers want to retain every value associated with a grouping key. collect_set() also aggregates values into an array but removes duplicates. array() constructs an array from expressions within a single row, while explode() expands an array into multiple rows. collect_list() is useful for building grouped histories, transaction lists, event sequences, or other nested structures where repeated values should remain visible.
Question 209.
Which aggregation function collects only unique values from a group into an array?
- collect_set()
2. collect_list()
3. array_union()
4. distinct()
Correct Answer: 1. collect_set()
Explanation:
collect_set() aggregates values from multiple rows into an array while removing duplicate values. It is useful when developers need the unique categories, tags, identifiers, or states associated with each group. collect_list() preserves duplicates, array_union() combines two existing arrays, and distinct() removes duplicate rows at the DataFrame level rather than collecting group values into arrays. Because the resulting order from collect_set() should not generally be assumed to be deterministic, sorting may be needed if a predictable array order is required later.
Question 210.
Which DataFrame method can rename a single existing column without rebuilding the entire select list?
- alias()
2. withColumnRenamed()
3. cast()
4. selectExpr()
Correct Answer: 2. withColumnRenamed()
Explanation:
withColumnRenamed() returns a new DataFrame with the specified existing column renamed. It is convenient when only one or a few names need to change and the developer does not want to rewrite the entire projection. alias() is generally applied to expressions or DataFrames in specific contexts, while selectExpr() can rename columns through SQL-style expressions but requires a projection. cast() changes a data type rather than a name. Since DataFrames are immutable, withColumnRenamed() returns a new logical DataFrame instead of modifying the original object.
Question 211.
A developer wants to replace the existing column price with a calculated value equal to price * 1.10. Which method is most appropriate?
- drop(“price”)
2. select(“price”)
3. withColumn(“price”, col(“price”) * 1.10)
4. groupBy(“price”)
Correct Answer: 3. withColumn(“price”, col(“price”) * 1.10)
Explanation:
withColumn() can add a new column or replace an existing column when the supplied name already exists. In this example, using withColumn(“price”, col(“price”) * 1.10) creates a new DataFrame in which price contains the calculated value. drop() removes the column, select() simply projects it, and groupBy() groups rows for aggregation. This pattern is widely used for derived values, normalization, conditional logic, type conversion, and other column-level transformations in Spark applications.
Question 212.
Which Spark SQL function should a developer use to create conditional logic similar to SQL CASE WHEN?
- lit()
2. coalesce()
3. expr()
4. when()
Correct Answer: 4. when()
Explanation:
when() provides conditional logic similar to SQL CASE WHEN and is typically combined with otherwise() for a default result. Multiple when() calls can be chained to represent multiple conditions. For example, a developer can classify sales values into low, medium, and high categories based on numeric thresholds. lit() creates constant values, coalesce() returns the first non-null expression, and expr() evaluates SQL expressions more generally. when() is therefore the most direct DataFrame function for building readable conditional transformations.
Question 213.
Which Spark SQL function is useful for specifying a fixed literal value inside a DataFrame expression?
- lit()
2. col()
3. expr()
4. alias()
Correct Answer: 1. lit()
Explanation:
lit() creates a literal column expression from a fixed value such as a string, integer, date-like value, or Boolean. It is commonly used inside withColumn(), select(), when(), and other DataFrame expressions. For example, withColumn(“source”, lit(“mobile”)) assigns the same value to every row. col() references an existing column, expr() evaluates SQL-style expressions, and alias() assigns a name to an expression. lit() is therefore the appropriate function when a constant must participate in distributed Spark transformations.
Question 214.
Which Spark SQL function provides a convenient way to reference a DataFrame column by name inside an expression?
- lit()
2. col()
3. struct()
4. broadcast()
Correct Answer: 2. col()
Explanation:
col() creates a Column expression referencing a named DataFrame column. It is frequently used inside transformations such as select(), filter(), withColumn(), and aggregation expressions. For example, col(“salary”) * 1.05 represents a calculated expression based on the salary column. lit() creates constants, struct() combines several expressions into a nested struct, and broadcast() is associated with join optimization. col() makes expression construction explicit and is particularly useful when column names are stored dynamically or passed into reusable functions.
Question 215.
Which Spark SQL function allows a developer to express a transformation using SQL expression syntax inside the DataFrame API?
- select()
2. withColumnRenamed()
3. expr()
4. cache()
Correct Answer: 3. expr()
Explanation:
expr() parses a string containing a SQL expression and converts it into a Spark Column expression. This can make complex transformations more concise, especially for developers who are comfortable with SQL syntax. For example, expr(“price * quantity AS revenue”) can represent a calculated expression. select() performs projection, withColumnRenamed() renames an existing field, and cache() persists computed data. expr() is useful for mixing SQL-style logic with DataFrame transformations without creating a separate SQL query.
Question 216.
Which DataFrame method lets developers specify multiple SQL-style expressions as strings in a projection?
- withColumn()
2. filter()
3. groupBy()
4. selectExpr()
Correct Answer: 4. selectExpr()
Explanation:
selectExpr() accepts SQL-style expressions as strings and evaluates them as part of a DataFrame projection. It can perform calculations, casts, aliases, and conditional expressions while selecting output columns. For example, a developer might use selectExpr(“customer_id”, “price * quantity AS revenue”). withColumn() adds or replaces one column at a time, filter() removes rows that do not satisfy a condition, and groupBy() prepares data for aggregation. selectExpr() is especially convenient when several SQL-like expressions need to be applied together.
Question 217.
Which DataFrame method is most appropriate for removing an unwanted column from a dataset?
- drop()
2. filter()
3. distinct()
4. unpersist()
Correct Answer: 1. drop()
Explanation:
drop() removes one or more specified columns and returns a new DataFrame without those fields. It is commonly used after joins or intermediate calculations when temporary, duplicate, or unnecessary columns should not remain in the final schema. filter() removes rows rather than columns, distinct() removes duplicate rows, and unpersist() releases cached data. Since DataFrames are immutable, drop() constructs a new logical DataFrame rather than changing the existing DataFrame in place.
Question 218.
A DataFrame contains duplicate records, and every column should be considered when identifying duplicates. Which method is most appropriate?
- drop(“duplicates”)
2. distinct()
3. collect_set()
4. groupBy()
Correct Answer: 2. distinct()
Explanation:
distinct() returns only unique rows by considering the complete set of selected columns in each record. It corresponds conceptually to SQL SELECT DISTINCT. When duplicates should be determined using only a subset of columns, dropDuplicates() with a column list can be more appropriate. collect_set() aggregates values into arrays, and groupBy() groups rows but does not by itself remove duplicates. Because distinct() may require comparing records across partitions, it can cause a shuffle and should be used thoughtfully on very large datasets.
Question 219.
Which method is best when duplicate detection should be based only on customer_id and order_date, while other columns may differ?
- distinct()
2. drop()
3. dropDuplicates([“customer_id”, “order_date”])
4. union()
Correct Answer: 3. dropDuplicates([“customer_id”, “order_date”])
Explanation:
dropDuplicates() can accept a subset of columns that determines which records are considered duplicates. In this case, rows sharing the same customer_id and order_date can be treated as duplicates even when other fields differ. distinct() compares complete rows, drop() removes columns, and union() combines datasets vertically. Subset-based deduplication is common in data-engineering pipelines where a business key or composite key determines record uniqueness rather than the entire row content.
Question 220.
A developer wants to append the rows of one DataFrame to another compatible DataFrame without automatically removing duplicates. Which operation should be used?
- join()
2. crossJoin()
3. merge()
4. union()
Correct Answer: 4. union()
Explanation:
union() combines two compatible DataFrames vertically by appending rows from one dataset to the other. It does not automatically remove duplicate records, so additional distinct() or dropDuplicates() logic is required if deduplication is needed. join() combines datasets horizontally based on matching conditions, and crossJoin() creates all possible row combinations. merge() is not the standard Spark DataFrame operation for simple row appending. union() is commonly used when consolidating files, time periods, partitions, or similarly structured data sources into a single logical dataset.