Computer Knowledge

Java Core Classes and Threads

1,473 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 web technology
  1. True

  2. False

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

The correct answer is:

B. False

StringBuffer objects in Java can be modified after they are created. StringBuffer is a mutable class that allows for the modification of its content. It provides various methods to append, insert, delete, and modify the character sequence it holds. Therefore, the statement "StringBuffer objects once created can not be modified" is false.

Multiple choice technology programming languages
  1. True

  2. False

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

Exception handling in Java is designed to REMOVE error-processing code from the main line of a program's logic, not keep it in the main line. Try-catch blocks separate normal execution flow from error handling, improving program clarity by keeping the main logic clean and focused. The statement claims the opposite of what exception handling actually does.

Multiple choice technology programming languages
  1. Checked exceptions must be explicitly caught or propagated(declared Thrown)

  2. Unchecked exceptions must be explicitly caught or propagated(declared Thrown)

  3. Checked exceptions in Java extend the java.lang.Exception class

  4. Unchecked exceptions extend the java.lang.RuntimeException class

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

Checked exceptions (like IOException) must be caught or declared in throws clause - this is enforced at compile-time. Unchecked exceptions (RuntimeException and its subclasses) do NOT require explicit handling. Checked exceptions extend Exception (not RuntimeException), while unchecked exceptions extend RuntimeException (which itself extends Exception).

Multiple choice technology programming languages
  1. True

  2. False

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

By convention, exception class names should end with 'Exception' (e.g., IOException, NullPointerException), but this is not a strict requirement - it's a naming convention for clarity. Some exception types like Error and its subclasses don't follow this pattern. The statement claims it's a strict requirement, which is false - it's a convention, not a rule.

Multiple choice technology programming languages
  1. ArithmeticException

  2. RunTimeException

  3. NumberFormatException

  4. NumberError

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

To solve the question, the user needs to know what parseInt() method does. The parseInt() method is used to convert a string to an integer. If the string argument cannot be parsed as an integer, parseInt() throws a NumberFormatException.

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

A. NumberError: There is no exception called NumberError in Java. This option is incorrect.

B. ArithmeticException: The ArithmeticException is thrown when an exceptional arithmetic condition has occurred. Since parseInt() throws a NumberFormatException, this option is incorrect.

C. RunTimeException: RuntimeException is a superclass of all classes that represent exceptional conditions that occur during the execution of Java code. Since NumberFormatException is a subclass of RuntimeException, this option is partially correct. However, the specific exception thrown by parseInt() is NumberFormatException.

D. NumberFormatException: This is the correct answer. When parseInt() gets illegal data, it throws a NumberFormatException. This exception is thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.

Therefore, the answer is: D. NumberFormatException.

Multiple choice technology programming languages
  1. This is not legal, so the program will not compile.

  2. The method throws the exception to its caller if there were no matching catch{} block.

  3. The program halts immediately.

  4. The program ignores the exception.

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

When an exception is thrown in a try block and no matching catch block exists, the exception propagates up the call stack. The method throws the exception to its caller, following Java's exception propagation mechanism. This allows higher-level methods to handle the exception or continue propagating it. The program does not halt immediately, ignore the exception, or fail to compile - this is legal Java code.

Multiple choice technology programming languages
  1. Only when an unhandled exception is thrown in a try{} block.

  2. Only when any exception is thrown in a try{} block.

  3. Always after execution has left a try{} block, no matter for what reason.

  4. Always just as a method is about to finish.

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

A finally block is ALWAYS executed after execution leaves the try block, regardless of how it exits - whether via normal completion, a caught or uncaught exception, return, break, or continue statements. This guarantee makes finally blocks ideal for cleanup code that must run under all circumstances. The finally block executes even when no exception occurs or when the exception is caught.

Multiple choice technology programming languages
  1. True

  2. False

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

In Java, you should never overload the run() method of a Thread class. The start() method specifically looks for the parameterless run() method to execute in a new thread of execution; an overloaded run() method with parameters would be treated as a regular method and will not be invoked concurrently by start().

Multiple choice technology programming languages
  1. java.nio

  2. java.text

  3. java.xml

  4. java.io

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

java.xml is not a standard Java API package in the same sense as the others. The java.nio, java.text, and java.io packages are core Java APIs that have been part of the standard edition since early versions. java.xml typically refers to XML processing libraries which are often in javax.xml or separate packages, not a core java.* API.

Multiple choice technology programming languages
  1. java.io

  2. java.rmi

  3. java.security

  4. None of the above

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

java.security is the correct Java API that provides the security framework. It includes classes and interfaces for authentication, authorization, encryption, digital signatures, and other security features. java.io is for I/O operations, java.rmi is for remote method invocation, making java.security the only security-related package among the options.

Multiple choice technology programming languages
  1. Array Object

  2. Neither Object

  3. String Objec

  4. Both of the Objects

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

Tracing the code: after creating the array and String, assigning 'students[0] = studentName' creates a second reference to 'Peter Parker'. Setting 'studentName = null' only removes one reference. The array object is still referenced by 'students' variable, and the String object is still referenced by 'students[0]'. Neither object loses all references, so neither is eligible for garbage collection.

Multiple choice technology programming languages
  1. X is a method Signature and Y is a method Header

  2. Both X and Y is a method Header

  3. X is a method Header and Y is a method Signature

  4. Both X and Y is a method Signature

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

X is a method header because it includes modifiers (public, final), return type (String), method name (getDetails), parameters (File file, String key), and throws clause. Y is a method signature because it only includes the method name and parameter types without modifiers or return type.

Multiple choice technology programming languages
  1. By invoking the method get (..., String type) on the ResultSet, where type is the database

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

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

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

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

ResultSet provides type-specific getter methods like getString(), getInt(), getBoolean() to retrieve column values by column name or index. The methods handle type conversion automatically. Options A, B, and C describe non-existent methods - there is no generic get() or getValue() method in ResultSet.

Multiple choice technology programming languages
  1. You must catch the checked SQLException which is thrown by the method which executes

  2. You must catch the unchecked SQLWarningException which is thrown by the method

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

  4. You must query the ResultSet object about possible warnings generated by the database

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

SQL warnings in JDBC do not throw exceptions. Developers must call getWarnings() on the Statement, Connection, or ResultSet object to check for them. Distractors suggesting catching exceptions are wrong since warnings are silent.