JDBC practice questions

From OCP Java SE 8 (1Z0-809) · 15 questions on this topic

JDBC practice questions from OCP Java SE 8 (1Z0-809). This pack has 15 questions tagged JDBC, drawn from its timed mock exams. 8 of them are worked through in full below — the question, every option, why each is right or wrong, and the explanation.

Worked examples for JDBC

  1. Question 1

    Which arguments does DriverManager.getConnection accept to open a database connection?

    1. A. A datasource JNDI name only

      A JNDI datasource lookup goes through DataSource, a different mechanism than getConnection.

    2. B. A JDBC URL, optionally followed by a username and passwordCorrect answer

      Correct: the overloads take a JDBC URL, optionally with a username and password.

    3. C. A Connection object to clone

      getConnection opens a new connection from a URL, not by cloning an existing one.

    4. D. A driver class name and a port number

      Host and port are embedded in the URL; a separate driver class and port pair is not an argument form.

    Explanation

    DriverManager.getConnection is called with a JDBC URL, optionally followed by a username and password or a Properties object. The URL itself carries the host and port details, and datasource-style lookups use a separate mechanism.

  2. Question 2

    Which JDBC components come from the JDK, and which from the database vendor?

    1. A. The vendor provides the interfaces; the JDK implements them

      This reverses the actual roles: the JDK's java.sql package defines the interfaces, while the vendor's driver JAR supplies the implementations.

    2. B. The JDK provides the java.sql interfaces; the vendor's driver JAR provides their implementationsCorrect answer

      java.sql defines the contract (Driver, Connection, Statement, ResultSet) while each vendor's driver JAR supplies the classes that implement it, which is what lets the same code target any database by swapping the JAR and URL.

    3. C. Both interfaces and implementations ship in the driver JAR

      The interfaces live in the JDK's java.sql package, not the driver JAR; the JAR carries only the vendor-specific implementations.

    4. D. The JDK provides full implementations for every common database

      The JDK ships only the interface contract; the concrete implementations for each database come from that vendor's driver JAR, not the JDK.

    Explanation

    java.sql defines the CONTRACT (Driver, Connection, Statement, ResultSet); each vendor's driver JAR supplies the classes that implement it. That separation is what lets the same code talk to any database by swapping the JAR and URL.

  3. Question 3

    A ResultSet was created with the default Statement settings. Which navigation call is guaranteed to work?

    1. A. first()

      first() requires a scrollable type such as TYPE_SCROLL_INSENSITIVE, so on the default forward-only ResultSet it throws SQLException rather than being guaranteed to work.

    2. B. absolute(3)

      absolute positioning needs a scrollable ResultSet type, so it is not available on the default TYPE_FORWARD_ONLY result and throws SQLException.

    3. C. previous()

      Moving backward with previous() requires a scrollable type; a forward-only ResultSet cannot go backward, so it throws SQLException.

    4. D. next()Correct answer

      The default ResultSet type is TYPE_FORWARD_ONLY, and next() is the one navigation call that type supports, so it is always guaranteed to work.

    Explanation

    The default ResultSet type is TYPE_FORWARD_ONLY — only next() is available. previous, absolute, and first all require creating the Statement with a scrollable type like TYPE_SCROLL_INSENSITIVE, otherwise they throw SQLException.

  4. Question 4

    Which three pieces of diagnostic information does a SQLException provide through dedicated getters?

    1. A. Message text, SQLState string, and vendor error codeCorrect answer

      Correct: getMessage, getSQLState, and getErrorCode provide exactly these three pieces of diagnostic information.

    2. B. Connection URL, username, and driver version

      Connection URL, username, and driver version are not carried by the exception.

    3. C. Query plan, execution time, and rows affected

      Query plans, timings, and row counts are not exposed by SQLException.

    4. D. Table name, column name, and row number

      Table, column, and row locations are not part of the exception's dedicated getters.

    Explanation

    SQLException exposes the message text, the standardized SQLState string, and the vendor-specific error code through dedicated getters, plus chaining to the next exception. Connection metadata, performance data, and schema locations are not part of it.

  5. Question 5

    A program calls stmt.executeUpdate("SELECT * FROM users"). What happens?

    1. A. The query runs and the results are silently discarded

      Results are not silently discarded; executeUpdate rejects a result-producing statement with an exception rather than running it.

    2. B. An SQLException is thrown at runtimeCorrect answer

      executeUpdate is defined to reject any SQL that produces a ResultSet, so passing a SELECT throws SQLException at runtime.

    3. C. Compilation fails because the SQL is a query

      The SQL is an opaque string to the Java compiler, so nothing about the query is checked at compile time.

    4. D. It compiles and returns the rows as an int

      executeUpdate returns an affected-row count for data-changing statements, never row data, so a SELECT's rows cannot be returned as an int.

    Explanation

    The SQL text is just an opaque string to the Java compiler, so the mismatch is never caught at compile time. At runtime, executeUpdate is defined to reject any statement that yields a ResultSet, so handing it a SELECT throws SQLException; the method-to-SQL pairing is enforced by the driver rather than the language.

  6. Question 6

    On a scrollable ResultSet containing five rows, what does rs.absolute(-1) do?

    1. A. Moves the cursor before the first row

      Positioning before the first row is what absolute(0) does, not absolute(-1).

    2. B. Moves the cursor to the last rowCorrect answer

      A negative argument to absolute counts backward from the end, so absolute(-1) lands on the last row.

    3. C. Moves the cursor one row backward

      Moving one row backward from the current position is relative(-1), not absolute(-1).

    4. D. Throws SQLException because positions cannot be negative

      Negative positions are valid for absolute and count backward from the end, for example -2 is the second-to-last row, so no exception is thrown.

    Explanation

    A negative argument to absolute counts backward from the end of the ResultSet, so on five rows absolute(-1) positions the cursor on the last row and -2 would be the second-to-last. This differs from relative, which moves by an offset from the current row, and from absolute(0), which sits before the first row.

  7. Question 7

    Which Statement method is appropriate for executing a CREATE TABLE command, and what does it return on success?

    1. A. executeQuery — returning an empty ResultSet

      executeQuery is for statements that produce a ResultSet and throws otherwise, so it is not appropriate for DDL.

    2. B. execute — which throws an exception for DDL

      execute works for DDL rather than throwing.

    3. C. executeUpdate — returning 0 for DDL statementsCorrect answer

      Correct: executeUpdate runs DDL and returns zero for statements that change no rows.

    4. D. executeUpdate — returning 1 for each table created

      executeUpdate returns zero for DDL, not a per-table count.

    Explanation

    DDL statements run through executeUpdate, which returns the affected-row count for data changes and zero for statements that return no rows. executeQuery is only for row-producing statements, and plain execute also handles DDL.

  8. Question 8

    A JDBC URL is written as `jdbc:postgresql://dbhost:5432/inventory`. Which statement about its structure is true?

    1. A. It has three colon-separated parts: the literal jdbc, the vendor subprotocol, and vendor-specific connection detailsCorrect answer

      Every JDBC URL is three colon-separated parts: the literal jdbc, the vendor subprotocol, and a vendor-defined remainder holding the connection details.

    2. B. The port and host are required in every JDBC URL

      The final segment is vendor-defined; only some drivers encode a host and port, while others use a file path or nothing at all, so host and port are not universally required.

    3. C. The first part names the database vendor

      The first segment is always the literal jdbc, not the vendor; the vendor subprotocol is the second segment.

    4. D. The URL format is identical for every database vendor

      The final segment is defined by each vendor's driver, so the format varies from one database to another rather than being identical.

    Explanation

    A JDBC URL always has three colon-separated segments: the literal jdbc prefix, the vendor subprotocol, and a vendor-defined remainder. Because that final segment is defined by each driver, it varies between databases, encoding host, port, and database for some vendors and a file path or nothing for others.

Practise all 15 JDBC questions

OCP Java SE 8 has the full set, inside timed mock exams that mirror real exam conditions — every question with a worked explanation.

Open OCP Java SE 8

Other topics in this pack