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 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 web technology
  1. The AutoEventWireup attribute is set to False.

  2. The AutomaticPostBack attribute is set to False.

  3. comThe codebehind module is not properly piled.

  4. The ListBox must be defined WithEvents.

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

The ListBox's SelectedIndexChanged event only fires automatically when the selection changes if AutoPostBack is set to True. When False (the default), the event only fires during the next postback from another control. Option A refers to automatic event wireup which is unrelated to postback timing. Option C has a typo but codebehind compilation issues would prevent all events.

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 platforms and products
  1. It is no longer necessary to use Line continuation character ( _ ) to continue the code to next line. This is not supported in NET 4.0

  2. Optional parameters can now be nullable

  3. It is now possible to create Jagged Arrays with following syntax: Dim ArrayNewWay = {({"1", "2", "3"}), ({"4", "5"})}

  4. Collections can now be initialized using the following syntax with the new From keyword: Dim Colors As New List(Of String) From {"Red", "Yellow", "Green"}

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

VB.NET 4.0 introduced implicit line continuation, making the underscore character optional in many cases where line breaks would naturally occur (after operators, commas, opening parentheses, etc.). Option A is incorrectly worded but identifies the right concept - line continuation became largely optional in VB.NET 4.0.

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.

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

Java's Set collections prevent duplicates by using the add method. Instead of throwing an exception, Set.add() simply returns false if the element is already present, indicating that the collection was not modified. It does not produce compile-time or runtime errors for duplicate additions.

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 class does not have a halt() method. The stop() method exists (though deprecated), and System.exit() terminates the entire JVM. Changing thread priority affects scheduling but doesn't stop a thread. Option D refers to a non-existent method, so it cannot cause a thread to stop executing.

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

The code calls run() directly instead of start(), so it executes in the main thread, not as separate threads. Each Pmcraven runs completely before the next begins: first pm1 prints 'one' twice, then pm2 prints 'two' twice. There is no actual concurrent execution here - it's sequential method calls.

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

A thread can stop executing if the program exits, if it's explicitly stopped with Thread.stop(), or if it is yielded to a higher priority thread. However, there is no halt method in the Thread class (which is why it fails to cause a thread to stop executing). Runtime.getRuntime().halt() exits the JVM, but it is not a method on Thread.