0

programming languages Online Quiz - 329

Description: programming languages Online Quiz - 329
Number of Questions: 20
Created by:
Tags: programming languages
Attempted 0/20 Correct 0 Score 0
  1. O (a) PreparedStatement

  2. O (b) ParameterizedStatement

  3. O (c) ParameterizedStatement and CallableStatement

  4. O (d) All kinds of Statements (i.e. which implement a sub interface of Statement)


Correct Option: A

How can you retrieve information from a ResultSet?

  1. O (a) By invoking the method get (..., String type) on the ResultSet, where type is the database

  2. O (b) By invoking the method get (..., Type type) on the ResultSet, where Type is an object

  3. O (c) By invoking the method getValue (...), and cast the result to the desired java type.

  4. O (d) By invoking the special getter methods on the ResultSet: getString (...), get Boolean (...),


Correct Option: D
Explanation:

To retrieve information from a ResultSet, the user needs to know the methods that can be used to access the data. A ResultSet is a table of data that represents the results of a database query.

Now let's go through each option and explain why it is right or wrong:

A. O (a) By invoking the method get (..., String type) on the ResultSet, where type is the database

This option is incorrect because there is no method get (..., String type) in ResultSet. The method get() can be used to retrieve data from the ResultSet, but it requires the index or the name of the column. The String type parameter is not valid.

B. O (b) By invoking the method get (..., Type type) on the ResultSet, where Type is an object

This option is incorrect because there is no method get (..., Type type) in ResultSet. The method get() can be used to retrieve data from the ResultSet, but it requires the index or the name of the column. The Type type parameter is not valid.

C. O (c) By invoking the method getValue (...), and cast the result to the desired java type.

This option is incorrect because there is no method getValue() in ResultSet. The method get() can be used to retrieve data from the ResultSet, but it requires the index or the name of the column. Additionally, the method get() returns an object that needs to be cast to the desired data type.

D. O (d) By invoking the special getter methods on the ResultSet: getString (...), get Boolean (...),

This option is correct. ResultSet provides special getter methods such as getString(), getInt(), getBoolean(), etc. that can be used to retrieve data from the ResultSet. These methods take the index or the name of the column as a parameter and return the value of the column as the desired data type.

The Answer is: D

How can you execute DML statements (i.e. insert, delete, update) in the database?

  1. O (a) By making use of the InsertStatement, DeleteStatement or UpdateStatement classes

  2. O (b) By invoking the execute(...) or executeUpdate(...) method of a normal Statement object

  3. O (c) By invoking the executeInsert(...), executeDelete(...) or executeUpdate(...) methods of

  4. O (d) By making use of the execute(...) statement of the DataModificationStatement object


Correct Option: B
Explanation:

To execute DML statements (i.e. insert, delete, update) in the database, the user needs to know the methods or classes available to achieve this.

Option A is incorrect because there are no predefined classes in Java named InsertStatement, DeleteStatement or UpdateStatement for executing DML statements in a database.

Option B is correct. You can execute DML statements by invoking the execute() or executeUpdate() method of a normal Statement object. The execute() method is used for executing any type of SQL statement and can return a boolean value indicating whether the query returns a ResultSet or not. The executeUpdate() method is used for executing insert, delete, and update statements and returns an integer value representing the number of rows affected by the query.

Option C is incorrect because there are no predefined methods named executeInsert(), executeDelete() or executeUpdate() for executing DML statements in Java.

Option D is incorrect because there is no class named DataModificationStatement in Java.

Therefore, the correct answer is:

The Answer is: B

  1. O (a) You must catch the checked SQLException which is thrown by the method which executes

  2. O (b) You must catch the unchecked SQLWarningException which is thrown by the method

  3. O (c) You must invoke the getWarnings() method on the Statement object (or a sub interface

  4. O (d) You must query the ResultSet object about possible warnings generated by the database


Correct Option: C
Explanation:

To know in your Java program that a SQL warning is generated as a result of executing a SQL statement in the database, you must invoke the getWarnings() method on the Statement object (or a sub interface). Therefore, option C is the correct answer.

Option A is incorrect because SQLException is thrown when an SQL error occurs, not a warning. It is a checked exception and must be caught or declared to be thrown.

Option B is incorrect because SQLWarningException is not a valid exception class in Java. SQLWarning is the class that represents a warning issued by the database.

Option D is incorrect because ResultSet object is used to retrieve data from the database after executing a query, not to retrieve warnings.

Therefore, the correct answer is:

The Answer is: C. O (c) You must invoke the getWarnings() method on the Statement object (or a sub interface).

  1. O (a) A DataSource is the basic service for managing a set of JDBC drivers

  2. O (b) A DataSource is the Java representation of a physical data source

  3. O (c) A DataSource is a registry point for JNDI-services

  4. O (d) A DataSource is a factory of connections to a physical data source


Correct Option: D
  1. O (a) This means that the ResultSet is insensitive to scrolling

  2. O (b) This means that the Resultset is sensitive to scrolling, but insensitive to updates, i.e. not

  3. O (c) This means that the ResultSet is sensitive to scrolling, but insensitive to changes made

  4. O (d) The meaning depends on the type of data source, and the type and version of the driver


Correct Option: C

Are ResultSets updateable?

  1. O (a) Yes, but only if you call the method openCursor() on the ResultSet, and if the driver and

  2. O (b) Yes, but only if you indicate a concurrency strategy when executing the statement, and

  3. O (c) Yes, but only if the ResultSet is an object of class UpdateableResultSet, and if the driver

  4. O (d) No, ResultSets are never updateable. You must explicitly execute DML statements (i.e.


Correct Option: B
Explanation:

To answer this question, the user needs to have knowledge about ResultSets and their characteristics.

Option A is incorrect because there is no openCursor() method in the ResultSet class.

Option B is partially correct. The ResultSet object is updateable only if you indicate a concurrency strategy when executing the statement, and the driver supports updateable ResultSets.

Option C is incorrect because there is no class called UpdateableResultSet in the Java API.

Option D is incorrect because ResultSets can be updateable, but it depends on the driver, the type of ResultSet, and the concurrency strategy used.

Therefore, the correct answer is:

The Answer is: B

  1. [_] [a] A transaction is a set of successfully executed statements in the database

  2. [_] [b] A transaction is finished when commit() or rollback() is called on the Connection object,

  3. [_] [c] A transaction is finished when commit() or rollback() is called on the Transaction object

  4. [_] [d] A transaction is finished when close() is called on the Connection object.


Correct Option: B,D
  1. O (a) By asking a Transaction object to your Connection, and calling the method begin() on it

  2. O (b) By asking a Transaction object to your Connection, and setting the autoCommit property

  3. O (c) By calling the method beginTransaction() on the Connection object

  4. O (d) By setting the autoCommit property of the Connection to false, and execute a statement


Correct Option: D
  1. O (a) Dirty reads, non-repeatable reads and phantom reads can occur

  2. O (b) Dirty reads are prevented; non-repeatable reads and phantom reads can occur

  3. O (c) Dirty reads and non-repeatable reads are prevented; phantom reads can occur

  4. O (d) Dirty reads, non-repeatable reads and phantom reads are prevented


Correct Option: C
Explanation:

To understand the meaning of the transaction isolation level TRANSACTION_REPEATABLE_READ, we need to know about transaction isolation levels in database management systems.

Transaction isolation level is a concept that ensures the correctness and consistency of database transactions. It defines the level of isolation between concurrent transactions in the system.

The TRANSACTION_REPEATABLE_READ isolation level guarantees that during the transaction, any row that is read will be locked for the duration of the transaction. This means that if the transaction tries to read the same row again, it will get the same value as before. This ensures that the transaction sees a consistent snapshot of the data.

Now let's go through each option to see which one is correct:

A. (a) Dirty reads, non-repeatable reads, and phantom reads can occur - This option is incorrect. The TRANSACTION_REPEATABLE_READ level prevents dirty reads and non-repeatable reads, but phantom reads can still occur.

B. (b) Dirty reads are prevented; non-repeatable reads and phantom reads can occur - This option is incorrect. The TRANSACTION_REPEATABLE_READ level prevents dirty reads, but non-repeatable reads and phantom reads can still occur.

C. (c) Dirty reads and non-repeatable reads are prevented; phantom reads can occur - This option is correct. The TRANSACTION_REPEATABLE_READ level prevents dirty reads and non-repeatable reads, but phantom reads can still occur.

D. (d) Dirty reads, non-repeatable reads, and phantom reads are prevented - This option is incorrect. The TRANSACTION_REPEATABLE_READ level only prevents dirty reads and non-repeatable reads, but phantom reads can still occur.

Therefore, the answer is: C. (c) Dirty reads and non-repeatable reads are prevented; phantom reads can occur.

  1. [_] [a] Using the cursor technique is currently the only possible way to change the data in the

  2. [_] [b] Insert statements are not supported when using cursors.

  3. [_] [c] Only scrollable updateable ResultSets can use this approach to change the data in the

  4. [_] [d] The name of the cursor is specified by the setCursorName(String name) method the


Correct Option: B,D
  1. O (a) Call method execute() on a CallableStatement object

  2. O (b) Call method executeProcedure() on a Statement object

  3. O (c) Call method execute() on a StoredProcedure object

  4. O (d) Call method run() on a ProcedureCommand object


Correct Option: A
Explanation:

The correct answer is A. You can execute a stored procedure in the database by calling the execute() method on a CallableStatement object.

Explanation:

A stored procedure is a precompiled set of SQL statements that are stored in the database. To execute a stored procedure, you need to create a CallableStatement object, which allows you to call the stored procedure.

Here's an example of how to execute a stored procedure using Java and JDBC:

// Assuming you have already established a database connection
CallableStatement cstmt = conn.prepareCall("{call your_stored_procedure(?, ?)}");

// Set any input parameters for the stored procedure, if needed
cstmt.setString(1, "input_parameter1");
cstmt.setInt(2, 2);

// Execute the stored procedure
cstmt.execute();

In the code above, your_stored_procedure is the name of the stored procedure you want to execute. The ? placeholders are used for any input parameters the stored procedure may require. You can set the input parameters using the setXxx() methods on the CallableStatement object.

Lastly, you call the execute() method on the CallableStatement object to execute the stored procedure.

What happens if you call the method close() on a ResultSet object?

  1. O (a) the method close() does not exist for a ResultSet. Only Connections can be closed.

  2. O (b) the database and JDBC resources are released

  3. O (c) you will get a SQLException, because only Statement objects can close ResultSets

  4. O (d) the ResultSet, together with the Statement which created it and the Connection from


Correct Option: B
Explanation:

To answer this question, the user needs to have a basic understanding of Java Database Connectivity (JDBC) and ResultSet objects.

Option A is incorrect. The close() method does exist for ResultSet objects.

Option B is correct. If you call the close() method on a ResultSet object, the resources associated with the ResultSet will be released and returned to the database. This includes any database and JDBC resources that were used to create the ResultSet.

Option C is incorrect. The close() method can be called on a ResultSet object, but not on a Statement object. It is the Statement object that creates the ResultSet.

Option D is incorrect. While the ResultSet is associated with the Statement and Connection objects that created it, calling the close() method on the ResultSet will only release the resources associated with the ResultSet itself, not the Statement or Connection objects.

Therefore, the answer is: B. O (b) the database and JDBC resources are released.

  1. O (a) The row you are positioned on is deleted from the ResultSet, but not from the database.

  2. O (b) The row you are positioned on is deleted from the ResultSet and from the database

  3. O (c) The result depends on whether the property synchonizeWithDataSource is set to true or

  4. O (d) You will get a compile error: the method does not exist because you can not delete rows


Correct Option: B
Explanation:

The correct answer to the question is B. The row you are positioned on is deleted from the ResultSet and from the database.

The deleteRow() method of the ResultSet interface deletes the current row from the ResultSet object and from the underlying database table. This means that the row will no longer be visible in the ResultSet object, and it will also be deleted from the database.

The deleteRow() method only works if the ResultSet object is updatable. An updatable ResultSet object is one that can be used to modify data in the database. To create an updatable ResultSet object, you must set the CONCUR_UPDATABLE property of the Statement object that you use to execute the query.

The deleteRow() method does not throw any exceptions. If you try to delete a row from a ResultSet object that is not updatable, the method will simply do nothing.

Here is an example of how to use the deleteRow() method:

import java.sql.*;

public class DeleteRowExample {

    public static void main(String[] args) throws SQLException {
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase", "username", "password");
        Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        String sql = "SELECT * FROM customers";
        ResultSet resultSet = statement.executeQuery(sql);

        // Move the cursor to the first row.
        resultSet.first();

        // Delete the current row.
        resultSet.deleteRow();

        // Close the connection.
        connection.close();
    }
}

This code will delete the first row in the customers table.

What statements are correct about batched insert and updates? (2 answers)

  1. [_] [a] To create a batch of insert and update statements, you create an object of type Batch,

  2. [_] [b] Batch insert and updates are only possible when making use of parameterized queries.

  3. [_] [c] To do a batched update/insert, you call addBatch(String statement) on a Statement

  4. [_] [d] To execute a batched update/insert, you call the executeBatch() method on a Statement


Correct Option: C,D

AI Explanation

To answer this question, let's go through each statement:

A) To create a batch of insert and update statements, you create an object of type Batch. This statement is incorrect. In general, to create a batch of insert and update statements, you would typically use an object of type PreparedStatement or CallableStatement, not Batch.

B) Batch insert and updates are only possible when making use of parameterized queries. This statement is not provided as an option, so it is not relevant to the question.

C) To do a batched update/insert, you call addBatch(String statement) on a Statement. This statement is correct. To add a SQL statement to a batch, you would call the addBatch(String statement) method on a Statement object. The statement parameter should be a valid SQL statement.

D) To execute a batched update/insert, you call the executeBatch() method on a Statement. This statement is correct. To execute a batch of SQL statements, you would call the executeBatch() method on a Statement object. This method returns an array of integers representing the update counts for each statement in the batch.

Based on the explanations above, the correct statements are C) and D).

What is correct about DDL statements (create, grant,...)?

  1. O (a) DDL statements are treated as normal sql statements, and are executed by calling the

  2. O (b) To execute DDL statements, you have to install additional support files

  3. O (c) DDL statements can not be executed by making use of JDBC, you should use the native

  4. O (d) Support for DDL statements will be a feature of a future release of JDBC


Correct Option: A
Explanation:

To answer this question, the user needs to have knowledge about DDL (Data Definition Language) statements and their characteristics.

Now, let's go through each option and explain why it is right or wrong:

A. O (a) DDL statements are treated as normal SQL statements, and are executed by calling them. This statement is correct. DDL statements are used to define or modify the structure of database objects like tables, indexes, etc. and they are executed like normal SQL statements.

B. O (b) To execute DDL statements, you have to install additional support files. This statement is incorrect. There is no requirement for additional support files to execute DDL statements.

C. O (c) DDL statements cannot be executed by making use of JDBC, you should use the native database SQL interface. This statement is incorrect. DDL statements can be executed using JDBC (Java Database Connectivity) API like any other SQL statement.

D. O (d) Support for DDL statements will be a feature of a future release of JDBC. This statement is incorrect. DDL statements are already supported by JDBC.

Therefore, the correct option is:

The Answer is: A

Given the following to JSP files select the right answers. ////////// different file Hello

  1. 1) File container.jsp will compile if the directive page comes before the directive include

  2. 2) File container will compile and when executed it will show:”Hello”.

  3. 3) File container.jsp will compile if the errorPage in container.jsp is the same as in file included.jsp.

  4. 4) File container.jsp will compile if instead of directive include () it is used the action include ()


Correct Option: D
  1. 1) It is necessary to implement TagExtraInfo interface.

  2. 2) You have to insert into the tag element of the taglib descriptor file an entry for tei-class element.

  3. 3) The interface you have to implement has a method called getVariableInfo.

  4. 4) None of the above.


Correct Option: A,B,C
  1. 1) It won't compile.

  2. 2) It is a valid jsp line and it will print the variable called name.

  3. 3) It will compile but it will always produce null as the output.

  4. 4) It will work if you create a javabean class with only one variable of type java.lang.String.


Correct Option: B
- Hide questions