Java OCA OCP Practice Question 2287

Question

Consider the following code segment.

assume that the connection object is valid and the statement.

executeQuery() method successfully returns a ResultSet object with a few rows in it:.

Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM contact"));

int numOfColumns = resultSet.getMetaData().getColumnCount();

while (resultSet.next()) {
     // traverse the columns by index values
     for(int i = 0; i < numOfColumns; i++) {
             // since we do not know the data type of the column, we use getObject()
            System.out.print(resultSet.getObject(i) + "\t");
     }// w  w  w.ja v a2s  .c om
     System.out.println("");
}

which of the following statements is true regarding this code segment?

A.  the code segment will successfully print the contents of the rows in the ResultSet object.

B.   the looping header is wrong. to traverse all the columns, it should be

     for(int i = 0; i <= numOfColumns; i++) {

C.   the looping header is wrong. to traverse all the columns, it should be

     for(int i = 1; i <= numOfColumns; i++) {

d.   the looping header is wrong. to traverse all the columns, it should be

     for(int i = 1; i < numOfColumns; i++) {


C.

Note

given N columns in a table, the valid column indexes are from 1 to N, not 0 to N - 1.




PreviousNext

Related