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 architecture
  1. java.util.List

  2. java.lang.String

  3. java.io.BufferedInputStream.

  4. None of the above.

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

The Decorator pattern attaches additional responsibilities to an object dynamically. In Java's I/O streams, BufferedInputStream decorates an underlying InputStream to add buffering functionality. This allows adding behavior to individual objects without affecting other objects of the same class.

Multiple choice technology architecture
  1. Strategy Pattern.

  2. Template Pattern.

  3. Observer pattern.

  4. None of the above.

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

The Observer pattern defines a one-to-many dependency between objects so that when one object changes state, all dependents are notified. Java's AWT and Swing use this in their event model - listeners (observers) register with components (subjects) to receive event notifications.

Multiple choice technology architecture
  1. The capability of an object to exist after the program that created it has stopped running.

  2. The ability of a class to support multiple threads.

  3. An Error-Handling technique.

  4. None of the above.

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

Persistence means that data continues to exist after the application or process that created it has terminated. This is achieved by saving the data to durable storage like files, databases, or other non-volatile media. The object can later be reconstructed from this stored state.

Multiple choice technology architecture
  1. Call System.gc()

  2. Garbage collection cannot be forced.

  3. Call System.gc(), passing in a reference to the object to be garbage-collected.

  4. Call Runtime.gc()

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

You cannot force garbage collection in Java - you can only suggest it using System.gc() or Runtime.gc(). The JVM makes the final decision based on memory pressure and its own optimization strategies. Even when gc() runs, it may not collect all eligible objects. This design allows for better performance tuning.

Multiple choice technology architecture
  1. get(1);

  2. get(2);

  3. get(“Chance”);

  4. None of the above.

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

To solve this question, the user needs to know about Vector methods and how they work.

When a string is added to a Vector, it is assigned an index based on the order in which it was added. In this case, "Tinker" would be assigned an index of 0, "Evers" would be assigned an index of 1, and "Chance" would be assigned an index of 2.

The method removeElement("Evers") will remove the string "Evers" from the Vector and shift the index of "Chance" down by one, so it will now have an index of 1.

Now let's go through each option and explain why it is right or wrong:

A. get(1): This option will retrieve the string at index 1 of the Vector, which is now "Chance". This option is correct.

B. get(2): This option will retrieve the string at index 2 of the Vector, which is now out of bounds since the vector size is now 2. This option is incorrect.

C. get("Chance"): This option is not a valid method for retrieving elements from a Vector. This option is incorrect.

D. None of the above: This option is incorrect because option A is correct.

Therefore, the answer is: A. get(1);

Multiple choice technology architecture
  1. String [] strArr = [“element 1”, “element 2];

  2. String [] strArr = {“element 1”, “element 2”};

  3. String [] strArr = (“element 1”, “element2”);

  4. String [] strArr = new String [“element 1” , “element 2”];

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

Java uses curly braces for array initialization, not square brackets or parentheses. The correct syntax is String[] strArr = {"element 1", "element 2"};. This creates an array with the specified elements. You can also use new String[]{"element 1", "element 2"} but the short form with braces is more concise.

Multiple choice technology architecture
  1. Opens a Request to the server.

  2. Returns the value of an HTTP header.

  3. Aborts the HTTP Request.

  4. None of the above.

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

The send() method actually transmits the HTTP request to the server (with optional data payload). Option A describes open() which initializes the request. Option B describes getResponseHeader() which retrieves header values. Option C describes abort() which cancels the request. Since none of A-B-C correctly describe send(), D is the correct choice.

Multiple choice technology architecture
  1. Sends an HTTP request to the server.

  2. Returns all of the HTTP headers.

  3. Sets the name and value of an HTTP Header.

  4. Opens a request to the server.

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

The setRequestHeader() method sets a custom HTTP header (name-value pair) that will be sent with the request. It must be called AFTER open() but BEFORE send(). Option A describes send(), Option B describes getAllResponseHeaders(), and Option D describes open().

Multiple choice technology architecture
  1. xmlHttp.responseBody

  2. xmlHttp.readyState.

  3. xmlHttp.statusText.

  4. xmlHttp.responseText.

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

The responseText property contains the response body from the server as a string. The responseBody property (option A) is not a standard JavaScript XMLHttpRequest property. readyState (option B) indicates the request state (0-4). statusText (option C) contains the status message (e.g., 'OK').

Multiple choice technology architecture
  1. java.util.List

  2. java.lang.String

  3. java.io.BufferedInputStream.

  4. None of the above.

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

java.io.BufferedInputStream wraps another InputStream (like FileInputStream) to add buffering functionality. Both implement the InputStream interface, and BufferedInputStream adds behavior (buffering for performance) without changing the underlying stream - this is the Decorator pattern in action. java.util.List and java.lang.String are not examples of Decorator.

Multiple choice technology architecture
  1. Strategy Pattern.

  2. Template Pattern.

  3. Observer pattern.

  4. None of the above.

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

Java's AWT/Swing event model uses the Observer pattern (also called Listener pattern in this context). Components (subjects) maintain lists of listeners (observers) and notify them when events occur (like button clicks). Listeners implement event listener interfaces and register with components to receive notifications. Strategy and Template patterns don't describe this publish-subscribe relationship.