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 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
D 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 web technology
  1. UI-> Proxy?Provider -> Service-> Database?Mapper

  2. UI-> Service?Service -> Mapper -> Database?Provider

  3. UI-> Proxy -> Service -> Provider -> Database?Mapper

  4. UI-> Service?Provider-> Proxy-> Database?Mapper

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

The correct architectural flow is UI -> Proxy -> Service -> Provider -> Database -> Mapper. This layered architecture ensures proper separation of concerns: the UI communicates through a Proxy layer, which calls the Service layer, which uses a Provider for data access, which interacts with the Database, and finally a Mapper converts between data models. The other options either skip layers or place components in an illogical order.

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 web 2.0
  1. The JavaScript will throw an error in runtime

  2. The JavaScript does not have to wait for the server response

  3. The JavaScript has to wait for the server response

  4. None of the above

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

Setting the third parameter of open() to false specifies a synchronous request, blocking further JavaScript execution until a response is received from the server. By contrast, asynchronous requests allow execution to continue immediately. Distractors describing runtime errors are incorrect.

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.

Multiple choice technology web technology
  1. validate()

  2. reset()

  3. validate() & reset ()

  4. none of these

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

ActionForm defines two key lifecycle methods: validate() for validating form inputs and reset() for resetting form properties to default values. Both methods are called by the controller at appropriate times - validate() before action execution and reset() before form population.

Multiple choice technology web technology
  1. Declarative Exception

  2. programmatic Exception

  3. both declarative and programmatic exception

  4. none of these

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

Struts supports both declarative exception handling (configured in struts-config.xml via global-exceptions and exception tags) and programmatic exception handling (using try-catch in Action classes or extending ExceptionHandler). Both approaches are valid and commonly used.