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

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 data survives beyond the execution of the program that created it. Objects can be stored to disk or database and reconstructed later, allowing data to exist between program runs. Thread support, error handling, and other options are unrelated to the core concept of persistence.

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

Java garbage collection cannot be forced - calling System.gc() or Runtime.gc() only suggests to the JVM that garbage collection should run, but the JVM may ignore the request. Garbage collection runs automatically based on heap pressure and JVM algorithms. This is a fundamental Java concept developers must understand.

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

Initially: [Tinker(0), Evers(1), Chance(2)]. After removeElement('Evers'): [Tinker(0), Chance(1)]. Calling get(1) returns 'Chance' at index 1. get(2) would throw IndexOutOfBoundsException. get('Chance') is invalid syntax - Vector.get() expects int index, not object.

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

In Java, array initialization uses curly braces: String[] strArr = {"element 1", "element 2"}; Option A uses square brackets incorrectly. Option C uses parentheses (invalid for arrays). Option D incorrectly uses 'new String[]' with initialization values - should be 'new String[]{...}' or just {...}.

Multiple choice technology programming languages
  1. StringBuilder

  2. StringBuffer

  3. Both (1) and (2)

  4. None of the above

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

StringBuffer is synchronized (thread-safe) while StringBuilder is not. StringBuffer's methods are synchronized, making it safe for use in multi-threaded environments, though slower than StringBuilder. StringBuilder was introduced in Java 5 as a non-synchronized alternative for single-threaded contexts.

Multiple choice technology programming languages
  1. True

  2. False

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

JVM stands for Java Virtual Machine, not Java Virtual Method. It's an abstract computing machine that enables a computer to run Java programs as well as programs written in other languages compiled to Java bytecode. The False option is correct as 'Machine' replaces 'Method'.