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

  2. Caught object

  3. Compiler error on line 12

  4. Compiler error on line 2

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

The reference variable target is of type Catchable. When calling target.catchAnObject(now), polymorphism and overload resolution apply. Overload resolution happens at compile-time based on the static type. Since now is a Date object, it matches catchAnObject(Object) at compile time and runs the overridden method in MyStringCatcher printing 'Caught object'.

Multiple choice technology architecture
  1. Object

  2. void

  3. String

  4. Boolean

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

The @PrePassivate annotation in EJB indicates a callback method that is invoked before a stateful session bean is passivated (temporarily serialized to secondary storage). Like other lifecycle callback methods such as @PostConstruct and @PreDestroy, it must return void and throw no checked exceptions.

Multiple choice technology architecture
  1. Yes, we can restart a thread after it has died

  2. No, we can’t restart a thread after it has died. You will have to create a new thread.

  3. None of the above.

  4. Cant say. Not predictable.

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

Once a thread completes execution (dies), it cannot be restarted. The Thread class's start() method can only be called once per thread instance. Attempting to restart a dead thread throws an IllegalThreadStateException. You must create a new Thread instance to run the task again.

Multiple choice technology architecture
  1. public class MyRunnable extends Runnable{public void run(){}}

  2. public class MyRunnable extends Object{public void run(){}}

  3. public class MyRunnable implements Runnable{public void run(){}}

  4. public class MyRunnable implements Runnable{void run(){}}

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

To solve this question, the user needs to have knowledge about the concept of threads in Java and how to create a thread using a Runnable target.

The correct answer is:

C. public class MyRunnable implements Runnable{public void run(){}}

Explanation:

In the given code, we are creating a thread using a Runnable target. The Runnable interface provides a way to implement multi-threading in Java by defining a run() method that will be executed in a separate thread.

To create the target, we need to implement the Runnable interface by providing a definition for the run() method. Option C correctly implements the Runnable interface with a run() method, so it is the correct answer.

Option A is incorrect because it extends the Runnable interface without providing a definition for the run() method.

Option B is incorrect because it extends the Object class and does not implement the Runnable interface.

Option D is incorrect because it implements the Runnable interface but does not provide the run() method with the correct signature (i.e., it should be public void run()).

Therefore, the correct answer is:

The Answer is: C

Multiple choice technology architecture
  1. ClassCastException

  2. IllegalStateException

  3. NumberFormatException

  4. None

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

Integer.parseInt() throws NumberFormatException when the input string cannot be parsed as an integer. 'one' is not a valid numeric string, so this exception must be caught. ClassCastException is for invalid type casts, IllegalStateException for inappropriate invocation state.

Multiple choice technology architecture
  1. 1 2

  2. 1 2 3

  3. Compile error

  4. Runtime exception

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

TreeSet requires elements to be mutually Comparable or a Comparator to be provided. Adding String '2' and '1' works, but adding Integer 3 causes ClassCastException at runtime when TreeSet tries to compare Integer with String. The TreeSet uses natural ordering which fails for mixed types.

Multiple choice technology architecture
  1. addSize

  2. getCust

  3. deleteRep

  4. putDimensions

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

JavaBeans naming conventions for getters: get() for non-boolean properties, is() for boolean properties. Option B (getCust) follows this pattern. Option A (addSize) suggests an add operation, not getter naming. Option C (deleteRep) doesn't follow getter/setter pattern. Option D (putDimensions) suggests a map-like operation, not JavaBean convention.

Multiple choice technology architecture
  1. Query length is limited in POST, not limited in GET.

  2. POST is secured whereas data is been submitted as part of URL in GET (not secured).

  3. By using POST method, we cannot submit the form.

  4. None

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

GET method submits form data as part of the URL in the query string, which is visible in browser history, logs, and can be bookmarked - making it insecure for sensitive data. POST method sends data in the request body, which is not visible in the URL and more secure. Additionally, GET has length limitations (URL length restrictions), while POST can send much larger amounts of data.

Multiple choice technology architecture
  1. public class MyRunnable extends Runnable{public void run(){}}

  2. public class MyRunnable extends Object{public void run(){}}

  3. public class MyRunnable implements Runnable{public void run(){}}

  4. public class MyRunnable implements Runnable{void run(){}}

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

To solve this question, the user needs to have knowledge of Java threads, Runnable interface, and the difference between extending and implementing a class/interface.

The code snippet above creates a Thread using a Runnable target. To use this code snippet, the user needs to create a class that implements the Runnable interface. The Runnable interface has only one method, which is the run() method.

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

A. public class MyRunnable extends Runnable{public void run(){}}

This option is incorrect because the Runnable interface cannot be extended. It can only be implemented.

B. public class MyRunnable extends Object{public void run(){}}

This option is incorrect because it does not implement the Runnable interface. Instead, it extends the Object class and defines a run() method, which is not the correct implementation of the run() method from the Runnable interface.

C. public class MyRunnable implements Runnable{public void run(){}}

This option is correct. It creates a class called MyRunnable that implements the Runnable interface. It defines the run() method, which is required by the Runnable interface. Therefore, this code will compile correctly.

D. public class MyRunnable implements Runnable{void run(){}}

This option is incorrect because the run() method is missing the public access modifier. The run() method must be declared as public since it is an implementation of the run() method from the Runnable interface.

Therefore, the correct answer is:

The Answer is: C

Multiple choice technology architecture
  1. ClassCastException

  2. IllegalStateException

  3. NumberFormatException

  4. None

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

Integer.parseInt throws NumberFormatException when the string cannot be parsed as a valid integer. The string 'one' contains letters, not digits, so parsing fails. ClassCastException is for incompatible type conversions, and IllegalStateException is for invalid state timing.

Multiple choice technology architecture
  1. addSize

  2. getCust

  3. deleteRep

  4. putDimensions

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

JavaBeans getter methods follow the pattern 'getXxx()' where Xxx is the property name with the first letter capitalized. 'getCust' correctly follows this convention. 'addSize' and 'deleteRep' don't start with 'get' or 'is', and 'putDimensions' starts with 'put' which isn't a standard accessor prefix.

Multiple choice technology architecture
  1. Query length is limited in POST, not limited in GET.

  2. POST is secured whereas data is been submitted as part of URL in GET (not secured).

  3. By using POST method, we cannot submit the form.

  4. None

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

GET method submits data as part of the URL (visible in browser history/logs), making it insecure for sensitive data. POST sends data in the request body, hiding it from the URL. Also, GET has URL length limitations (typically 2048 characters), while POST does not.