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
  1. ClassNotFoundException

  2. ClassNotSupportedException

  3. InstantiationException

  4. UnsupportedOperationException

  5. NoSuchMethodException

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

This is the correct answer. This is an unchecked exception. This occurs when an unsupported action gets encountered.

Multiple choice
  1. It is sometimes good practice to throw an AssertionError explicitly.

  2. Private getter() and setter() methods should not use assertions to verify arguments.

  3. If an AssertionError is thrown in a try-catch block, the finally block will be bypassed.

  4. It is proper to handle assertion statement failures using a catch (AssertionException ae) block.

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

Option 1 is correct because it is sometimes advisable to thrown an assertion error even if assertions have been disabled. Option 2 is incorrect because it is considered appropriate to check argument values in private methods using assertions. Option 3 is incorrect; finally is never bypassed. Option 4 is incorrect because AssertionErrors should never be handled.

Multiple choice
  1. new Runnable(MyRunnable).start();

  2. new Thread(MyRunnable).run();

  3. new Thread(new MyRunnable()).start();

  4. new MyRunnable().start();

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

Because the class implements Runnable, an instance of it has to be passed to theThread constructor, and then the instance of the Thread has to be started. 1 is incorrect. There is no constructor like this for Runnable because Runnable is an interface, and it is illegal to pass a class or interface name to any constructor. 2 is incorrect for the same reason; you can't pass a class or interface name to any constructor. 4 is incorrect because MyRunnable doesn't have a start() method, and the onlystart() method that can start a thread of execution is the start() in the Threadclass.

Multiple choice
  1. 1 and 2

  2. 2 and 3

  3. 1 and 4

  4. 3 and 4

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

There are two ways of creating a thread; extend (sub-class) the Thread class and implement the Runnable interface. For both of these ways you must implement (override and not overload) the public void run() method. (1) is correct - Extending the Thread class and overriding its run method is a valid procedure. (4) is correct - You must implement interfaces, and runnable is an interface and you must also include the run method. (2) is wrong - Runnable is an interface which implements not Extends. Gives the error: (No interface expected here) (3) is wrong - You cannot implement java.lang.Thread (This is a Class). (Implements Thread, gives the error: Interface expected). Implements expects an interface. (5) is wrong - You cannot implement java.lang.Thread (This is a class). You Extend classes, and Implement interfaces. (Implements Thread, gives the error: Interface expected)