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
  1. getChildCount(TreePath path)

  2. getVisibleChildrenCount(TreePath path)

  3. getVisibleChildCount(TreePath path)

  4. getChildrenCount(TreePath path)

  5. getAllChildCount(TreePath path)

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

This is the correct choice. getVisibleChildCount(TreePath path) method is present in the AbstractLayoutCache class and is used to retrieve the number of visible children for a row.

Multiple choice
  1. 1 and 4

  2. 2 and 3

  3. 3 and 4

  4. 1 and 3

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

(1) is a restatement of the equals() and hashCode() contract. (4) is true because if the hashCode() comparison returns ==, the two objects might or might not be equal. (2) and (3) are incorrect because the hashCode() method is very flexible in its return values, and often two dissimilar objects can return the same hash code value.

Multiple choice
  1. x.delete()

  2. x.finalize()

  3. Runtime.getRuntime().gc()

  4. Only the garbage collection system can destroy an object.

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

Option D is correct. When an object is no longer referenced, it may be reclaimed by the garbage collector. If an object declares a finalizer, the finalizer is executed before the object is reclaimed to give the object a last chance to clean up resources that would not otherwise be released. When a class is no longer needed, it may be unloaded. Option A is wrong. I found 4 delete() methods in all of the Java class structure. They are: delete() - Method in class java.io.File : Deletes the file or directory denoted by this abstract pathname. delete(int, int) - Method in class java.lang.StringBuffer : Removes the characters in a substring of this StringBuffer. delete(int, int) - Method in interfacejavax.accessibility.AccessibleEditableText : Deletes the text between two indices delete(int, int) - Method in class :javax.swing.text.JTextComponent.AccessibleJTextComponent; Deletes the text between two indices None of these destroy the object to which they belong. Option B is wrong. I found 19 finalize() methods. The most interesting, from this questions point of view, was the finalize() method in class java.lang.Object which is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. This method does not destroy the object to which it belongs. Option C is wrong. But it is interesting. The Runtime class has many methods, two of which are: getRuntime() - Returns the runtime object associated with the current Java application. gc() - Runs the garbage collector. Calling this method suggests that the Java virtual machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the virtual machine has made its best effort to recycle all discarded objects. Interesting as this is, it doesn't destroy the object.

Multiple choice
  1. 1 and 3

  2. 2 and 4

  3. 1 and 5

  4. 2 and 6

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

(2) and (6). When a thread's run() method completes, the thread will die. The run()method must be declared public void and not take any arguments. (1) is incorrect because classes can never extend interfaces. (3) is incorrect because therun() method is typically not empty; if it were, the thread would do nothing. (4) is incorrect because the mandatory method is run(). (5) is incorrect because the classimplements Runnable.

Multiple choice
  1. 1 and 2

  2. 2, 3 and 5

  3. 3, 4, and 5

  4. 2 and 4

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

(2), (3), and (5). These are all valid interface method signatures. (1), is incorrect because an interface method must be public; if it is not explicitly declared public it will be made public implicitly. (4) is incorrect because interface methods cannot be static.