Computer Knowledge

Java Core Classes and Threads

1,935 Questions

Java core classes and threads form the foundation of object oriented programming and are crucial for IT officer and programming exams. This includes concepts like string mutability, collections, and multithreading. Solve these questions to test your practical coding and theoretical knowledge.

String and StringBuffer classesThread execution methodsJava collections frameworkCharacter streams input outputVariable serialization rules

Java Core Classes and Threads Questions

Multiple choice technology programming languages
  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

Reveal answer Fill a bubble to check yourself
B Correct answer
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.

Multiple choice technology programming languages
  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

Reveal answer Fill a bubble to check yourself
B Correct answer
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.

Multiple choice technology programming languages
  1. 1) java.util.Date

  2. 2) java.sql.Date.

  3. 3) int.

  4. 4) long

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

The getLastModified() method in HttpServlet returns a long value representing milliseconds since the epoch (January 1, 1970). This matches the Last-Modified HTTP header format. It does not return java.util.Date or java.sql.Date objects, nor does it return an int. Option D is correct.

Multiple choice technology programming languages
  1. 1) There is no such method.

  2. 2) It searches the attribute in all 4 scopes.

  3. 3) It searches the attribute in page scope.

  4. 4) The method is findAttribute(String name, int scope)

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

PageContext.findAttribute(String name) searches for an attribute across all four scopes in order: page, request, session, and application. It returns the first match found or null if not found in any scope. Option B correctly describes this behavior. Option C is wrong because it's not limited to page scope only.

Multiple choice technology programming languages
  1. 1) getHeaderNames returns an Enumeration

  2. 2) getHeaders returns a String[]

  3. 3) getHeaderValues returns a String[]

  4. 4) getIntParameter returns an int

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The HttpServletRequest interface specifies getHeaderNames() which returns an Enumeration of String header names. Option A is correct. Option B is wrong because getHeaders(String) returns an Enumeration of String values (all values for a multi-value header), not a String array. Option C is incorrect because getHeaderValues is not a standard method (likely confused with getHeaders). Option D is wrong because there's no getIntParameter method (use getParameter() then parseInt).

Multiple choice technology programming languages
  1. 1) pushBody is called by the jsp container after doStartTag and before doAfterBdy

  2. 2) pushBody is called by the jsp container before doInitBody

  3. 3) popBody is called by the jsp container after doAfterBody and before doEndTag

  4. 4) popBody is called by the jsp container after doEndTag

Reveal answer Fill a bubble to check yourself
A,C Correct answer
Explanation

In JSP custom tags with body content, the JSP container manages out/out nesting using pushBody() and popBody(). pushBody() is called after doStartTag() and before doInitBody/doAfterBody to save the current out. popBody() is called after doAfterBody() and before doEndTag to restore the previous out. Options A and C correctly describe this lifecycle. The timing in options B and D is incorrect.

Multiple choice technology platforms and products
  1. True

  2. False

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Android uses a specialized Java implementation that is NOT Java ME (Micro Edition). While Android apps are written in Java, they run on Dalvik VM (now ART), which uses a different bytecode format (dex) and runtime optimized for mobile devices. Java ME was designed for feature phones and embedded devices, but Android built its own application stack from scratch.

Multiple choice technology testing
  1. A Collection of Objects

  2. A count of the number of Objects on the page

  3. All the Web pages displayed in the browser

  4. None of the above

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The ChildObjects method returns a collection of all child objects that match the specified description properties from a parent object. This enables programmatic retrieval and manipulation of multiple similar objects.

Multiple choice technology
  1. Arrays.sort(s);

  2. s = new TreeSet(s);

  3. Collections.sort(s);

  4. s = new SortedSet(s);

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

To sort the keys from a HashMap, you can create a TreeSet from the keySet() - TreeSet automatically sorts elements using their natural ordering (alphabetical for strings). Arrays.sort() doesn't work on Sets directly, Collections.sort() works on Lists, and SortedSet is an interface that cannot be instantiated directly. Option B correctly uses TreeSet to achieve sorting.

Multiple choice technology
  1. java.lang.RuntimeException: Problem

  2. run.java.lang.RuntimeException: Problem

  3. End of method.java.lang.RuntimeException: Problem

  4. End of method.run.java.lang.RuntimeException: Problem

  5. run.java.lang.RuntimeException: ProblemEnd of method.

Reveal answer Fill a bubble to check yourself
D,E Correct answer
Explanation

Thread execution is non-deterministic. Either main prints "End of method." first (option D), or the thread prints "run." first (option E). The RuntimeException is uncaught in the thread so it just terminates after printing. Options A, B, C are not valid output patterns.

Multiple choice technology
  1. declare reset() using the synchronized keyword

  2. declare getName() using the synchronized keyword

  3. declare getCount() using the synchronized keyword

  4. declare the constructor using the synchronized keyword

  5. declare increment() using the synchronized keyword

Reveal answer Fill a bubble to check yourself
A,C,E Correct answer
Explanation

The count field is mutable and accessed by reset(), getCount(), and increment(). These need synchronization to prevent race conditions. getName() only reads immutable 'name', and constructors don't need sync as the object isn't shared yet.

Multiple choice technology
  1. This code may throw an InterruptedException

  2. This code may throw an IllegalStateException

  3. This code may throw a TimeoutException after ten minutes

  4. This code will not compile unless "obj.wait()" is replaced with "((Thread) obj).wait()".

  5. Reversing the order of obj.wait() and obj.notify() may cause this method to complete normally

  6. A call to notify() or notifyAll() from another thread may cause this method to complete normally

Reveal answer Fill a bubble to check yourself
B Correct answer
Multiple choice technology
  1. Line 92 will not execute.

  2. The connection will not be retrieved in line 85.

  3. The resource connection will not be closed on line 88.

  4. The enclosing method will throw an exception to its caller

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

When ResourceException is thrown on line 86, execution jumps to the catch block at line 89. Line 88 (con.close()) is skipped, leaving the connection unclosed. This is a resource leak.