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 databases
  1. Trap it with a Handler

  2. Propagate it to the Calling Environment

  3. a & then b

  4. b & then a

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

Exception handling in PL/SQL follows a two-phase process: first trap the exception with an exception handler (WHEN clause in the EXCEPTION block), then optionally propagate it to the calling environment if the handler cannot fully resolve it. Option C ('a & then b') correctly captures this sequence - handle first, propagate if needed. Option A is incomplete because trapping alone doesn't complete the handling. Option B is incorrect as the first step must always be handling. Option D reverses the correct order - you cannot propagate before trapping.

Multiple choice technology programming languages
  1. True

  2. False

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

A method can declare throwing multiple exceptions using a comma-separated list in the method signature. For example: 'void myMethod() throws IOException, SQLException, CustomException {...}' is valid syntax. The throws clause can include any number of checked exceptions that the method might throw but not handle internally.

Multiple choice technology programming languages
  1. Garbage collection cannot be forced

  2. Call System.gc()

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

  4. Call Runtime.gc()

  5. Set all references to the object to new values (null, for example)

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

In Java, garbage collection is managed entirely by the JVM and cannot be forced. Calling System.gc() or Runtime.getRuntime().gc() only suggests or requests that the JVM run the garbage collector, but does not guarantee or force its immediate execution.

Multiple choice technology programming languages
  1. DoubleField

  2. int

  3. TextField

  4. String

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

The setLabel method is typically used with UI component objects to set their display label. Among the options, DoubleField represents an object (a UI component field), while int is a primitive and TextField/String are different types. Option A DoubleField is the correct object type that would have a setLabel() method.

Multiple choice technology programming languages
  1. void run();

  2. protected void go();

  3. static void get();

  4. public abstract set();

  5. none of these

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

In Java interfaces, methods are implicitly public and abstract. Thus, void run(); is a valid declaration. Option two is invalid because interface methods cannot be protected. Option three is invalid because static interface methods require a body. Option four is invalid because it lacks a return type.

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

  2. Prints: In TechnoSample

  3. Prints: In TechnoSampleSub

  4. Method test() in class TechnoSampleSub 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 try-catch is mandatory because the reference type TechnoSample has a test() method declaring throws Exception, so callers must handle it. The output is 'In TechnoSampleSub' because Java invokes the subclass method at runtime due to polymorphism. The subclass method can choose not to throw exceptions since overriding methods may declare fewer exceptions than the superclass version.

Multiple choice technology programming languages
  1. many

  2. a few

  3. Compilation fails.

  4. An exception is thrown at runtim

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

The literal 7 is of type int. Java does not perform implicit narrowing of integer arguments to match method parameters of type short or short.... Since there is no matching method that accepts int arguments, the compiler cannot resolve the call, resulting in a compilation failure.

Multiple choice technology programming languages
  1. Mutable (changeable)

  2. Immutable (unchangeable)

  3. Violins and cellos

  4. Rotated and scaled

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

StringBuffer in Java is a mutable sequence of characters, unlike String which is immutable. This means StringBuffer objects can be modified after creation without creating new objects, making them efficient for string manipulation operations.

Multiple choice technology web technology
  1. Vector

  2. TreeMap

  3. TreeSet

  4. HashMap

  5. HashSet

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

To answer this question, the user needs to have knowledge of Java's synchronized collections and multithreading.

Option A: Vector is a synchronized collection class, which means that multiple threads can read and write from it but only one thread can access it at a time. Therefore, unsynchronized read operations are not allowed by multiple threads in Vector.

Option B: TreeMap is not a synchronized collection class, and therefore, it does not provide thread-safety. Multiple threads can read and write to it at the same time, which means that unsynchronized read operations are allowed by multiple threads in TreeMap.

Option C: TreeSet is not a synchronized collection class, and therefore, it does not provide thread-safety. Multiple threads can read and write to it at the same time, which means that unsynchronized read operations are allowed by multiple threads in TreeSet.

Option D: HashMap is not a synchronized collection class, and therefore, it does not provide thread-safety. Multiple threads can read and write to it at the same time, which means that unsynchronized read operations are allowed by multiple threads in HashMap.

Option E: HashSet is not a synchronized collection class, and therefore, it does not provide thread-safety. Multiple threads can read and write to it at the same time, which means that unsynchronized read operations are allowed by multiple threads in HashSet.

Therefore, the answer is: A. Vector