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 programming languages
  1. static public void main(String []args)

  2. public static void main(String heyhey[])

  3. public static int main(String[] args)

  4. public static void main(String ...args)

  5. public static void main (String [], int argv)

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

Valid main method signatures must be public static void with a single String array or varargs parameter. Modifier order (static public vs public static) is flexible. Parameter names can be any valid identifier. Return type must be void, and only one String array/varargs parameter is allowed.

Multiple choice technology programming languages
  1. all sub-classes of Throwable

  2. not the sub classes of RuntimeException

  3. all sub-classes of Error

  4. None of these

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

In Java, checked exceptions are subclasses of Exception but are not subclasses of RuntimeException. Subclasses of Error and RuntimeException are unchecked.

Multiple choice technology web technology
  1. The Remote object reference

  2. The Remote object’s stub

  3. A local copy of the remote object

  4. None

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

When passing Remote objects to/from remote methods, the stub (proxy) is passed, not the actual remote object. The stub runs on the client side and communicates with the remote object via RMI. This is fundamental to how Java RMI works.

Multiple choice technology programming languages
  1. Asynchronous

  2. Synchronous

  3. Synchronous and Asynchronous

  4. None of above

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

In SAP, the Session Method (specifically BDC Session Method or Call Transaction) for data transfer is synchronous - it waits for processing to complete before moving to the next record/session. This ensures data integrity and proper error handling during batch data processing.

Multiple choice technology programming languages
  1. RuntimeException

  2. Error

  3. VirtualMachineError

  4. IllegalAccessException

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

In Java, the AssertionError class extends Error, which in turn extends Throwable. The 'is-a' relationship refers to inheritance. AssertionError is used when an assertion fails, and Error is the superclass for serious system-level errors that applications should not try to catch. Option B is correct.

Multiple choice technology programming languages
  1. Vector

  2. HashMap

  3. ArrayList

  4. Hashtable

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

In java.util, Vector and Hashtable are legacy classes (from Java 1.0/1.1) that are synchronized and therefore thread-safe. Modern classes like ArrayList and HashMap (added in Java 1.2) are not thread-safe for performance. ConcurrentHashMap would be a modern thread-safe alternative. Options A and D are correct.

Multiple choice technology programming languages
  1. Thread(Runnable threadob,String threadName)

  2. Thread(String threadob, Runnable threadName)

  3. Thread(Runnable threadob,ArrayList threadName)

  4. Thread(ArrayList threadob,String threadName

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

To solve this question, the user needs to know the syntax and signature of the constructor of the Thread class in Java.

The constructor signature for the Thread class is:

Thread(Runnable threadob, String threadName)

This constructor creates a new thread object with the specified name and the given runnable object.

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

A. Thread(Runnable threadob, String threadName): This option is correct as it matches the signature of the Thread class constructor.

B. Thread(String threadob, Runnable threadName): This option is incorrect because the order of the parameters is reversed, and the first parameter is of type String, which is not valid for the Thread constructor.

C. Thread(Runnable threadob, ArrayList threadName): This option is incorrect because the second parameter should be of type String, not ArrayList.

D. Thread(ArrayList threadob, String threadName): This option is incorrect because the first parameter should be of type Runnable, not ArrayList.

Therefore, the answer is:

The Answer is: A

Multiple choice technology programming languages
  1. Both classes extend Throwable.

  2. The Error class is final and the Exception class is not.

  3. The Exception class is final and the Error is not.

  4. Both classes implement Throwable.

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

Both Error and Exception classes extend Throwable in the Java exception hierarchy. Throwable is the superclass of all errors and exceptions in Java. Neither Error nor Exception is final - both have subclasses. They extend (inherit from) Throwable, not implement it as an interface. Option A is correct.

Multiple choice technology programming languages
  1. True

  2. False

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

To solve this question, the user needs to have knowledge about the Thread class and its methods.

The Thread class is a built-in class in Python that is used to create new threads of execution. A thread is a lightweight process that runs concurrently with other threads in a process.

When creating a subclass of the Thread class, the run() method is the method that is executed when the start() method is called on an instance of the class. The run() method contains the code that will be executed in the new thread.

Therefore, the run() method is necessary in a class created as a subclass of Thread class.

The answer is: A. True.

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 this question, the user needs to be familiar with the parseInt() method and the types of exceptions it can throw.

The parseInt() method is used to convert a string to an integer. If the string contains illegal data (i.e. not a valid integer), the method will throw an exception.

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

A. ArithmeticException: This exception is thrown when an arithmetic operation produces an error, such as dividing by zero. It is not related to the parseInt() method, so this option is incorrect.

B. RunTimeException: This is a general exception that can be thrown at runtime for a variety of reasons. It is not specific to the parseInt() method, so this option is incorrect.

C. NumberFormatException: This is the correct answer. This exception is thrown by parseInt() when the string being parsed contains illegal data. For example, if the string "abc" is passed to parseInt(), a NumberFormatException will be thrown because "abc" is not a valid integer.

D. NumberError: This is not a standard Java exception, so this option is incorrect.

Therefore, the answer is: C. NumberFormatException

Multiple choice technology programming languages
  1. Error, Exception

  2. Exception, RunTimeException

  3. Throwable, RunTimeException

  4. ArithmeticException, RunTimeException

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

To solve the question and determine the order of exception types from most specific to least specific, you need to understand the hierarchy of exception classes in Java.

In Java, exceptions are organized in a hierarchical structure. The topmost class in this hierarchy is Throwable, which is the superclass of all exceptions and errors. The Throwable class has two immediate subclasses: Error and Exception.

Error represents serious issues that typically cannot be handled by your program, such as OutOfMemoryError or StackOverflowError. Errors are usually caused by external factors or problems with the Java Virtual Machine (JVM) itself.

Exception is the superclass of all exceptions that can be thrown by a Java program. It further branches into various subclasses, including RuntimeException and its subclasses.

RuntimeException and its subclasses represent exceptions that occur during the execution of the program and are often caused by logical errors or violations of programming conventions. Examples include NullPointerException or ArithmeticException.

Based on this hierarchy, we can determine the order of exception types from most specific to least specific:

  1. Option D: ArithmeticException, RunTimeException

    • ArithmeticException is a specific type of exception that occurs when an arithmetic operation fails, such as dividing by zero.
    • RuntimeException is a more general class that encompasses a broader range of runtime exceptions.
  2. Option C: Throwable, RunTimeException

    • Throwable is the top-level class in the exception hierarchy and is the superclass of all exceptions and errors.
    • RuntimeException is a specific subclass of Exception.
  3. Option B: Exception, RunTimeException

    • Exception is a broader class that encompasses a wide range of exceptions, including runtime and non-runtime exceptions.
    • RuntimeException is a more specific subclass of Exception that represents runtime exceptions.
  4. Option A: Error, Exception

    • Error is a subclass of Throwable and represents serious issues that usually cannot be recovered from.
    • Exception is a superclass that encompasses both runtime and non-runtime exceptions.

Therefore, the correct order of exception types from most specific to least specific is:

D. ArithmeticException, RunTimeException

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, exactly if there were no try{} block.

  3. The program halts immediately.

  4. The program ignores the exception.

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

If an exception is thrown inside a try block and no matching catch block is found in the current method, the exception propagates up the call stack to the calling method, behaving as if no try block existed.

Multiple choice technology programming languages
  1. Always after execution has left a try{} block, no matter for what reason.

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

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

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

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

The finally block is guaranteed to execute after execution leaves a try block, regardless of whether an exception was thrown, caught, or if the block exited via a return statement.