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

  2. this

  3. do

  4. that

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

The this keyword in C# refers to the current instance of the class in which it is used, allowing access to its members. Distractors like call and do are keywords reserved for other operations, while that is not a valid C# keyword for referencing the current object.

Multiple choice technology programming languages
  1. Invoke()

  2. StartInvoke()

  3. BeginInvoke()

  4. SetInvoke()

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

BeginInvoke() is the correct method for asynchronous delegate invocation in C#. It initiates the delegate call on a thread pool thread and returns an IAsyncResult object for tracking. Invoke() executes synchronously, while StartInvoke() and SetInvoke() are not standard C# delegate methods.

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

  2. Synchronous

  3. Synchronous and Asynchronous

  4. None of above

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

In SAP BDC (Batch Data Communication), the Session Method is Synchronous. When a session is created and processed, it waits for the session to complete before proceeding. This is different from Call Transaction which can be Asynchronous. Session method ensures data integrity through synchronous processing.

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

Multiple choice technology web technology
  1. True

  2. False

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

The correct answer is:

B. False

StringBuffer objects in Java can be modified after they are created. StringBuffer is a mutable class that allows for the modification of its content. It provides various methods to append, insert, delete, and modify the character sequence it holds. Therefore, the statement "StringBuffer objects once created can not be modified" is false.

Multiple choice technology programming languages
  1. True

  2. False

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

Exception handling in Java is designed to REMOVE error-processing code from the main line of a program's logic, not keep it in the main line. Try-catch blocks separate normal execution flow from error handling, improving program clarity by keeping the main logic clean and focused. The statement claims the opposite of what exception handling actually does.

Multiple choice technology programming languages
  1. Checked exceptions must be explicitly caught or propagated(declared Thrown)

  2. Unchecked exceptions must be explicitly caught or propagated(declared Thrown)

  3. Checked exceptions in Java extend the java.lang.Exception class

  4. Unchecked exceptions extend the java.lang.RuntimeException class

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

Checked exceptions (like IOException) must be caught or declared in throws clause - this is enforced at compile-time. Unchecked exceptions (RuntimeException and its subclasses) do NOT require explicit handling. Checked exceptions extend Exception (not RuntimeException), while unchecked exceptions extend RuntimeException (which itself extends Exception).

Multiple choice technology programming languages
  1. True

  2. False

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

By convention, exception class names should end with 'Exception' (e.g., IOException, NullPointerException), but this is not a strict requirement - it's a naming convention for clarity. Some exception types like Error and its subclasses don't follow this pattern. The statement claims it's a strict requirement, which is false - it's a convention, not a rule.

Multiple choice technology programming languages
  1. ArithmeticException

  2. RunTimeException

  3. NumberFormatException

  4. NumberError

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

To solve the question, the user needs to know what parseInt() method does. The parseInt() method is used to convert a string to an integer. If the string argument cannot be parsed as an integer, parseInt() throws a NumberFormatException.

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

A. NumberError: There is no exception called NumberError in Java. This option is incorrect.

B. ArithmeticException: The ArithmeticException is thrown when an exceptional arithmetic condition has occurred. Since parseInt() throws a NumberFormatException, this option is incorrect.

C. RunTimeException: RuntimeException is a superclass of all classes that represent exceptional conditions that occur during the execution of Java code. Since NumberFormatException is a subclass of RuntimeException, this option is partially correct. However, the specific exception thrown by parseInt() is NumberFormatException.

D. NumberFormatException: This is the correct answer. When parseInt() gets illegal data, it throws a NumberFormatException. This exception is thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.

Therefore, the answer is: D. NumberFormatException.