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

Calling run directly on a Thread executes the method synchronously in the current thread instead of starting a new thread of execution. Therefore, pm1 completes its loop printing one twice before pm2 starts its loop printing two twice.

Multiple choice technology programming languages
  1. Extend Thread class

  2. Implement Runnable interface

  3. Both

  4. None

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

Implementing the Runnable interface is preferred over extending the Thread class in Java because Java does not support multiple inheritance. By implementing an interface, the class remains free to inherit from another class. This approach also promotes better separation of concerns between the task execution logic and the thread mechanism.

Multiple choice technology programming languages
  1. Extend Thread class

  2. Implement Runnable interface

  3. Both

  4. None

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

Implementing Runnable interface is preferred over extending Thread class because Java supports only single inheritance - by implementing Runnable, your class can still extend another class if needed. It also separates the task logic from the thread execution mechanism, following better object-oriented design principles. Extending Thread should only be used when you need to override Thread methods other than run().

Multiple choice technology programming languages
  1. foo.notify();

  2. bar.notify();

  3. foo.notifyAll();

  4. Thread.notify();

  5. bar.notiFYAll();

  6. Object.notify();

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

Since thread foo is executing bar.wait(), it's waiting on bar's monitor. To wake it, you must call notify() or notifyAll() on the SAME object (bar). bar.notifyAll() is the most reliable as it wakes all waiting threads, ensuring foo stops waiting.

Multiple choice technology programming languages
  1. Arrays.sort(s);

  2. s = new TreeSet(s);

  3. Collections.sort(s);

  4. s = new SortedSet(s);

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

HashMap keys are unordered. Wrapping the keySet in a TreeSet (new TreeSet(s)) automatically sorts elements because TreeSet maintains natural ordering. Arrays.sort() requires arrays, Collections.sort() requires Lists, and SortedSet is an interface.

Multiple choice technology programming languages
  1. Deserialization

  2. Serialization

  3. Interface

  4. All of the Above

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

Serialization is the process of converting an object's state into a byte stream for storage or transmission. Deserialization is the reverse process - reconstructing the object from the byte stream. Option B correctly identifies that writing object state to a byte stream is called serialization.

Multiple choice technology programming languages
  1. String x = (String) pageContext.getAttribute("foo");

  2. String x = (String) pageContext.getRequestScope("foo");

  3. It is NOT possible to access the pageContext object from within doStartTag.

  4. String x = (String)

Reveal answer Fill a bubble to check yourself
A Correct answer
Multiple choice technology programming languages
  1. java.util.Queue

  2. java.util.ArrayList

  3. java.util.LinearList

  4. java.util.LinkedList

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

java.util.LinkedList implements the List interface and stores elements in a doubly-linked list, allowing O(1) time complexity when adding elements to the beginning via add(0, object). However, it does not support quick random access (O(N)), meeting all requirements. ArrayList has O(N) insertion at index 0.

Multiple choice technology programming languages
  1. True

  2. Not true

  3. An exception is thrown at runtime.

  4. An exception is thrown at runtime

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

Boolean.valueOf("True") returns a Boolean object wrapping true. When used in an if condition, any non-null Boolean evaluates to true, so the if block executes and prints "True". The method parses the string "True" as truthy.

Multiple choice technology programming languages
  1. 1

  2. 2

  3. 3

  4. 5

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

Five String objects are created: the literal "Fred" (1), "47" (2), concatenation "Fred47" (3), substring "ed4" (4), and uppercase "ED4" (5). The toString() call on line 16 doesn't create a new String since it just returns the object itself.

Multiple choice technology programming languages
  1. text.txt will be created and overwrite the old file

  2. file will not be created

  3. exception will be thrown

  4. none of the above

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

File.createNewFile() creates the file ONLY if it does not already exist. If the file exists (like test.txt in this case), the method returns false and does nothing - it does NOT overwrite. This method only throws IOException if there's a directory issue or permission problem, not if the file exists. Option A is wrong because existing files are not overwritten.

Multiple choice technology programming languages
  1. Declare the variable as volatile.

  2. not possible

  3. By java.util.*;

  4. By final keyword

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

The volatile keyword tells the JVM that a variable may be modified by multiple threads asynchronously. This prevents threads from caching the variable's value in CPU registers, ensuring all reads and writes go directly to main memory where all threads can see the latest value.

Multiple choice technology programming languages
  1. Add the List elements to Set

  2. List doesnt allow that

  3. using remove()

  4. none of these

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

A Set in Java automatically removes duplicates because it only stores unique elements. By adding all elements from a List to a Set (like HashSet), duplicates are automatically eliminated. You can then convert back to a List if needed.