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. The element will be successfully added

  2. ArrayIndexOutOfBounds Exception

  3. The Vector allocates space to accommodate up to 17 elements

  4. The element will be successfully deleted

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

Java's Vector class is dynamically resizable and automatically grows its capacity when needed. Unlike fixed-size arrays, Vector will successfully add elements beyond its initial capacity by internally allocating more space, so adding a 6th element to a Vector initialized with capacity 5 will succeed without throwing an exception.

Multiple choice technology programming languages
  1. True

  2. False

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

The Runnable interface requires only the run() method to be implemented. The start() method belongs to the Thread class, not Runnable. When implementing Runnable, you override run() with your task logic, then pass the Runnable to a Thread which calls start() to invoke run() in a new thread.

Multiple choice technology programming languages
  1. True

  2. False

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

The wait() method explicitly releases the object lock before putting the thread to wait. The thread must wait for another thread to call notify() or notifyAll() on the same object, then reacquire the lock before resuming execution. This mechanism enables inter-thread communication by allowing other threads to access the synchronized object while one thread waits.

Multiple choice technology programming languages
  1. A

  2. B

  3. C

  4. D

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

Throwing IllegalArgumentException is the standard practice in Java to signal that a method argument is invalid (null) when it is required to be non-null.

Multiple choice technology programming languages
  1. A

  2. B

  3. C

  4. D

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

The method count recursively calls itself with ++i without any base case or termination condition, leading to infinite recursion. This rapidly exhausts the execution stack and throws a java.lang.StackOverflowError.

Multiple choice technology programming languages
  1. addSize

  2. getCust

  3. deleteRep

  4. isColorado

  5. putDimensions

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

JavaBeans naming convention requires getters to use 'get' prefix (or 'is' for boolean properties) and setters to use 'set' prefix. getCust() correctly uses the 'get' prefix for a getter method. isColorado() correctly uses the 'is' prefix for a boolean getter. addSize(), deleteRep(), and putDimensions() don't follow JavaBeans patterns.

Multiple choice technology web technology
  1. The before() method will print 1 2

  2. The before() method will print 1 2 3

  3. The before() method will print three numbers, but the order cannot be determined

  4. The before() method will not compile

  5. The before() method will throw an exception at runtime

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

TreeSet stores elements in sorted order and requires elements to be mutually comparable. When you add String "2" then Integer 3, TreeSet attempts to compare them using natural ordering, which causes ClassCastException at runtime because String and Integer are not mutually comparable. The TreeSet.iterator() triggers the comparison during traversal.

Multiple choice technology web technology
  1. java.util.HashSet

  2. java.util.LinkedHashSet

  3. java.util.List

  4. java.util.ArrayList

  5. java.util.Vector

  6. java.util.PriorityQueue

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

java.util.ArrayList implements the List interface, allowing dynamic resizing and providing fast O(1) indexed access (random access) to its elements. Unlike Vector, the methods of ArrayList are not synchronized, making it faster but not thread-safe.

Multiple choice technology web technology
  1. 2 2 3 3

  2. 2 2 3 4

  3. 4 3 3 4

  4. 2 2 3 3 3

  5. 4 3 3 3 3

  6. 2 2 3 3 4

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

At line 16, pq.peek() returns '2'. Next, '1' is offered, '3' is added, and '1' is removed, leaving '2', '3', '4'. Line 20 polls and prints '2'. At line 21, pq.remove('2') returns false (as '2' was already polled), preventing the print inside the if-condition. Finally, line 22 polls '3' and peeks at '4'.

Multiple choice technology web technology
  1. The before() method will print 1 2

  2. The before() method will print 1 2 3

  3. The before() method will print three numbers, but the order cannot be determined

  4. The before() method will not compile

  5. The before() method will throw an exception at runtime

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

The code mixes String objects ("2", "1") with Integer object (3) in a TreeSet without generics. TreeSet orders elements using compareTo, which throws ClassCastException when comparing incompatible types like String and Integer at runtime. Compilation succeeds due to raw type.

Multiple choice technology web technology
  1. The before() method will print 1 2

  2. The before() method will print 1 2 3

  3. The before() method will print three numbers, but the order cannot be determined

  4. The before() method will not compile

  5. The before() method will throw an exception at runtime

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

The code mixes String objects ("2", "1") with Integer object (3) in a TreeSet without generics. TreeSet orders elements using compareTo, which throws ClassCastException when comparing incompatible types like String and Integer at runtime. Compilation succeeds due to raw type.

Multiple choice technology web technology
  1. java.util.HashSet

  2. java.util.LinkedHashSet

  3. java.util.List

  4. java.util.ArrayList

  5. java.util.Vector

  6. java.util.PriorityQueue

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

ArrayList is a resizable array implementation that provides indexed access via get(int index) and set(int index, E element) methods. Unlike Vector, ArrayList's methods are not synchronized, making it faster for single-threaded use. HashSet and LinkedHashSet don't provide indexed access. List is an interface, not a concrete class. Vector has synchronized methods. PriorityQueue doesn't support indexed access.

Multiple choice technology web technology
  1. 2 2 3 3

  2. 2 2 3 4

  3. 4 3 3 4

  4. 2 2 3 3 3

  5. 4 3 3 3 3

  6. 2 2 3 3 4

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

At line 16, pq.peek() returns '2' (sorted first among '2', '4'). Next, '1' is offered, '3' is added, and '1' is removed. At line 20, pq.poll() retrieves and removes the lowest element, '2'. Line 21 removes '2' (returns false because '2' was polled), so the if-block doesn't run. Line 22 polls '3' and peeks at '4'.

Multiple choice technology web technology
  1. The before() method will print 1 2

  2. The before() method will print 1 2 3

  3. The before() method will print three numbers, but the order cannot be determined

  4. The before() method will not compile

  5. The before() method will throw an exception at runtime

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

TreeSet orders its elements as they are added by comparing them using their natural ordering. When the code attempts to add the Integer 3 after the String "2", the TreeSet throws a ClassCastException at runtime because Integer and String are not mutually comparable.