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 platforms and products
  1. UncheckedException

  2. CheckedException

  3. Error

  4. UnspecifiedException

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

NullPointerException extends RuntimeException, making it an unchecked exception. This means developers aren't required to catch or declare it - it typically indicates programming errors like attempting to use a null reference where an object is required.

Multiple choice technology platforms and products
  1. Thread.start()

  2. Thread.run()

  3. Thread.yield()

  4. A dead thread cannot be restarted.

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

Once a Java thread completes its execution (exits its run() method) or is terminated, its state becomes 'TERMINATED' (dead). In JVM, you cannot restart a dead thread; attempting to call start() on it again will throw an IllegalThreadStateException.

Multiple choice technology platforms and products
  1. StringBuilder is immutable but StringBuffer is mutable.

  2. StringBuffer is immutable but StringBuilder is mutable.

  3. StringBuffer is Synchronized but StringBuilder is not.

  4. They both are immutable.

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

StringBuffer is synchronized (thread-safe) while StringBuilder is not. Both classes are mutable - they can be modified after creation. StringBuilder is faster when you don't need thread safety.

Multiple choice technology platforms and products
  1. Implementing Thread interface.

  2. Implementing Cloneable interface.

  3. Implementing the Runnable interface.

  4. We can not create a Thread, JVM has to create it by it's own.

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

A Thread can be created either by extending the Thread class or by implementing the Runnable interface. Option A is wrong because Thread is a class, not an interface. Option D is wrong - we can and do create Threads programmatically.

Multiple choice technology platforms and products
  1. StackOverFlowError

  2. OutOfMemoryError

  3. Process will not be terminate till the time we reboot the machine.

  4. Method will be called only 32768 times.

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

The method callMe() calls itself recursively without a terminating base case, leading to infinite recursion. Each recursive call allocates a stack frame on the call stack until the stack's memory limit is exceeded, throwing a StackOverflowError.

Multiple choice technology programming languages
  1. Both HashMap and Hashtable will allow null as key and value.

  2. HashMap is not Synchronized and Hashtable is Synchronized.

  3. The iterator in the HashMap is fail-safe, but enumerator for the Hashtable is not fail-safe.

  4. Hashtable will be faster than HashMap.

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

HashMap is unsynchronized (not thread-safe) while Hashtable is synchronized (thread-safe), which is the key architectural difference. HashMap allows one null key and multiple null values, but Hashtable does not allow any nulls. Hashtable is slower due to synchronization overhead. HashMap's iterator is fail-fast (not fail-safe) - it throws ConcurrentModificationException if modified during iteration.

Multiple choice technology programming languages
  1. By creating an implementation of the java.lang.Comparable interface.

  2. java.util.Collections.sort()

  3. java.utils.Arrays.sort()

  4. By creating an implementation of the java.lang.Comparator interface.

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

ArrayList can be sorted by implementing Comparable (natural ordering for the object type), using Collections.sort() utility method, or by providing a custom Comparator for custom ordering logic. The option C references java.utils.Arrays.sort() which has a typo (should be java.util.Arrays) and works on arrays not ArrayLists directly.

Multiple choice technology programming languages
  1. String doFileWork() { return "b"; }

  2. String doFileWork() throws IOException ( return "b"; }

  3. String doFileWork(int x) throws IOException { return "b"; }

  4. String doFileWork() throws FileNotFoundException { return "b"; }

  5. String doFileWork() throws NumberFormatException { return "b"; }

  6. String doFileWork() throws NumberFormatException, FileNotFoundException { return "b"; }

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

When overriding a method, the overriding method cannot throw broader or new checked exceptions. It can only throw the same exceptions, subclasses of those exceptions, or unchecked exceptions (RuntimeException). Options A (no exception), D (same FileNotFoundException), E and F (NumberFormatException is unchecked/RuntimeException) are valid. Option B (IOException is broader than FileNotFoundException) violates this rule. Option C is not an override (different parameter list - overloaded method).

Multiple choice technology programming languages
  1. Just L1 is sufficient.

  2. Just L2 is sufficient.

  3. Just L3 is sufficient.

  4. Both L1 and L2 are required.

  5. Both L1 and L3 are required.

  6. Both L3 and L2 are required.

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

The compile-time type of u is Utils (reference type), so it calls Utils.getInt(String) which throws Exception. L1: Declaring main() throws Exception passes the exception up - compiles. L2: Declaring Ping.getInt() throws Exception doesn't help - the call is through Utils reference, not Ping. L3: Wrapping in try/catch handles the exception - compiles. So L1 or L3 alone are sufficient. L2 is ineffective due to polymorphism.

Multiple choice technology web technology
  1. removeChild()

  2. removeChildAt()

  3. removeItem()

  4. deleteAll()

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

In ActionScript/Flex, removeChild() removes a specific child control by reference, while removeChildAt() removes a child by its index position. Both are standard methods for removing controls from a container. Options C and D are incorrect - removeItem() and deleteAll() are not standard Flex control removal methods.

Multiple choice technology programming languages
  1. The program exits via a call to System.exit(0);

  2. Another thread is given a higher priority.

  3. A call to the thread's stop method.

  4. A call to the halt method of the Thread class.

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

The Thread.halt() method was added in Java 9 and causes the thread to stop executing immediately, unlike other methods that might be deprecated or less direct ways to stop a thread.

Multiple choice technology programming languages
  1. The program exits via a call to System.exit(0);

  2. Another thread is given a higher priority.

  3. A call to the thread's stop method.

  4. A call to the halt method of the Thread class.

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

Options A, B, and C all cause threads to stop: System.exit() terminates the JVM, higher priority threads can preempt lower priority ones, and the stop() method (deprecated) stops threads. However, option D refers to 'halt method' - the Thread class has no such method. The correct method is interrupt() for cooperative cancellation, or stop() (deprecated), but halt() does not exist.

Multiple choice technology programming languages
  1. Compile time error, class Rpcraven does not import java.lang.Thread

  2. Output of One One Two Two

  3. Output of One Two One Two

  4. Output of One Two Two One

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

When run() is called directly on a Thread (instead of start()), it executes in the calling thread without creating a new thread. The two Pmcraven objects execute sequentially: pm1's run() completes (printing 'one' twice), then pm2's run() completes (printing 'two' twice). Options C and D incorrectly suggest interleaving, which would only occur with proper thread startup using start().

Multiple choice technology programming languages
  1. An exception is thrown if you attempt to add an element with a duplicate value.

  2. The add method returns false if you attempt to add an element with a duplicate value.

  3. A set may contain elements that return duplicate values from a call to the equals method.

  4. Duplicate values will cause an error at compile time.

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

Set collections maintain uniqueness by contract. When Set.add() is called with a duplicate element (as determined by equals()), the method returns false and the set remains unchanged. No exception is thrown. This allows code to check whether the addition actually modified the set. Options A and C incorrectly suggest exception behavior or allowing duplicates.