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

  2. no

  3. DO NOT SELECT 1

  4. DO NOT SELECT 2

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

The code is NOT legal. In Java, catch blocks must be ordered from most specific to most general. Since ExceptionB extends ExceptionA, catching ExceptionA first would also catch any ExceptionB instances, making the second catch block unreachable (a compile-time error). To fix this, reverse the order: catch ExceptionB first, then ExceptionA.

Multiple choice technology programming languages
  1. yes

  2. no

  3. DO NOT SELECT 1

  4. DO NOT SELECT 2

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

The code IS legal. When overriding a method, Java allows the subclass to narrow the declared exception types - ExceptionB is more specific than ExceptionA (its parent). This follows the principle that subclasses can be more restrictive but not more permissive than the superclass contract.

Multiple choice technology programming languages
  1. An exception is thrown if you attempt to add an element with a duplicate value.

  2. The add method returns false if you attempt to add an element with a duplicate value.

  3. A set may contain elements that return duplicate values from a call to the equals method.

  4. Duplicate values will cause an error at compile time.

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

Java's Set collections prevent duplicates by using the add method. Instead of throwing an exception, Set.add() simply returns false if the element is already present, indicating that the collection was not modified. It does not produce compile-time or runtime errors for duplicate additions.

Multiple choice technology programming languages
  1. yes

  2. no

  3. DO NOT SELECT 1

  4. DO NOT SELECT 2

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

Java requires catch blocks to be ordered from most specific to most general. ExceptionB extends ExceptionA, so ExceptionB is more specific. Catching ExceptionA first would make the ExceptionB catch unreachable since any ExceptionB would already be caught by ExceptionA. This causes a compilation error.

Multiple choice technology programming languages
  1. yes

  2. no

  3. DO NOT SELECT 1

  4. DO NOT SELECT 2

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

When overriding a method, the child class can declare a narrower subset of exceptions. ExceptionB extends ExceptionA, so throwing ExceptionB is more restrictive than throwing ExceptionA. This is valid - the child is promising to throw fewer types of exceptions than the parent declared.

Multiple choice technology programming languages
  1. The program exits via a call to System.exit(0);

  2. Another thread is given a higher priority.

  3. A call to the thread's stop method.

  4. A call to the halt method of the Thread class.

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

The Thread class does not have a halt() method. The stop() method exists (though deprecated), and System.exit() terminates the entire JVM. Changing thread priority affects scheduling but doesn't stop a thread. Option D refers to a non-existent method, so it cannot cause a thread to stop executing.

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

The code calls run() directly instead of start(), so it executes in the main thread, not as separate threads. Each Pmcraven runs completely before the next begins: first pm1 prints 'one' twice, then pm2 prints 'two' twice. There is no actual concurrent execution here - it's sequential method calls.

Multiple choice technology programming languages
  1. The program exits via a call to System.exit(0);

  2. Another thread is given a higher priority.

  3. A call to the thread's stop method.

  4. A call to the halt method of the Thread class.

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

A thread can stop executing if the program exits, if it's explicitly stopped with Thread.stop(), or if it is yielded to a higher priority thread. However, there is no halt method in the Thread class (which is why it fails to cause a thread to stop executing). Runtime.getRuntime().halt() exits the JVM, but it is not a method on Thread.

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

The code calls run() directly on each Pmcraven object instead of start(). This means the code executes sequentially in the main thread, not as separate threads. Each run() method prints the thread name twice before returning, producing 'one one' then 'two two'.

Multiple choice technology programming languages
  1. An exception is thrown if you attempt to add an element with a duplicate value.

  2. The add method returns false if you attempt to add an element with a duplicate value.

  3. A set may contain elements that return duplicate values from a call to the equals method.

  4. Duplicate values will cause an error at compile time.

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

The Set interface prevents duplicate elements. When attempting to add an element already present in the set, the add method simply returns false and leaves the set unchanged. It does not throw exceptions or cause compile-time errors.

Multiple choice technology programming languages
  1. yes

  2. no

  3. DO NOT SELECT 1

  4. DO NOT SELECT 2

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

The catch block order violates Java's rule: more specific exceptions must be caught before their parent types. ExceptionB is a subclass of ExceptionA, so catching ExceptionA first makes the ExceptionB handler unreachable code. This causes a compilation error.

Multiple choice technology programming languages
  1. yes

  2. no

  3. DO NOT SELECT 1

  4. DO NOT SELECT 2

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

When overriding a method, the subclass can narrow the declared exception types. ExceptionB is a subclass of ExceptionA, so throwing ExceptionB is compatible with the parent's throws ExceptionA clause. This is legal overriding.

Multiple choice technology programming languages
  1. yes

  2. no

  3. DO NOT SELECT 1

  4. DO NOT SELECT 2

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

This is a duplicate of question 149712. The catch blocks are in the wrong order: the parent ExceptionA is caught before the child ExceptionB, making the ExceptionB handler unreachable. This violates Java's exception handling rules and causes a compilation error.

Multiple choice technology programming languages
  1. yes

  2. no

  3. DO NOT SELECT 1

  4. DO NOT SELECT 2

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

Java allows overriding methods to declare narrower checked exceptions than those declared by the superclass method. Since ExceptionB extends ExceptionA, the subclass method throws a more specific exception, which complies with Java overriding rules and compiles without issue.

Multiple choice technology programming languages
  1. An exception is thrown if you attempt to add an element with a duplicate value.

  2. The add method returns false if you attempt to add an element with a duplicate value.

  3. A set may contain elements that return duplicate values from a call to the equals method.

  4. Duplicate values will cause an error at compile time.

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

To answer this question, the user needs to understand the basic functionality of the Set collection in Java.

Option A is incorrect. When attempting to add an element to a Set that already exists in the Set, an exception is not thrown. Instead, the Set simply ignores the duplicate element and does not add it to the collection.

Option B is correct. The add method of a Set returns a boolean value of false if the element being added already exists in the Set. This is because Sets do not allow duplicate elements.

Option C is incorrect. Sets are designed to contain only unique elements. If an element already exists in the Set, it will not be added again. Therefore, a Set cannot contain elements that return duplicate values from a call to the equals method.

Option D is incorrect. Duplicate values will not cause an error at compile time. Instead, the Set will simply ignore the duplicate value when the code is executed.

Therefore, the correct answer is:

The Answer is: B. The add method returns false if you attempt to add an element with a duplicate value.