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
  1. void run()

  2. public void run()

  3. public void start()

  4. void run(int priority)

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

Option B is correct because in an interface all methods are abstract by default therefore they must be overridden by the implementing class. The Runnable interface only contains 1 method, the void run() method therefore it must be implemented. Option A and D are incorrect because they are narrowing the access privileges i.e. package(default) access is narrower than public access. Option C is not method in the Runnable interface therefore it is incorrect.

Multiple choice
  1. 1 and 2

  2. 3 and 5

  3. 4 and 6

  4. 1 and 3

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

Statements (4) and (6) are correct. (4) is correct because the wait() method is overloaded to accept a wait duration in milliseconds. If the thread has not been notified by the time the wait duration has elapsed, then the thread will move back to runnable even without having been notified. (6) is correct because wait()/notify()/notifyAll() must all be called from within a synchronized, context. A thread must own the lock on the object its invokingwait()/notify()/notifyAll() on. (1) is incorrect because wait()/notify() will not prevent deadlock. (2) is incorrect because a sleeping thread will return to runnable when it wakes up, but it might not necessarily resume execution right away. To resume executing, the newly awakened thread must still be moved from runnable to running by the scheduler. (3) is incorrect because synchronization prevents two or more threads from accessing the same object. (5) is incorrect because notify() is not overloaded to accept a duration.

Multiple choice
  1. If assertions are compiled into a source file, and if no flags are included at runtime, assertions will execute by default.

  2. As of Java version 1.4, assertion statements are compiled by default.

  3. With the proper use of runtime arguments, it is possible to instruct the VM to disable assertions for a certain class, and to enable assertions for a certain package, at the same time.

  4. When evaluating command-line arguments, the VM gives -ea flags precedence over -da flags.

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

Option C is true because multiple VM flags can be used on a single invocation of a Java program. Option A is incorrect because at runtime assertions are ignored by default. Option B is incorrect because as of Java 1.4 you must add the argument -source 1.4 to the command line if you want the compiler to compile assertion statements. Option D is incorrect because the VM evaluates all assertion flags left to right.

Multiple choice
  1. BootStrap Classloader, Extension Classloader, System Classloader

  2. BootStrap Classloader, System Classloader, Extension Classloader

  3. Extension Classloader, BootStrap Classloader, System Classloader

  4. Extension Classloader, System Classloader, BootStrap Classloader

  5. None of the above

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

BootStrap is the parent class loader, then comes Extension class loader, and then System class loader.

Multiple choice
  1. Comparator is present in java.Util package and Comparable is present in java.lang Package.

  2. Comparator consists of int Compare(Object o1, Object o2)and Comparable consists of int CompareTo(Object o).

  3. In case of Comparable Sorting, logic must be in the same class whose objects are to be sorted whereas in case of Comparator sorting, logic is in separate class.

  4. All of the above.

  5. None of these

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

All the statements are true.