Home / ML Associate practice test / Data Processing

Free · 8 questions with explanations

Data Processing: Databricks Machine Learning Associate Practice Questions

Exam-style questions on Data Processing. Pick your answer, then open the explanation to see why it's right — and why the other options are wrong.

1 Data Processing

A data scientist applies PySpark MLlib's OneHotEncoder to a region column that has 5 distinct category values (indices 0–4 after StringIndexer). The encoder uses the default dropLast=True setting. What is the dimensionality of the resulting sparse vector, and what is the purpose of the dropLast=True default?

  1. AThe output vector has 5 elements (one per category), and dropLast=True is set by default to guarantee that every category is represented by its own dedicated binary indicator column, ensuring complete and fully explicit encoding for all 5 categories with no information loss in downstream model training
  2. BThe output vector has 4 elements (one fewer than the total number of categories), and dropLast=True is the default behavior to prevent linear dependence among the encoded columns — since the 5 indicators would always sum to exactly 1.0, the last column is mathematically redundant and dropping it avoids the dummy variable trap (perfect multicollinearity) in linear models such as logistic regression and linear SVM
  3. CThe output vector has 6 elements (one more than the number of categories), and the additional 6th element serves as a built-in bias term that dropLast=True activates to allow linear models to fit an intercept implicitly through the encoded feature space, eliminating the need for a separate fitIntercept parameter in the downstream model
  4. DThe output vector has 4 elements because dropLast=True removes the most frequently occurring category first in order to prevent the model from overfitting to the dominant class — StringIndexer assigns index 0 to the most frequent label, and dropping it by default forces the model to learn from the less common regions which carry more discriminative signal
  5. EThe output vector has 5 elements when dropLast=True is active because this default setting adds one extra null-indicator element to the standard 4-element encoding, allowing the model to explicitly represent the case where a row has no valid region value — the extra element is used for missing value representation during inference on new data
Show answer & explanation

Correct answer: B

WHY B: With 5 distinct categories and dropLast=True, the resulting vector has 4 elements. The 5th category is implicitly represented by the all-zeros vector (when all four indicators are 0, the row must belong to the dropped last category). This default is specifically designed to prevent the dummy variable trap — a form of perfect multicollinearity where the sum of all indicators always equals 1, making one column perfectly predictable from the others, which causes problems in linear models. WHY NOT A: The output has 4 elements, not 5; dropLast=True specifically reduces the vector dimension by one, it does not preserve all 5 categories. WHY NOT C: OHE does not add a bias term; intercept handling in linear models is controlled by the fitIntercept parameter in the model itself, not by OHE. WHY NOT D: dropLast=True drops the last category by index order (the least frequent category, since StringIndexer sorts by descending frequency, placing the most common at index 0 and the least common at the last index) — it is not dropping the most frequent category. WHY NOT E: The vector has 4 elements, not 5; the all-zeros vector represents the dropped last category, not a missing value indicator; missing value handling is controlled by the handleInvalid parameter.

2 Data Processing

A data scientist is preparing features for a linear regression model. The dataset contains a subscription_tier column with 3 nominal categories: 'Bronze', 'Silver', 'Gold'. There is no natural ordering among these tiers for this model's purpose. Which encoding strategy is most appropriate, and why?

  1. AUse ordinal integer encoding by assigning Bronze=1, Silver=2, Gold=3 — linear regression requires ordinal numeric inputs and the model will correctly interpret the 1/2/3 values as evenly spaced ordinal levels, learning a single coefficient that scales linearly with tier rank without introducing redundant indicator columns
  2. BSkip encoding entirely and pass the raw string column directly to the linear regression model — Spark MLlib's LinearRegression transformer automatically converts string feature values to numeric TF-IDF weighted embeddings before fitting, so manual encoding is unnecessary for nominal categorical columns
  3. CUse target encoding (also called mean encoding) because it always outperforms one-hot encoding for linear models on nominal categorical features with fewer than 10 categories — it captures the label signal in a compact single numeric column and eliminates the dimensionality expansion introduced by creating multiple binary indicator columns
  4. DUse StringIndexer alone (without a subsequent OneHotEncoder step) — for linear regression, the integer indices 0, 1, and 2 assigned by StringIndexer are sufficiently close in magnitude to binary indicator values that the model can learn separate effects for each tier without the full OHE transformation, which would be redundant given the small number of categories
  5. EUse one-hot encoding via StringIndexer followed by OneHotEncoder with dropLast=True, producing 2 binary indicator columns — for a linear regression model, one-hot encoding is appropriate because treating the unordered nominal categories as integers (0, 1, 2) would impose a false linear ordering (Gold > Silver > Bronze) that the model would incorrectly learn as a meaningful numeric gradient in the coefficient
Show answer & explanation

Correct answer: E

WHY E: One-hot encoding is the appropriate strategy for nominal categorical features in linear regression. Using integer encoding (0, 1, 2) would cause the model to learn a single slope coefficient implying that moving from Bronze to Silver to Gold is a uniform linear increase — a false ordinal assumption. OHE eliminates this by creating independent binary indicators, allowing the model to learn a separate additive effect for each tier. With 3 categories and dropLast=True, 2 binary columns are produced (sufficient to represent all 3 tiers). WHY NOT A: Assigning integers 1/2/3 to nominal categories introduces a false ordinal relationship; linear regression will treat the numeric gap between categories as a meaningful slope, which is incorrect for unordered nominal data. WHY NOT B: Spark MLlib's LinearRegression requires all features to be numeric; passing a string column directly raises a runtime error — there is no internal string-to-embedding conversion. WHY NOT C: While target encoding can be effective, the blanket claim that it 'always outperforms' OHE for linear models is false; target encoding can introduce data leakage if not carefully implemented with cross-fitting, and OHE is the standard reliable approach. WHY NOT D: Using only StringIndexer for a linear model introduces the same false ordinal assumption problem; even with 3 categories, the model will treat the integer codes 0, 1, and 2 as numerically meaningful, learning incorrect coefficient magnitudes.

3 Data Processing

A data scientist wants to understand the relationship between age (continuous, range 18–85) and annual_income (continuous, range $20K–$500K). They want to visualize the relationship and also quantify the strength and direction of any linear association. Which approach is most appropriate?

  1. ACreate separate histograms for age and annual_income using different colors on the same plot, and compute the mean and standard deviation of each feature independently — overlapping histograms reveal the degree of linear correlation between two continuous variables by showing where their individual distributions intersect
  2. BApply a Chi-Square test of independence after binning both age and annual_income into decile groups — binning both continuous variables and applying the Chi-Square test is the most statistically rigorous method for quantifying linear correlation between two continuous numeric features
  3. CCreate a scatter plot with age on the x-axis and annual_income on the y-axis using df.plot.scatter(x='age', y='annual_income'), and compute the Pearson correlation coefficient with df.stat.corr('age', 'annual_income') — the scatter plot reveals the visual pattern and direction of the relationship while the Pearson coefficient quantifies its strength
  4. DApply a two-sample t-test comparing the mean annual_income between observations where age is above the median versus below the median — splitting one continuous variable at the median and applying a t-test is a complete and rigorous method for measuring the linear relationship between two continuous numeric features
  5. ECreate a box plot of annual_income grouped by quartile bins of age — grouping the income values into four age buckets and displaying box plots for each bucket provides a complete measure of the linear correlation between the two continuous variables without requiring any additional statistical tests
Show answer & explanation

Correct answer: C

WHY C: A scatter plot is the standard bivariate visualization for two continuous variables — it reveals the pattern, direction, and strength of the relationship visually. Pearson correlation (df.stat.corr()) provides the numeric coefficient ranging from -1 to +1 quantifying the linear association. Together these two tools are the correct and complete approach for comparing two continuous features. WHY NOT A: Overlapping histograms show individual univariate distributions for each variable separately but cannot reveal the relationship or correlation between the two variables; they are not bivariate analysis tools. WHY NOT B: Chi-Square tests are designed for categorical data; binning continuous variables discards information and the result measures general statistical dependence, not specifically linear correlation — Pearson correlation is the correct test for linear continuous-to-continuous relationships. WHY NOT D: A median-split t-test compares group means but loses all information about how income varies across the full continuous range of age values; it cannot capture linear trends and is a weaker analytical approach. WHY NOT E: While box plots grouped by age bins provide useful directional insight, binning age discards information and the result does not measure linear correlation; it cannot replace the Pearson coefficient for quantifying linear association.

4 Data Processing

A data analyst wants to determine whether there is a statistically significant association between two nominal categorical variables: customer_segment (values: Premium, Standard, Basic) and preferred_channel (values: Mobile, Web, In-Store). Which approach is most appropriate for comparing these two categorical features?

  1. ABuild a contingency table using df.stat.crosstab('customer_segment', 'preferred_channel') to compute the frequency of each segment-channel combination, then apply a Chi-Square test of independence — this is the standard method for testing whether two nominal categorical variables are statistically associated with each other
  2. BCompute the Pearson correlation coefficient between numerically label-encoded values of customer_segment and preferred_channel using df.stat.corr() — Pearson correlation is the standard statistical measure for quantifying the strength and direction of the linear relationship between two categorical variables when they are represented as integers
  3. CCreate a scatter plot with customer_segment on the x-axis and preferred_channel on the y-axis — scatter plots reveal the joint distribution and strength of association between two nominal categorical variables and are the most commonly used comparison method in exploratory data analysis on string-valued features
  4. DCompute the mean of a label-encoded preferred_channel grouped by customer_segment using df.groupBy('customer_segment').agg(F.mean('preferred_channel')) — averaging the label-encoded channel values per segment gives an interpretable numeric measure of which channel each segment prefers most
  5. EApply an independent samples t-test comparing the distribution of a numerically encoded preferred_channel across different levels of customer_segment — the two-sample t-test is the appropriate hypothesis test for comparing two nominal categorical distributions when each category group can be treated as an independent numeric sample
Show answer & explanation

Correct answer: A

WHY A: Cross-tabulation followed by a Chi-Square test of independence is the statistically correct method for testing whether two nominal categorical variables are related. PySpark's df.stat.crosstab() efficiently computes the contingency table, and PySpark's ChiSquareTest class can test for independence. WHY NOT B: Pearson correlation measures linear relationships between continuous numeric variables; applying it to arbitrarily label-encoded categories produces results that depend on the arbitrary encoding order and carry no statistical meaning. WHY NOT C: Scatter plots are designed for two continuous variables; plotting two categorical axes creates a grid of overlapping points that reveals no distributional differences or statistical association. WHY NOT D: Averaging a label-encoded nominal variable is statistically invalid because integer codes assigned to categories have no inherent numeric meaning or ordering — the result is uninterpretable. WHY NOT E: The t-test is designed to compare the means of a continuous numeric variable between two groups, not to test association between two nominal categorical variables; applying it here would be methodologically incorrect.

5 Data Processing

A machine learning engineer is performing exploratory data analysis on a house_price column, which is a continuous numeric feature. They want to understand the shape of its distribution — specifically whether it is approximately normal, right-skewed, or multimodal. Which visualization is most appropriate?

  1. AA bar chart of the top 20 most frequent house_price values — converting the continuous price values into a ranked-frequency bar chart is the most accurate way to detect skewness or multimodality because it highlights the most common price points as discrete categories
  2. BA pie chart showing the proportion of each unique house_price value out of the total dataset — pie charts reveal the relative frequency of each price point and scale well to thousands of unique continuous values, making them especially effective for visualizing the full distribution of high-cardinality numeric features
  3. CA scatter plot of house_price against the row index in the DataFrame — plotting price values against their sequential index reveals the shape and spread of the continuous distribution and is the most appropriate univariate visualization tool for detecting skewness or multimodality
  4. DA box-and-whisker plot only, without any histogram complement — box plots capture the median, interquartile range, and whisker bounds of the distribution and are the single most complete tool for detecting multimodality and the full shape of any continuous feature distribution
  5. EA histogram that groups house_price values into bins and shows the count of observations per bin, created using df.plot.hist(column='house_price', bins=30) in PySpark native plotting, or by converting to Pandas and using df_pd['house_price'].hist(bins=30) — histograms directly visualize the shape of the distribution by aggregating continuous values into adjacent frequency bins, making it straightforward to identify skewness, outlier tails, and multiple modes
Show answer & explanation

Correct answer: E

WHY E: Histograms are the standard and most appropriate visualization for continuous features. By dividing the value range into bins and showing the count per bin, a histogram directly reveals whether the distribution is symmetric, skewed, or multimodal. PySpark's native .plot.hist() (available in Databricks Runtime 17.0+ / Spark 4.0+) and pandas .hist() both support this. WHY NOT A: A bar chart of the top 20 most frequent values ignores the vast majority of the distribution and cannot reveal overall shape, skewness, or multimodality. WHY NOT B: Pie charts are inappropriate for high-cardinality continuous data; with thousands of unique price values, every slice is invisibly thin and no distributional insight can be gained. WHY NOT C: Scatter plot vs. row index reflects data entry order, not the statistical distribution shape; it cannot reliably detect skewness or modes. WHY NOT D: While box plots usefully summarize the five-number summary and flag outliers, they cannot detect multimodality (multiple peaks) and do not show the full shape of the distribution — a histogram is necessary for that purpose.

6 Data Processing

A data scientist is exploring a dataset with a product_category column containing 8 distinct nominal string values. She wants to understand the frequency distribution of each category. Which visualization type is most appropriate for this column?

  1. AA line chart connecting the frequency count of each category in alphabetical order — line charts reveal trends in categorical frequencies over ordered discrete categories and are the standard approach for visualizing nominal categorical frequency distributions in exploratory data analysis
  2. BA scatter plot with product_category on the x-axis and row index on the y-axis — scatter plots show where each category appears across the dataset rows, revealing unequal frequency distributions across nominal categories through the density of plotted points
  3. CA histogram with automatic bin selection — histograms automatically detect nominal string columns and group them into equal-width bins by alphabetical ordering, making them the most versatile chart type for visualizing both categorical and continuous feature distributions in Databricks
  4. DA bar chart showing the count of each category on the y-axis and category labels on the x-axis — created using Databricks built-in display() with bar chart visualization, or by calling df.groupBy('product_category').count().toPandas().plot.bar(), this chart directly shows each category's frequency as a proportional bar height, making it easy to compare category prevalences at a glance
  5. EA box plot grouped by product_category — box plots display the five-number summary (min, Q1, median, Q3, max) for each category group and are the most appropriate chart type for understanding the frequency count distribution of a nominal categorical column in a Databricks notebook
Show answer & explanation

Correct answer: D

WHY D: A bar chart is the standard and most appropriate visualization for nominal categorical data, showing the frequency count of each category as a proportional bar. Both Databricks' display() function and PySpark's native .plot.bar() support this directly after a groupBy().count() aggregation. WHY NOT A: Line charts imply a continuous trend between adjacent points; connecting nominal categories with a line incorrectly suggests an ordered relationship between them. WHY NOT B: Scatter plots of category vs. row index produce a scattered cloud that does not summarize frequencies and cannot clearly communicate the relative prevalence of each category. WHY NOT C: Histograms divide a numeric range into bins; they cannot process string columns and are designed for continuous numeric distributions, not nominal categorical data. WHY NOT E: Box plots summarize the distribution of a numeric variable across groups; they are not used to show frequency counts of a categorical column itself.

7 Data Processing

A data scientist is building a PySpark MLlib preprocessing Pipeline for a dataset with three columns: annual_salary (double), years_experience (integer), and job_title (string). They plan to use PySpark's Imputer class to handle missing values across all three columns in one step. What is an important limitation they must account for?

  1. AThe PySpark Imputer class only supports the 'mean' strategy and does not implement 'median' or 'mode' imputation — for any strategy other than mean, missing values must be manually computed using approxQuantile() or groupBy().agg() and applied with fillna() before constructing the Pipeline
  2. BThe PySpark Imputer class requires that all missing values be encoded as Python None objects and will silently skip NaN floating-point values in double columns — all NaN values in numeric columns must first be converted to None using a custom UDF or F.when() expression before passing the DataFrame to the Imputer
  3. CThe PySpark Imputer class only supports numeric data types such as integer and double — it does not support string or categorical columns, and the documentation explicitly warns that attempting to impute a categorical feature may produce incorrect imputed values; the job_title string column must be handled separately with a different imputation strategy before being passed to the Imputer
  4. DThe PySpark Imputer class cannot be included as a stage inside a PySpark MLlib Pipeline object — it is a standalone utility that only functions correctly when called individually outside of a Pipeline, and placing it in a Pipeline stages list will raise an incompatible-stage runtime exception during Pipeline.fit()
  5. EThe PySpark Imputer class automatically casts all double columns to integer before performing imputation if the computed surrogate value is a whole number — this automatic type casting can silently truncate decimal salary values to integers, resulting in loss of precision in the annual_salary column after imputation
Show answer & explanation

Correct answer: C

WHY C: The PySpark Imputer documentation explicitly states: 'The imputer does not support categorical features and possibly creates incorrect values for a categorical feature.' The job_title string column cannot be passed to the Imputer — it must be handled separately (e.g., filling with the most frequent title using a custom approach or SQL). WHY NOT A: PySpark's Imputer fully supports 'mean', 'median', and 'mode' strategies — all three are documented and implemented. WHY NOT B: By default, the PySpark Imputer treats both NaN and null/None as missing values for float/double columns (the missingValue parameter defaults to NaN); it does not silently skip NaN values. WHY NOT D: The Imputer is a standard MLlib Estimator and is fully compatible with PySpark Pipeline.stages — it is commonly chained with StringIndexer, OneHotEncoder, and VectorAssembler in preprocessing pipelines. WHY NOT E: PySpark's Imputer preserves the original data type of input columns in the output; it does not automatically cast between double and integer types during imputation.

8 Data Processing

A data scientist wants to explore a large Databricks Spark DataFrame to understand numeric distributions, string column frequencies, and see inline visual histograms of value distributions — all in a single command without writing extra plotting code. Which tool best satisfies all of these requirements inside a Databricks notebook?

  1. ACall df.summary() and then pipe the resulting statistics DataFrame to matplotlib — df.summary() returns all statistical metrics and automatically renders distribution histograms for each column when its output is passed to matplotlib.show() in a Databricks notebook cell
  2. BCall df.describe() — this PySpark method computes count, mean, stddev, min, and max for numeric and string columns and also renders embedded histograms alongside each metric when executed in a Databricks notebook environment
  3. CUse df.stat.freqItems(['col1', 'col2']) — this method computes frequency distributions across all specified columns and automatically renders interactive visualizations for both numeric and categorical features directly in the Databricks notebook output cell
  4. DCall dbutils.data.summarize(df) — this Databricks utility generates a rich data profile for the entire DataFrame including summary statistics, distinct value counts, and inline histograms of value distributions for numeric, string, and date columns, all rendered directly in the notebook
  5. EConvert the Spark DataFrame to Pandas with .toPandas() and then call pandas_df.describe() — this produces the same comprehensive data profile as dbutils.data.summarize() and automatically renders inline histogram visualizations for each column without requiring additional imports or plotting code
Show answer & explanation

Correct answer: D

WHY D: dbutils.data.summarize(df) is the Databricks-specific tool designed to render a full interactive data profile — including summary statistics, null counts, distinct value distributions, and visual histograms for numeric, string, and date columns — in a single call within a Databricks notebook. WHY NOT A: df.summary() returns a statistical summary as a Spark DataFrame but does not automatically render histograms; additional plotting code is required. WHY NOT B: df.describe() provides basic statistics (count, mean, stddev, min, max) but does not render histograms. WHY NOT C: df.stat.freqItems() computes only the most frequent items for specified columns and does not produce a full visual data profile. WHY NOT E: While .toPandas().describe() provides numeric statistics, it does not render histograms automatically and risks out-of-memory errors on large Spark DataFrames; it cannot replicate the inline visual profile produced by dbutils.data.summarize().

Take the full ML Associate practice test →