Question 1
Which statement about persistent tables created with df.write.saveAsTable('sales') is correct?
- The table disappears when the SparkSession ends
- The table can only be queried with the DataFrame API, not SQL
- It is registered in the catalog and outlives the session — Correct
- The table is only visible to the current notebook
Correct answer
It is registered in the catalog and outlives the session
Explanation
saveAsTable writes data and registers a table in the metastore, unlike temporary views, which are session-scoped.
Question 2
Which statement about spark.table('sales') is correct?
- It returns a DataFrame for a catalog table or view — Correct
- It creates an empty table named sales
- It returns the table's schema as a string
- It reads a file named sales in the working directory
Correct answer
It returns a DataFrame for a catalog table or view
Explanation
spark.table (or spark.read.table) loads a catalog table or view as a DataFrame.
Question 3
A view must be accessible from different SparkSessions in the same application. Which method should be used, and how is the view referenced?
- createOrReplaceTempView('v'), referenced as v in any session
- createOrReplaceGlobalTempView('v'), read as global_temp.v — Correct
- createGlobalView('v'), referenced as default.v
- saveAsTable('v'), referenced as temp.v
Correct answer
createOrReplaceGlobalTempView('v'), read as global_temp.v
Explanation
Global temporary views live in the global_temp database and are shared across sessions of the same application until it ends.
Question 4
Which statement about querying CSV files directly with SELECT * FROM csv.`/path` is correct?
- Reader options cannot be set, so columns become _c0, _c1 strings — Correct
- It creates a permanent table named csv
- The header row is detected automatically and types are inferred
- It only works for files smaller than 1 MB
Correct answer
Reader options cannot be set, so columns become _c0, _c1 strings
Explanation
The direct-file syntax uses default reader options. For headers, delimiters or schemas, create a view or table with options, or use read_files or the DataFrame reader.
Question 5
Which SQL statement creates a table from a query and saves it as Parquet partitioned by country?
- CREATE VIEW sales_p USING parquet PARTITIONED BY (country) AS SELECT * FROM sales
- SAVE TABLE sales_p FROM sales USING parquet BY country
- CREATE TABLE sales_p USING parquet PARTITIONED BY (country) AS SELECT * FROM sales — Correct
- CREATE TABLE sales_p AS SELECT * FROM sales PARTITION BY country STORED AS parquet FORMAT
Correct answer
CREATE TABLE sales_p USING parquet PARTITIONED BY (country) AS SELECT * FROM sales
Explanation
CTAS with USING and PARTITIONED BY creates a partitioned table in the given format. Views do not store data.
Question 6
A user runs spark.sql('SELECT * FROM delta.`/mnt/data/orders`'). What does this do?
- Converts the Parquet files at that path to Delta
- It queries the Delta table at that path — Correct
- Creates a new Delta table named delta
- Fails because Delta tables must be registered first
Correct answer
It queries the Delta table at that path
Explanation
The format.`path` syntax works for Delta as well, reading the table at that path.
Question 7
A Python UDF must be callable from Spark SQL queries. Which code registers it?
- spark.catalog.createFunction('clean_name')
- spark.sql.register('clean_name', clean_name_fn)
- spark.udf.register('clean_name', clean_name_fn, StringType()) — Correct
- df.createOrReplaceTempView('clean_name')
Correct answer
spark.udf.register('clean_name', clean_name_fn, StringType())
Explanation
spark.udf.register makes the function available to SQL under the given name.
Question 8
Which statement about spark.sql() is correct?
- It returns results as a Python list
- It returns a DataFrame usable with the DataFrame API — Correct
- It executes the query immediately even without an action
- It can only query permanent tables
Correct answer
It returns a DataFrame usable with the DataFrame API
Explanation
spark.sql returns a DataFrame; like other transformations, SELECT queries execute when an action runs.
Question 9
Which code writes a DataFrame to a partitioned table in the catalog?
- df.write.bucketBy('country').saveAsTable('sales_by_country')
- df.write.partitionBy('country').saveAsTable('sales_by_country') — Correct
- df.partitionBy('country').saveAsTable('sales_by_country')
- df.write.saveAsTable('sales_by_country', partitionBy='country', mode='overwrite', format='parquet')
Correct answer
df.write.partitionBy('country').saveAsTable('sales_by_country')
Explanation
partitionBy on the DataFrameWriter is combined with saveAsTable to create a partitioned table.
Question 10
Given:
df.write.mode('append').saveAsTable('t')
executed twice with the same 100-row DataFrame. How many rows does table t contain if it was empty before?
- 0
- 200 — Correct
- The second write fails
- 100
Correct answer
200
Explanation
Append adds rows on each write, so the table has 200 rows.