Docsity

WGU D522 Python for IT Automation | Database Automation, SQLite & Data Pipeline Workbook |, Exams of Information Technology

Partial preview of the text

Download WGU D522 Python for IT Automation | Database Automation, SQLite & Data Pipeline Workbook | and more Exams Information Technology in PDF only on Docsity!

WGU D522 Python for IT Automation |

Database Automation, SQLite & Data Pipeline

Workbook | 20 Python Labs + Practice

Questions with Rationales + 5 Projects |

2026/2027 update | with complete solutions…

Section 1: SQLite Database Fundamentals & Connection

  1. Which Python module provides built-in support for SQLite databases? A. sql B. sqlite C. dbapi D. pysqlite Answer: B Rationale: sqlite3 is part of Python's standard library and provides the DB-API 2.0 interface for SQLite databases.
  2. What does sqlite3.connect('example.db') do? A. Creates a new database file or opens an existing one B. Deletes the database file C. Connects to a remote SQL server D. Creates a table named 'example' Answer: A Rationale: connect() opens a connection to the SQLite database file. If the file does not exist, it is created automatically.
  3. Which object is used to execute SQL statements in sqlite3? A. Connection B. Cursor C. Statement

Answer: A Rationale: A primary key uniquely identifies each row in a table. SQLite enforces uniqueness and non-null constraints on primary key columns.

  1. Which constraint ensures that a column cannot contain NULL values? A. UNIQUE B. NOT NULL C. CHECK D. DEFAULT Answer: B Rationale: NOT NULL prevents NULL values from being inserted into the column.
  2. What does AUTOINCREMENT do in SQLite? A. Automatically deletes old rows B. Generates a unique integer value for each new row, never reusing values C. Automatically creates a new database D. Encrypts the column Answer: B Rationale: AUTOINCREMENT ensures that the primary key value is always larger than any previously used value, even after deletions.
  3. Which data type in SQLite stores integer values? A. TEXT B. REAL C. INTEGER D. BLOB Answer: C Rationale: INTEGER stores whole numbers. REAL stores floating-point numbers, TEXT stores strings, and BLOB stores binary data.
  4. How do you close a SQLite connection in Python? A. connection.end() B. connection.close() C. connection.stop() D. connection.disconnect() Answer: B

Rationale: close() terminates the database connection and releases resources.

  1. What happens if you try to insert a duplicate value into a column with a UNIQUE constraint? A. The insert succeeds with a warning B. SQLite raises an IntegrityError C. The duplicate is silently ignored D. The table is dropped Answer: B Rationale: SQLite enforces UNIQUE constraints and raises an IntegrityError when a duplicate value is inserted.
  2. Which SQLite data type is most appropriate for storing text? A. INTEGER B. REAL C. TEXT D. BLOB Answer: C Rationale: TEXT stores character strings. SQLite uses dynamic typing, but declaring TEXT provides type affinity.
  3. What is the purpose of the WITH clause in SQLite? A. To define a common table expression (CTE) for use within a query B. To create a new database C. To commit a transaction D. To drop a table Answer: A Rationale: WITH defines a temporary named result set (CTE) that can be referenced within the main query, improving readability for complex queries.
  4. How do you check the SQLite version in Python? A. sqlite3.version B. sqlite3.sqlite_version C. sqlite3.version_info D. sqlite3.get_version() Answer: B Rationale: sqlite3.sqlite_version returns the version of the underlying

D. To encrypt the database Answer: B Rationale: Parameterized queries use placeholders (e.g., ?) and pass values separately, preventing malicious input from being interpreted as SQL code.

  1. Which placeholder style is used with sqlite3 parameterized queries? A. %s B.? C. :name D. Both B and C Answer: D Rationale: sqlite3 supports both qmark style (?) and named style (:name) placeholders.
  2. What does the UPDATE statement do? A. Adds new rows B. Modifies existing rows in a table C. Deletes rows D. Creates a table Answer: B Rationale: UPDATE changes values in existing rows based on a WHERE condition.
  3. What does the DELETE FROM statement do? A. Drops a table B. Removes rows from a table C. Deletes the database D. Clears all columns Answer: B Rationale: DELETE FROM removes rows that match the specified condition.
  4. What happens if you run DELETE FROM table_name; without a WHERE clause? A. Nothing happens B. All rows in the table are deleted C. The table structure is deleted D. An error is raised

Answer: B Rationale: Without a WHERE clause, DELETE removes all rows from the table. The table structure remains.

  1. Which method fetches all remaining rows from a cursor? A. cursor.fetchone() B. cursor.fetchall() C. cursor.fetchmany() D. cursor.getall() Answer: B Rationale: fetchall() returns a list of all remaining rows in the result set.
  2. Which method fetches a single row from a cursor? A. fetchone() B. fetchsingle() C. fetchrow() D. getone() Answer: A Rationale: fetchone() returns the next row of the result set, or None if no more rows are available.
  3. What is the most secure way to insert user-provided values into a SQL query? A. String concatenation B. f-string interpolation C. Parameterized queries D. % formatting Answer: C Rationale: Parameterized queries prevent SQL injection by treating user input as data, not executable SQL.
  4. Which SQL clause is used with aggregate functions to filter groups? A. WHERE B. HAVING C. GROUP BY D. ORDER BY Answer: B

B. A column that references the primary key of another table, establishing a relationship C. A key used for encryption D. A temporary key Answer: B Rationale: A foreign key creates a link between two tables by referencing the primary key of another table.

  1. Which type of JOIN returns only rows where there is a match in both tables? A. LEFT JOIN B. RIGHT JOIN C. INNER JOIN D. FULL JOIN Answer: C Rationale: INNER JOIN returns rows that have matching values in both tables.
  2. What does a LEFT JOIN return? A. Only matching rows B. All rows from the left table, plus matching rows from the right (NULL if no match) C. All rows from the right table only D. No rows Answer: B Rationale: LEFT JOIN preserves all rows from the left table and fills with NULLs where no match exists in the right table.
  3. Which SQL function calculates the average of a numeric column? A. SUM() B. AVG() C. MEAN() D. AVERAGE() Answer: B Rationale: AVG() returns the arithmetic mean of a numeric column.
  4. What does COUNT(*) return? A. The number of columns B. The number of rows in the result set

C. The sum of all values D. The average value Answer: B Rationale: COUNT(*) counts all rows, including those with NULL values.

  1. Which clause groups rows with the same values into summary rows? A. ORDER BY B. GROUP BY C. WHERE D. HAVING Answer: B Rationale: GROUP BY groups rows that have the same values in specified columns, often used with aggregate functions.
  2. What is the difference between COUNT(column_name) and COUNT()? A. No difference B. COUNT(column_name) excludes NULL values; COUNT() includes all rows C. COUNT() excludes NULL values D. COUNT(column_name) is faster Answer: B Rationale: COUNT(column_name) counts only non-NULL values in that column. COUNT() counts all rows regardless of NULLs.
  3. Which JOIN returns all rows from both tables, with NULLs where no match exists? A. INNER JOIN B. LEFT JOIN C. RIGHT JOIN D. FULL OUTER JOIN Answer: D Rationale: FULL OUTER JOIN combines the results of both LEFT and RIGHT joins, including unmatched rows from both tables.
  4. What is a many-to-many relationship in a database? A. A relationship where one row in table A relates to many rows in table B, and vice versa B. A relationship with no foreign keys
  1. What is the purpose of the json module in data pipelines? A. To parse and serialize JSON data B. To connect to SQLite C. To read CSV files D. To create tables Answer: A Rationale: json provides loads()/dumps() for string conversion and load()/dump() for file I/O.
  2. What does json.dumps() return? A. A Python dictionary B. A JSON-formatted string C. A file object D. A database cursor Answer: B Rationale: dumps() serializes a Python object to a JSON string. loads() does the reverse.
  3. In an ETL pipeline, what does "Extract" refer to? A. Loading data into the target database B. Reading data from source systems (e.g., CSV, JSON, APIs) C. Transforming data types D. Deleting old data Answer: B Rationale: Extract is the first phase, retrieving data from source systems.
  4. What is the purpose of validating data before importing it into SQLite? A. To slow down the pipeline B. To catch malformed records, missing fields, and type mismatches before they cause database errors C. To encrypt the data D. To delete duplicate rows automatically Answer: B Rationale: Validation prevents invalid data from entering the database and causing integrity errors or corrupting reports.
  1. How do you handle duplicate records when importing CSV data into SQLite? A. Use INSERT OR IGNORE or check for existing records before inserting B. Delete the entire table first C. Ignore all errors D. Use UPDATE for every row Answer: A Rationale: INSERT OR IGNORE skips rows that violate unique constraints. Alternatively, query for existing records before inserting.
  2. What is a common cause of a sqlite3.IntegrityError during data import? A. The CSV file is too large B. A foreign key or unique constraint is violated by the incoming data C. The database is encrypted D. The Python version is outdated Answer: B Rationale: IntegrityError occurs when data violates constraints such as UNIQUE, NOT NULL, or foreign key relationships.
  3. What does the "Transform" phase of ETL involve? A. Loading data into the database B. Cleaning, validating, and converting data into the required format C. Extracting data from the source D. Backing up the database Answer: B Rationale: Transform applies business rules, cleans data, converts types, and prepares it for loading.
  4. Why is structured logging important in a database automation script? A. It makes the script run faster B. It provides an audit trail and helps diagnose failures during data processing C. It replaces the need for error handling D. It encrypts the database Answer: B Rationale: Logging records what happened during execution, making it easier to debug failed imports, trace data issues, and audit changes.
  1. Which Python exception is raised when a SQL constraint is violated? A. sqlite3.OperationalError B. sqlite3.IntegrityError C. sqlite3.DatabaseError D. sqlite3.ProgrammingError Answer: B Rationale: IntegrityError is raised when a constraint (e.g., UNIQUE, NOT NULL, FOREIGN KEY) is violated.
  2. Which Python exception is raised for syntax errors in SQL? A. sqlite3.IntegrityError B. sqlite3.OperationalError C. sqlite3.ProgrammingError D. sqlite3.DataError Answer: C Rationale: ProgrammingError is raised for SQL syntax errors and incorrect number of parameters.
  3. What is the recommended pattern for ensuring a connection is closed even if an exception occurs? A. Call close() at the end of the script only B. Use a try/finally block or a with context manager C. Never close the connection D. Rely on Python's garbage collector Answer: B Rationale: try/finally or with conn: ensures close() is called regardless of exceptions.
  4. What does conn.execute() return? A. A connection object B. A cursor object C. A list of rows D. None Answer: B Rationale: execute() returns a cursor that can be used to fetch results or check rowcount.
  1. What does cursor.rowcount indicate after an INSERT? A. The number of rows in the table B. The number of rows affected by the statement C. The number of columns D. The last inserted row ID Answer: B Rationale: rowcount returns the number of rows modified, inserted, or deleted by the most recent statement.
  2. What does cursor.lastrowid return? A. The primary key of the last inserted row B. The last row of the result set C. The last column name D. The last error message Answer: A Rationale: lastrowid returns the ROWID of the most recently inserted row, useful for retrieving auto-generated IDs.
  3. Why should database operations be wrapped in try/except blocks in automation scripts? A. To make the script run faster B. To handle constraint violations, connection failures, and other errors gracefully without crashing C. To encrypt the database D. To create backups automatically Answer: B Rationale: Error handling allows the script to log the failure, continue processing other records, or roll back the transaction appropriately.
  4. What is the purpose of a savepoint in SQLite? A. To create a full backup B. To set a point within a transaction to which you can roll back partially C. To encrypt the database D. To drop a table Answer: B Rationale: SAVEPOINT creates a named point within a transaction. ROLLBACK TO savepoint_name reverts to that point without rolling back the entire transaction.