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. 1 and 2

  2. 2 and 3

  3. 3 and 4

  4. 1 and 4

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

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

Multiple choice
  1. wait()

  2. notify()

  3. notifyall()

  4. exits synchronized code

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

Option 1 is correct. wait() causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. Option 2 is wrong. notify() - wakes up a single thread that is waiting on this object's monitor. Option 3 is wrong. notifyAll() - wakes up all threads that are waiting on this object's monitor. Option 4 is wrong. Typically, releasing a lock means the thread holding the lock (in other words, the thread currently in the synchronized method) exits the synchronized method. At that point, the lock is free until some other thread enters a synchronized method on that object. Exiting synchronized code does not directly stop the execution of a thread.

Multiple choice
  1. The program will not compile.

  2. The program will print Hello world, then will print that a RuntimeExceptionhas occurred, then will print Done with try block, and then will print Finally executing.

  3. The program will print Hello world, then will print that a RuntimeExceptionhas occurred, and then will print Finally executing.

  4. The program will print Hello world, then will print Finally executing, then will print that a RuntimeException has occurred.

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

Once the program throws a RuntimeException (in the throwit() method) that is not caught, the finally block will be executed and the program will be terminated. If a method does not handle an exception, the finally block is executed before the exception is propagated.

Multiple choice
  1. Calling Runtime.gc() will cause eligible objects to be garbage collected.

  2. The garbage collector uses a mark and sweep algorithm.

  3. If an object can be accessed from a live thread, it can't be garbage collected.

  4. If object 1 refers to object 2, then object 2 can't be garbage collected.

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

This is a great way to think about when objects can be garbage collected. Option A and B assume guarantees that the garbage collector never makes. Option D is wrong because of the now famous islands of isolation scenario.

Multiple choice
  1. java.util.HashSet

  2. java.util.LinkedHashSet

  3. java.util.List

  4. java.util.ArrayList

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

All of the collection classes allow you to grow or shrink the size of your collection.ArrayList provides an index to its elements. The newer collection classes tend not to have synchronized methods. Vector is an older implementation of ArrayListfunctionality and has synchronized methods; it is slower than ArrayList.

Multiple choice
  1. notify()

  2. wait()

  3. InputStream access

  4. sleep()

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

Option A is correct. notify() - wakes up a single thread that is waiting on this object's monitor. Option B is wrong. wait() causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. Option C is wrong. Methods of the InputStream class block until input data is available, the end of the stream is detected, or an exception is thrown. Blocking means that a thread may stop until certain conditions are met. Option D is wrong. sleep() - Causes the currently executing thread to sleep (temporarily cease execution) for a specified number of milliseconds. The thread does not lose ownership of any monitors.

Multiple choice
  1. Memory is reclaimed by calling Runtime.gc().

  2. Objects are not collected if they are accessible from live threads.

  3. An OutOfMemory error is only thrown if a single block of memory cannot be found that is large enough for a particular requirement.

  4. Objects that have finalize() methods always have their finalize()methods called before the program ends.

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

Option B is correct. If an object can be accessed from a live thread, it can't be garbage collected. Option A is wrong. Runtime.gc() asks the garbage collector to run, but the garbage collector never makes any guarantees about when it will run or what unreachable objects it will free from memory. Option C is wrong. The garbage collector runs immediately the system is out of memory before an OutOfMemoryException is thrown by the JVM. Option D is wrong. If this were the case then the garbage collector would actively hang onto objects until a program finishes - this goes against the purpose of the garbage collector.

Multiple choice
  1. If only one thread is blocked in the wait method of an object, and another thread executes the modify on that same object, then the first thread immediately resumes execution.

  2. If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, it is still possible that the first thread might never resume execution.

  3. If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, then the first thread definitely resumes execution as a direct and sole consequence of the notify call.

  4. If two threads are blocked in the wait method of one object, and another thread executes the notify method on the same object, then the first thread that executed the wait call first definitely resumes execution as a direct and sole consequence of the notify call.

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

Option B is correct - The notify method only wakes the thread. It does not guarantee that the thread will run. Option A is incorrect - just because another thread activates the modify method in A this does not mean that the thread will automatically resume execution Option C is incorrect - This is incorrect because as said in Answer B notify only wakes the thread but further to this once it is awake it goes back into the stack and awaits execution therefore it is not a "direct and sole consequence of the notify call" Option D is incorrect - The notify method wakes one waiting thread up. If there are more than one sleeping threads then the choice as to which thread to wake is made by the machine rather than you therefore you cannot guarantee that the notify'ed thread will be the first waiting thread.

Multiple choice
  1. run();

  2. start();

  3. stop();

  4. main();

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

Option A is Correct. The run() method to a thread is like the main() method to an application. Starting the thread causes the object's run method to be called in that separately executing thread. Option B is wrong. The start() method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. Option C is wrong. The stop() method is deprecated. It forces the thread to stop executing. Option D is wrong. Is the main entry point for an application.

Multiple choice
  1. int

  2. byte

  3. long

  4. double

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

However A, B and C are all wrong. Each of these would result in a narrowing conversion. Whereas we want a widening conversion, therefore the only correct answer is D. Don't be put off by the long cast, this applies only to the variable x and not the rest of the expression. It is the variable y (of type double) that forces the widening conversion todouble. Java's widening conversions are:

  • From a byte to a short, an int, a long, a float, or a double.
  • From a short, an int, a long, a float, or a double.
  • From a char to an int, a long, a float, or a double.
  • From an int to a long, a float, or a double.
  • From a long to a float, or a double.
  • From a float to a double.