Courseiva

Databricks-DE-Pro Data Transformation, Cleansing, Quality Practice Question

A data engineer is using PySpark to cleanse a DataFrame containing customer addresses. The 'zip_code' column has some values with leading zeros that were stripped during CSV ingestion. The engineer needs to restore all zip codes to a fixed 5-character length by padding with leading zeros. Which function should be used?

⚠ Common exam trap

Watch out — candidates often confuse lpad with rpad, or assuming that zip codes are numeric and can be formatted as integers.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

lpad(zip_code, 5, '0')

The lpad function is designed to pad strings on the left to a specified length. It correctly restores leading zeros for zip codes that lost them during ingestion, ensuring all values are exactly 5 characters. Other functions either pad on the wrong side, assume numeric types, or do not achieve the required fixed length.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    regexp_replace(zip_code, '^', '0')

    Why it's wrong here

    This would prepend a single zero to every zip code, regardless of its current length. It does not ensure a fixed length of 5. For example, '123' becomes '0123' (length 4), not '00123'. It also would add a zero to already 5-character zip codes, making them 6 characters. This does not solve the problem.

  • ✗

    format_string('%05d', zip_code)

    Why it's wrong here

    format_string with '%05d' expects an integer argument, but zip_code is a string column. If the column contains non-numeric characters or is already a string, this will fail or produce unexpected results. Additionally, zip codes can start with zeros and may not be valid integers; using integer formatting is semantically wrong for identifiers.

  • ✗

    rpad(zip_code, 5, '0')

    Why it's wrong here

    rpad pads the right side of the string, which would append zeros to the end of the zip code. For example, '123' becomes '12300', which is incorrect for a zip code that should be '00123'. This would corrupt the data. rpad is used when trailing padding is needed, not for restoring leading zeros.

  • ✓

    lpad(zip_code, 5, '0')

    Why this is correct

    The lpad function pads the left side of a string with a specified character until it reaches the desired length. Using lpad(zip_code, 5, '0') ensures every zip code becomes exactly 5 characters, restoring leading zeros that were lost. This is the correct and idiomatic way to fix the issue in PySpark without complex logic.

About these practice questions

This Databricks-DE-Pro question is part of Courseiva's 267-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Databricks exam blueprint

This Databricks-DE-Pro practice question is part of Courseiva's free Databricks certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the Databricks-DE-Pro exam.