Dive deep into the world of database programming with Perl! Test your knowledge on connecting to databases using DBI, performing queries, handling transactions, and working with errors and prepared statements.
Using DBI to Connect to Databases (10 Questions)
Which module in Perl is used to connect to databases? A) DBI B) DBConnect C) DBD D) MySQL
What function is used to establish a connection to a database using DBI? A) DBI->connect B) DBI->connect_db C) DBI->open D) DBI->begin
Which of the following is required to connect to a database using DBI? A) Database driver B) Table name C) SQL query D) Port number
How do you specify the database’s connection string in Perl when using DBI? A) By using DBI->connect() and providing the DSN string B) By using DBI->connect_db() C) By using DBI->connection() D) By providing the host and database separately
Which parameter is used to store the database username in DBI? A) username B) user C) login D) uid
How do you disconnect from the database using DBI in Perl? A) DBI->disconnect() B) DBI->close() C) DBI->end() D) DBI->exit()
What is DSN in the context of DBI? A) Data Source Name, it identifies the database to connect to B) Data Set Name, used to specify table names C) Database Security Name D) Database Server Name
Which of the following DBI methods is used to fetch a row of data from the database? A) fetchrow_array B) fetchrow_hashref C) fetchrow D) fetchdata
Which of the following statements is correct about DBI? A) DBI abstracts the database-specific details, making it easier to switch between different database systems B) DBI is used only for MySQL databases C) DBI doesn’t support SQL queries D) DBI is used only for connecting to NoSQL databases
Which statement is true regarding database drivers in DBI? A) DBI requires a database driver (e.g., DBD::mysql) to interface with the database B) DBI has built-in support for all database types C) DBI connects directly to the database without the need for a driver D) DBI only supports SQLite
Performing Queries and Transactions (10 Questions)
In Perl DBI, which method is used to prepare an SQL query? A) prepare() B) execute() C) query() D) run()
How do you execute a query that doesn’t return a result (such as INSERT or UPDATE)? A) execute() B) do() C) run() D) query()
Which of the following DBI methods is used to fetch all rows from a database query? A) fetchall_arrayref() B) fetch_row() C) select_all() D) fetch()
What does the commit() method in Perl DBI do? A) It commits the current transaction to the database B) It starts a new transaction C) It rolls back the transaction D) It ends the connection
Which method is used to roll back a transaction in DBI? A) rollback() B) cancel() C) undo() D) terminate()
How can you bind parameters in a prepared statement using DBI? A) By using bind_param() B) By using prepare() C) By using execute() D) By using bind()
Which of the following is true about the do() method in DBI? A) It executes an SQL statement that does not return data (e.g., INSERT, UPDATE, DELETE) B) It executes a SELECT statement and returns a result C) It prepares an SQL statement without executing it D) It handles transactions
In DBI, how would you check if an INSERT operation was successful? A) By checking the $dbh->err value B) By checking the return value of execute() C) By checking the $dbh->rows value D) All of the above
Which method is used to fetch the next row from the result set in DBI? A) fetchrow_array() B) fetchrow_hashref() C) next_row() D) next()
What does the disconnect() method in DBI do? A) It terminates the database connection B) It deletes the database C) It rolls back any uncommitted changes D) It stops the query execution
Handling Errors and Prepared Statements (10 Questions)
How do you handle errors when connecting to a database using DBI in Perl? A) By checking the connection status with $dbh->err B) By using eval to catch errors C) By using try/catch blocks D) By checking $dbh->errstr
What is the purpose of using prepared statements in DBI? A) To prevent SQL injection attacks B) To optimize queries C) To handle complex queries efficiently D) All of the above
How do you fetch a result as a hash reference in DBI? A) fetchrow_hashref() B) fetchrow_array() C) fetch() D) fetchall_arrayref()
Which method in DBI allows you to prepare a statement with bound parameters? A) prepare() B) execute() C) do() D) query()
How can you check for an error after executing a query in DBI? A) By checking $dbh->err B) By checking $dbh->errstr C) By checking $sth->err D) All of the above
Which of the following is a recommended way to handle database errors in Perl DBI? A) Use eval to trap exceptions and handle errors B) Use try/catch to handle exceptions C) Use die to exit on errors D) Both A and B
Which method in DBI is used to fetch the next row of data as an array? A) fetchrow_array() B) fetchrow_hashref() C) fetch_array() D) fetch_all()
What will happen if a DBI query is executed with invalid SQL syntax? A) DBI will throw an exception and return an error message B) The query will execute, but with empty results C) DBI will silently ignore the error D) The program will terminate without an error
What does the $dbh->errstr method return in case of an error? A) The last error message from the database B) The last SQL query executed C) The connection status D) The query result
Which DBI method can be used to handle SQL injection vulnerabilities? A) Prepared statements with bound parameters B) execute() method C) do() method D) fetchall_arrayref()
Answers Table
QNo
Answer (Option with Text)
1
A) DBI
2
A) DBI->connect
3
A) Database driver
4
A) By using DBI->connect() and providing the DSN string
5
B) user
6
A) DBI->disconnect()
7
A) Data Source Name, it identifies the database to connect to
8
A) fetchrow_array
9
A) DBI abstracts the database-specific details, making it easier to switch between different database systems
10
A) DBI requires a database driver (e.g., DBD::mysql) to interface with the database
11
A) prepare()
12
A) execute()
13
A) fetchall_arrayref()
14
A) It commits the current transaction to the database
15
A) rollback()
16
A) By using bind_param()
17
A) It executes an SQL statement that does not return data (e.g., INSERT, UPDATE, DELETE)
18
D) All of the above
19
A) fetchrow_array()
20
A) It terminates the database connection
21
D) By checking $dbh->errstr
22
D) All of the above
23
A) fetchrow_hashref()
24
A) prepare()
25
D) All of the above
26
D) Both A and B
27
A) fetchrow_array()
28
A) DBI will throw an exception and return an error message