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
  1. assert value == null;

  2. assert value != null, "value is null";

  3. if (value == null) {throw new AssertionException("value is null");}

  4. if (value == null) {throw new IllegalArgumentException("value is null");}

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

IllegalArgumentException is the standard exception for invalid method arguments. Checking if (value == null) and throwing this exception provides clear error handling and prevents the NullPointerException at line 20. Assertions should not be used for parameter validation as they can be disabled at runtime.

Multiple choice technology
  1. restore 400

  2. restore 403

  3. restore 453

  4. Compilation fails.

  5. An exception is thrown at runtime

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

When Banana is serialized, all three fields (yellow=4, juice=5, good=3) are saved. Upon deserialization, the entire object graph is restored. The output concatenates these values: 4 (yellow) + 5 (juice) + 3 (good) = '453'. All three classes in the hierarchy (Food, Fruit, Banana) are Serializable, so the full object state persists.

Multiple choice technology
  1. This code is NOT thread-safe

  2. The programmer can replace StringBuffer with StringBuilder with no other changes

  3. This code will perform poorly. For better performance, the code should be rewritten:return "<" + this.name + ">";

  4. This code will perform well and converting the code to use StringBuilder will not enhance the performance

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

StringBuffer is thread-safe due to synchronization, while StringBuilder is not. Since 'buffer' is a local variable used only within this method, there's no concurrent access - no threads share this instance. Therefore, replacing StringBuffer with StringBuilder removes unnecessary synchronization overhead and improves performance, with identical functionality.

Multiple choice technology programming languages
  1. public T update()

  2. public void update(T updateValue)

  3. void update(T updateValue)

  4. public void update()

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

The correct declaration is 'public void update(T updateValue)'. It has the public modifier, void return type, method name 'update', and accepts a parameter of type T named 'updateValue'. Option A returns T instead of void, C lacks public modifier, D lacks the parameter entirely.

Multiple choice technology programming languages
  1. We can invoke run() method directly to start thread

  2. If we call start() method twice, two threads will be created

  3. t.sleep(50) - it will give minimum guarantee of at least 50ms this thread will not run

  4. Thread can be still alive even after the completion of run().

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

Calling Thread.sleep(50) guarantees that the current thread will sleep for at least 50 milliseconds before returning to the runnable state, meaning it will not run during this minimum duration.

Multiple choice technology programming languages
    1. sleep()
    1. yield()
    1. run()
    1. join()
Reveal answer Fill a bubble to check yourself
A,B,D Correct answer
Explanation

Methods that prevent thread execution: sleep() (pauses thread), yield() (suggests scheduler to switch), join() (waits for another thread to die). Option C (run()) is the thread's entry point but doesn't prevent execution - it's called by the JVM to start the thread. Thus A, B, D are correct.

Multiple choice technology programming languages
    1. It jumps out of the loop back to the calling method.
    1. It jumps out of the loop to the next statement after the loop.
    1. It stops the execution of the program and of the virtual machine.
    1. It stops the innermost iteration and continues with the next iteration of the loop.
Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The break statement immediately terminates the innermost enclosing loop or switch statement and transfers execution to the first statement after that construct. It does not return to the calling method (that's return), nor does it start the next iteration (that's continue).

Multiple choice technology programming languages
    1. Replace lines 7 to 14 with a break statement.
    1. Replace lines 7 to 14 with a continue statement.
    1. Replace line 12 with a continue statement.
    1. Insert a break statement between lines 16 and 17.
Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Replacing line 12 with continue prevents printing 'I'm shivering.' and keeps the loop running. The door closes at line 10, making the while condition false on the next iteration, so the loop exits naturally and the final message prints. This produces exactly the three required messages.

Multiple choice technology programming languages
  1. 1 public void myMethod(int x) {

  2. 2 private void myMethod(int x) {

  3. 3 switch(z) {

  4. 4 private void anotherMethod() {

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

Assertions should be used in private methods (internal invariants that the caller cannot violate) and in switch default cases (to catch unreachable states). Public methods validate arguments via explicit checks, not assertions, since assertions can be disabled. Option A shows a public method without argument validation - inappropriate.

Multiple choice technology programming languages
  1. 1 public class Cup extends ContainerClass {

  2. 2 public class Basket extends ContainerClass {

  3. 3 public class Caldron extends ContainerClass {

  4. 4 public class Bag extends ContainerClass {

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

The overriding method cannot throw checked exceptions broader than those declared in the overridden method. Since ContainerClass.draw() throws IOException, the override must throw IOException or narrower. Option B shows Basket.draw() throwing IOException - valid. Options with no throws clause or broader exceptions are invalid.

Multiple choice technology programming languages
    1. 3
    1. 4
    1. 5
    1. No lines enable the garbage collection of the test object.
Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

An object becomes eligible for garbage collection when there are no more references to it. Line 3 creates the StringBuffer referenced by test. Line 5 sets test to null, removing the only reference. The StringBuffer is now eligible for GC.

Multiple choice technology web technology
  1. OnClick call a server function

  2. retun false on OnClick

  3. return true on OnClientClick

  4. return false on OnClientClick

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

In ASP.NET, the OnClientClick property allows you to run JavaScript before the postback. If the JavaScript function returns 'false', the server-side click event (postback) is cancelled.

Multiple choice technology programming languages
  1. The try-catch block that encloses myref.test(); is mandatory for the code to compile.

  2. Prints: In Question20

  3. Prints: In Question20Sub

  4. Method test() in class Question20Sub has no obligation to declare a throws clause.

  5. An exception is thrown at runtime.

Reveal answer Fill a bubble to check yourself
A,C,D Correct answer
Explanation

The compile-time type of myref is Question20, so its test() method throws Exception, requiring the try-catch (A is true). The runtime object is Question20Sub, so its test() executes, printing 'In Question20Sub' (C is true, B is false). Overriding methods aren't required to declare throws clauses (D is true, E is false).

Multiple choice technology programming languages
  1. a. The elements in a Java array can only be of primitive types, not objects

  2. b. Arrays elements are initialized to default values wherever they are created using the keword new

  3. c. An array may be dynamically resized using the setSize method

  4. d. You can find out the size of an array using the size method

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

Java arrays of objects are valid (String[], Object[], etc.), contradicting A. Array elements ARE initialized to default values (null for objects, 0 for numbers, false for boolean) when created with 'new' - B is true. Arrays have fixed length and cannot be resized - C is false (no setSize method). Array length is a field 'arr.length', not a method - D is false.