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
-
suspend()
-
destroy()
-
resume()
-
stop()
A,C,D
Correct answer
Explanation
In Java's Thread class, suspend(), resume(), and stop() are final methods and therefore cannot be overridden by subclasses. These methods are also deprecated in modern Java. The destroy() method is not final, so it can technically be overridden, though it's also deprecated.
-
Heap memory
-
classes
-
objects
-
variables
C
Correct answer
Explanation
In Java, threads acquire locks at the object level using intrinsic locks (monitor locks). Each object has an associated monitor that threads can lock or unlock. Threads do not lock heap memory, classes themselves, or individual variables - they lock objects to achieve synchronization.
-
IllegalMonitorStateException
-
IllegalThreadStateException
-
IllegalLockStateException
-
InvalidLockException
A
Correct answer
Explanation
The wait(), notify(), and notifyAll() methods must be called from within a synchronized context (i.e., while holding the object's lock). Invoking these methods without acquiring the lock results in IllegalMonitorStateException at runtime.
B
Correct answer
Explanation
Invoking the notify() method wakes up a single thread waiting on the object's monitor, but it does not immediately relinquish the object lock. The lock is only released when the synchronized block or method execution finishes by the current thread.
-
sleep()
-
join()
-
activeCount()
-
none
B
Correct answer
Explanation
sleep() and activeCount() are static methods of the Thread class - you call them directly on Thread.sleep() or Thread.activeCount(). join() is an instance method that you call on a specific thread object. 'none' is incorrect since join() is indeed not static.
-
wait()
-
notify()
-
notifyAll()
-
start()
A,B,C
Correct answer
Explanation
wait(), notify(), and notifyAll() are defined in the Object class, not the Thread class - they're available on every object in Java for inter-thread communication. start() IS a method of Thread class that begins execution.
-
Thread.MAX_PRIORITY
-
Thread.NORM_PRIORITY
-
Thread.LOW_PRIORITY
-
Thread.MIN_PRIORITY
A,B,D
Correct answer
Explanation
Thread defines three priority constants: MAX_PRIORITY (10), MIN_PRIORITY (1), and NORM_PRIORITY (5). LOW_PRIORITY doesn't exist - it's a distractor mixing 'LOW' with the correct constant name MIN_PRIORITY.
-
Because the validation settings are hard coded.
-
Susceptible to bypass using various forms of character encoding
-
Because it's difficult to implement a black list filter that also takes into account data sent using the POST method
-
Because it is typically implemented using regular expressions to match known good data patterns
B
Correct answer
Explanation
Black list validation is weak because attackers can bypass it using various character encoding techniques (URL encoding, Unicode, double encoding, etc.) to represent blocked characters in ways the filter doesn't recognize. Being hard-coded (A) doesn't make it weak. POST method handling (C) isn't specifically problematic for black lists. Black lists don't match good patterns (D) - they block bad ones.
B
Correct answer
Explanation
Java is generally faster than JavaScript for heavy processing due to its compiled nature, static typing, and optimized JVM. JavaScript is interpreted and dynamically typed, which introduces runtime overhead. For computationally intensive tasks, Java's performance advantages are significant.
B
Correct answer
Explanation
Caching frequently accessed object properties in local variables reduces repeated property lookups, which improves performance. Each property access requires the JavaScript engine to traverse the object's prototype chain. Storing the value in a local variable eliminates this overhead, especially in loops.
-
if (x*x < 1000 && y) alert("true!");
-
if (x*x > 1000 && y) alert("true!");
-
if (y || x*x > 1000) alert("true!");
-
if (x*x > 1000 || y) alert("true!");
A,D
Correct answer
Explanation
Given x=10 (so x*x=100), option A evaluates both conditions (100<1000 is true, must check y), and option D evaluates both (100>1000 is false, must check y due to OR). Options B and C short-circuit without full evaluation. The question asks which are SLOWER, meaning more evaluations, so A and D are correct.
-
An Error is a subclass of Throwable
-
An Error is a subclass of Exception
-
Error indicates serious problems that a reasonable application should not try to catch.
-
An Error is a subclass of IOException
A,C
Correct answer
Explanation
In Java, Error is a direct subclass of Throwable, not Exception. Error represents serious problems like OutOfMemoryError or StackOverflowError that applications should not attempt to catch or recover from, as they typically indicate fatal JVM-level issues. Option B is incorrect because Error and Exception are siblings under Throwable.
B
Correct answer
Explanation
The dmf:checkbox uses the 'onchange' attribute for state changes, but uses 'onclick' or similar for event handling methods. The onchange attribute is for defining what happens when the checkbox state changes, not the action handling method itself.
-
Compile error : No method name like lastEntry()
-
null
-
NullPointerException
-
0
B
Correct answer
Explanation
The NavigableMap interface does have a lastEntry() method, so this is not a compile error. However, calling lastEntry() on an empty ConcurrentSkipListMap returns null, not a NullPointerException. The code compiles and runs, outputting null. Option B is correct.
-
Compile error : No method name like floorKey()
-
null
-
NullPointerException
-
2
D
Correct answer
Explanation
The floorKey(k) method returns the greatest key less than or equal to k. The map contains keys 1,2,4,5,6 (sorted order). floorKey(3) looks for the largest key <= 3, which is 2. The method exists in NavigableMap, so no compile error. The map has entries, so result is not null. Output is 2.