DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #169
Which of the following code blocks returns a DataFrame with column storeSlogan where single quotes in column storeSlogan in DataFrame storesDF have been replaced with double quotes? A sample of…
The correct answer is C. storesDF.withColumn("storeSlogan", regexp_replace(col("storeSlogan"), "'", "\"")). regexp_replace(column, pattern, replacement) requires exactly three arguments: the column expression, the regex pattern to find, and the replacement string. Option C - storesDF.withColumn('storeSlogan', regexp_replace(col('storeSlogan'), "'", '"')) - is correct because it…
Question
Which of the following code blocks returns a DataFrame with column storeSlogan where single quotes in column storeSlogan in DataFrame storesDF have been replaced with double quotes? A sample of DataFrame storesDF is below:
Exhibit
Options
- AstoresDF.withColumn("storeSlogan", col("storeSlogan").regexp_replace("'" """))
- BstoresDF.withColumn("storeSlogan", regexp_replace(col("storeSlogan"), "'"))
- CstoresDF.withColumn("storeSlogan", regexp_replace(col("storeSlogan"), "'", """))
- DstoresDF.withColumn("storeSlogan", regexp_replace("storeSlogan", "'", """))
- EstoresDF.withColumn("storeSlogan", regexp_extract(col("storeSlogan"), "'", """))
How the community answered
(46 responses)- A4% (2)
- B9% (4)
- C72% (33)
- D13% (6)
- E2% (1)
Explanation
regexp_replace(column, pattern, replacement) requires exactly three arguments: the column expression, the regex pattern to find, and the replacement string. Option C - storesDF.withColumn('storeSlogan', regexp_replace(col('storeSlogan'), "'", '"')) - is correct because it passes a proper col() reference and both the search and replacement strings. Option A has broken syntax (missing comma between pattern and replacement). Option B is missing the third replacement argument entirely. Option D passes the column name as a plain string 'storeSlogan' rather than using col('storeSlogan') - regexp_replace requires a Column object as its first argument. Option E uses regexp_extract, which extracts a matched substring rather than replacing it.
Topics
Community Discussion
No community discussion yet for this question.
