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