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. Compile error : No method name like floorKey()

  2. null

  3. NullPointerException

  4. 2

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

This is a duplicate of question 161465 with identical code and options. The floorKey(3) method returns the greatest key less than or equal to 3. The map contains keys 1,2,4,5,6, so floorKey(3) returns 2. The method exists and the map has entries, so output is 2.

Multiple choice technology programming languages
  1. Locale loc = Locale.getLocale();System.out.println(loc.getDisplayCountry()+ “ “+ df.format(d));

  2. Locale loc = Locale.getDefault();System.out.println(loc.getDisplayCountry()+ “ “ + df.format(d));

  3. Locale bc = Locale.getLocale();System.out.println(loc.getDisplayCountry()+ “ “+ df.setDateFormat(d));

  4. Locale loc = Locale.getDefault();System.out.println(loc.getDispbayCountry()+ “ “+ df.setDateFormat(d));

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

Locale.getDefault() is the correct method to retrieve the system's current locale, and loc.getDisplayCountry() returns the country name. The df.format(d) method is the standard way to format a Date object. Other options are incorrect because getLocale() and setDateFormat() do not exist in their respective classes.

Multiple choice technology programming languages
  1. Enables you to cycle through a collection in the forward direction only, for obtaining or removing elements

  2. just for loop

  3. while loop

  4. It is an class for storing objects

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

An Iterator in Java provides a uniform way to access elements of a collection sequentially, allowing both retrieval and removal of elements during iteration. It differs from simple loops (options B, C) which are control flow constructs, not a dedicated interface for traversing collections. Option D incorrectly describes it as a storage class rather than an access mechanism.

Multiple choice technology programming languages
  1. Array: can store primitive ArrayList: Stores object only

  2. Array: fix size ArrayList: resizable

  3. Array: can have multi dimensional ArrayList: can have multi dimensional

  4. Array: lang ArrayList: Collection framework

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

Arrays are fixed-size, built-in language structures that can store primitives directly. ArrayList is a resizable class within the Collections Framework that only stores objects, utilizing autoboxing for primitives. Additionally, arrays natively support multi-dimensional syntax, whereas ArrayList does not, and Arrays are part of Java language basics while ArrayList is in java.util.

Multiple choice technology programming languages
  1. incSize(int minCapacity)

  2. ensureCapacity(int minCapacity)

  3. incCapicity(int minCapacity)

  4. All of the Above

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

ArrayList uses ensureCapacity(int minCapacity) to request internal capacity expansion before adding elements. This is an optimization to avoid multiple internal array resizes. Option A's 'incSize' and option C's 'incCapicity' (misspelled) don't exist in the API.

Multiple choice technology programming languages
  1. Returns String

  2. Returns an Array

  3. Returns a shallow copy of this ArrayList instance

  4. Returns Void

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

The clone() method in ArrayList returns a shallow copy - the ArrayList object itself is new, but the elements are references to the same objects (not deep copies). It doesn't return a String, Array, or void.

Multiple choice technology programming languages
  1. True

  2. False

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

NullPointerException is an unchecked exception in Java because it inherits from RuntimeException. Checked exceptions must be declared in a method's throws clause or handled in a try-catch block, whereas unchecked exceptions represent programming errors occurring at runtime that do not require explicit handling.

Multiple choice technology programming languages
  1. True

  2. False

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

The statement "Declarations must appear at the start of the body of a Java method" is false.

In Java, variable declarations can appear anywhere within the body of a method, as long as they are declared before they are used. This allows for more flexibility in the design of a program and allows the programmer to declare variables closer to the point of their actual use, making the code more readable and easier to maintain.

Therefore, the correct answer is:

The Answer is: B. False

Multiple choice technology architecture
  1. doComplete()

  2. doCancel()

  3. doInit()

  4. doUpdate()

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

The doInit() method is the standard lifecycle method invoked when a page is first loaded or initialized in the PS Framework. doComplete() is called after successful form submission, doCancel() handles cancellation actions, and doUpdate() is for update operations during page lifecycle.

Multiple choice technology programming languages
  1. 3

  2. 1

  3. 2

  4. 0

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

Only one String object is created by 'new String("Cheese")'. The 'String strA;' declaration only creates a reference variable (null initially), not an object. In practice, this code would use a string literal instead, but technically only the 'new' keyword creates an object.

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