MCQs on Database Programming | Perl

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)

  1. Which module in Perl is used to connect to databases?
    A) DBI
    B) DBConnect
    C) DBD
    D) MySQL
  2. 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
  3. 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
  4. 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
  5. Which parameter is used to store the database username in DBI?
    A) username
    B) user
    C) login
    D) uid
  6. How do you disconnect from the database using DBI in Perl?
    A) DBI->disconnect()
    B) DBI->close()
    C) DBI->end()
    D) DBI->exit()
  7. 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
  8. 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
  9. 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
  10. 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)

  1. In Perl DBI, which method is used to prepare an SQL query?
    A) prepare()
    B) execute()
    C) query()
    D) run()
  2. 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()
  3. 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()
  4. 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
  5. Which method is used to roll back a transaction in DBI?
    A) rollback()
    B) cancel()
    C) undo()
    D) terminate()
  6. 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()
  7. 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
  8. 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
  9. 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()
  10. 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)

  1. 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
  2. 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
  3. How do you fetch a result as a hash reference in DBI?
    A) fetchrow_hashref()
    B) fetchrow_array()
    C) fetch()
    D) fetchall_arrayref()
  4. Which method in DBI allows you to prepare a statement with bound parameters?
    A) prepare()
    B) execute()
    C) do()
    D) query()
  5. 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
  6. 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
  7. 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()
  8. 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
  9. 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
  10. 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

QNoAnswer (Option with Text)
1A) DBI
2A) DBI->connect
3A) Database driver
4A) By using DBI->connect() and providing the DSN string
5B) user
6A) DBI->disconnect()
7A) Data Source Name, it identifies the database to connect to
8A) fetchrow_array
9A) DBI abstracts the database-specific details, making it easier to switch between different database systems
10A) DBI requires a database driver (e.g., DBD::mysql) to interface with the database
11A) prepare()
12A) execute()
13A) fetchall_arrayref()
14A) It commits the current transaction to the database
15A) rollback()
16A) By using bind_param()
17A) It executes an SQL statement that does not return data (e.g., INSERT, UPDATE, DELETE)
18D) All of the above
19A) fetchrow_array()
20A) It terminates the database connection
21D) By checking $dbh->errstr
22D) All of the above
23A) fetchrow_hashref()
24A) prepare()
25D) All of the above
26D) Both A and B
27A) fetchrow_array()
28A) DBI will throw an exception and return an error message
29A) The last error message from the database
30A) Prepared statements with bound parameters

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top