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

  2. Vector

  3. HashMap

  4. TreeMap

  5. None of the above

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

The question requires storing currencies (keys) with USD prices (values) in a thread-safe manner. HashSet and HashMap are not synchronized. Vector is synchronized but is a List, not a Map. TreeMap is not inherently synchronized. Hashtable would be the correct synchronized Map choice, but it's not listed. Therefore 'None of the above' is correct.

Multiple choice technology programming languages
  1. java.util.HashSet

  2. java.util.LinkedHashMap

  3. java.util.Hashtable

  4. java.util.Vector

  5. none of these

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

The question asks for a collection that maintains entries by last-accessed order. HashSet doesn't maintain any access order. Hashtable is synchronized but doesn't track access order. Vector maintains insertion order, not access order. LinkedHashMap with accessOrder=true would be correct, but plain LinkedHashMap (default) maintains insertion order, not access order. Hence none of these are correct.

Multiple choice technology programming languages
  1. Throw AssertionError

  2. Throw IllegalStateException

  3. Throw IllegalArgumentException

  4. Throw InvalidArgumentException

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

In Java, IllegalArgumentException is the standard runtime exception thrown to indicate that a method has been passed an illegal or inappropriate argument. AssertionError is for internal invariants, and IllegalStateException is for invalid object states.

Multiple choice technology programming languages
  1. A thread dies as soon as the execution of the start() method ends.

  2. A thread's priority can be specified as an argument to its constructor

  3. A sleeping thread can be made runnable by invoking notify().

  4. A thread can call notify() on an object only if it holds the lock on that object.

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

A thread continues running after start() completes; it executes the run() method. Thread priority is set via setPriority(), not constructor (in Java). notify() wakes waiting threads, not sleeping ones. Option D is correct because Java requires holding an object's lock before calling notify() on it.

Multiple choice technology programming languages
  1. Exactly 10 seconds after the start method is called, "Time's Up!" will be printed.

  2. Exactly 10 seconds after "Start!" is printed, "Time's Up!" will be printed.

  3. The delay between "Start!" being printed and "Time's Up!" will be 10 seconds plus or minus one tick of the system clock.

  4. If "Time's Up!" is printed you can be sure that at least 10 seconds have elapsed since "Start!" was printed.

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

Thread.sleep(10000) guarantees that the thread will sleep for at least 10,000 milliseconds (10 seconds) before resuming, unless interrupted. It does not guarantee an exact execution time due to OS scheduling overhead, but it ensures at least 10 seconds have elapsed.

Multiple choice technology programming languages
  1. It is an instance method of Object class.

  2. It is a static method of the Object class

  3. It is an instance method of Thread class.

  4. The Thread must have a lock on the object on which the wait() method is to be invoked

  5. An object can have only one Thread in a waiting state at a time

  6. It must be called in a synchronized code

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

wait() is an instance method of Object class (A), must be called in synchronized context (F), and requires the calling thread to hold the object's lock (D). It's not static (B), not in Thread class (C), and multiple threads can wait on the same object (E). Three options are correct.

Multiple choice technology programming languages
  1. Static methods cannot be synchronized

  2. Synchronized methods cannot make calls to non-synchronized methods

  3. A synchronized method can be overridden to be non-synchronized and vice versa

  4. When a thread is executing a synchronized method, other threads can freely access non-synchronized methods of the same object

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

In Java, synchronized methods can be overridden to be non-synchronized (and vice versa) because synchronization is a property of the implementation, not the method signature. Additionally, while a thread holds a lock on a synchronized method, other threads can freely access non-synchronized methods.

Multiple choice technology programming languages
  1. Null Pointer Exception at client side

  2. Client will receive warning message

  3. ConnectException is thrown

  4. Compilation Error

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

When a client attempts to connect to a server that is not running, Java throws a ConnectException. This is a runtime exception indicating the connection was refused because no server is listening at the specified host and port.

Multiple choice technology programming languages
  1. final

  2. transient

  3. public

  4. private

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

The transient modifier in Java tells the JVM to skip that field during serialization - it won't be written to the persistent stream. This is useful for fields that can be recalculated, are derived from other data, contain sensitive information, or reference non-serializable objects.

Multiple choice technology programming languages
  1. Map

  2. HashMap

  3. Dictionary

  4. Vector

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

Hashtable extends the abstract Dictionary class, which was part of Java's original collection framework in version 1.0. While Dictionary is now deprecated and Hashtable implements the Map interface (since Java 2), Dictionary remains its direct superclass in the class hierarchy.

Multiple choice technology programming languages
  1. Its return type is String

  2. It reads the password from the console

  3. It has echoing disabled

  4. It takes a single argument

  5. None of them

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

Console.readPassword() reads password input from the console with echoing disabled (characters don't appear on screen). It returns char[] not String for security (can be cleared from memory).