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
  1. ListIterator can iterate in backward direction whereas Iterator cannot.

  2. ListIterator can get index at any point while Iterator cannot.

  3. ListIterator can add a value at any point while Iterator cannot.

  4. All of the above

  5. None of these

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

All the three points are the differences between iterator and ListIterator.

Multiple choice
  1. Calling the SetPriority() method on a Thread object.

  2. Calling the wait() method on an object.

  3. Calling notify() method on an object.

  4. Calling read() method on an InputStream object.

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

Option 3 is correct. notify() - wakes up a single thread that is waiting on this object's monitor.

Multiple choice
  1. int a = Math.abs(-5);

  2. int b = Math.abs(5.0);

  3. int c = Math.abs(5.5F);

  4. int d = Math.abs(5L);

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

The return value of the Math.abs() method is always the same as the type of the parameter passed into that method. In the case of A, an integer is passed in and so the result is also an integer which is fine for assignment to "int a". The values used in B, C & D respectively are a double, a float and a long. The compiler will complain about a possible loss of precision if we try to assign the results to an "int".

Multiple choice
  1. 1, 2, 4

  2. 2, 4, 5

  3. 1, 2, 6

  4. 2, 3, 4

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

(1), (2), and (6) are correct. They are all related to the list of threads waiting on the specified object. (3), (5), (7), and (8) are incorrect answers. The methods isInterrupted() andinterrupt() are instance methods of Thread. The methods sleep() and yield() are static methods of Thread. D is incorrect because synchronized is a keyword and the synchronized() construct is part of the Java language.

Multiple choice
  1. run();

  2. construct();

  3. start();

  4. register();

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

Option 3 is correct. The start() method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. Option 1 is wrong. The run() method of a thread is like the main() method to an application. Starting the thread causes the object's run method to be called in that separately executing thread. Option 2 is wrong. There is no construct() method in the Thread class. Option 4 is wrong. There is no register() method in the Thread class.

Multiple choice
  1. Compilation will succeed.

  2. Compilation will fail at line 3.

  3. Compilation will fail at line 5.

  4. Compilation will fail at line 15.

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

Option B is correct. The compiler complains with the error "modifier private not allowed here". The class is created private and is being used by another class on line 19.

Multiple choice
  1. after line 5

  2. after line 6

  3. after line 7

  4. There is no way to be absolutely certain.

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

In Java, objects become eligible for garbage collection when they are no longer reachable through any reference chain. After line 5 (b = null), object B is not directly reachable. However, if the call a.s(b) on line 4 stored a reference to b inside object a, then B would still be reachable through a until line 6. Without seeing the code for class A and method s(), we cannot be certain whether B is referenced internally by A. Therefore, absolute certainty is impossible.

Multiple choice
  1. true

  2. false

  3. Compilation fails

  4. An exception is thrown at runtime

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

The code will not compile because in line 7, the line will work only if we use (x==y) in the line. The == operator compares values to produce a boolean, whereas the = operator assigns a value to variables. Option A, B, and D are incorrect because the code does not get as far as compiling. If we corrected this code, the output would be false.

Multiple choice
  1. 0

  2. 1

  3. 2

  4. Compilation fails.

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

Compilation failed because static was an illegal start of expression - method variables do not have a modifier (they are always considered local).