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
-
Asynchronous
-
Synchronous
-
Synchronous and Asynchronous
-
None of above
B
Correct answer
Explanation
In general API design (such as Java Servlet sessions, JMS session methods, or database sessions), session methods are synchronous blocking operations. While asynchronous messaging exists, a session's execution context is synchronous by default unless explicitly specified.
-
Step Over
-
Step Out
-
Step Into
-
Step Till
A
Correct answer
Explanation
Step Over executes the current line and if it calls a method, runs the entire method without showing internal steps. Step Into would enter the method's code, Step Out returns from the current method to its caller, and Step Till is not a standard debugging command.
-
queue
-
callback
-
interval
-
transport
A,B,C
Correct answer
Explanation
Creating a timer event requires a queue (to manage event dispatching), a callback function (to execute when the timer fires), and an interval (the time between timer events). Transport is not required for timers - they are local timing objects that don't involve network communication.
-
Set ThrowOnUnmappableChar = True
-
Set ThrowOnmappableChar = True
-
Set ThrowOnmappableChar = False
-
Set ThrowOnUnmappableChar = False
A
Correct answer
Explanation
Setting ThrowOnUnmappableChar to true causes marshaling operations to throw an exception when a Unicode character cannot be mapped to an ANSI character. Setting it to false silently replaces unmappable characters with a default '?' character.
-
The code does not compile
-
The code compiles cleanly and shows “Object Version
-
The code compiles cleanly and shows “String Version”
-
The code throws an Exception at Runtime
C
Correct answer
Explanation
When method overloading exists with null argument, Java chooses the most specific method. Since String extends Object, the String version is more specific than Object version. The compiler resolves null to the String overload at compile-time. The code compiles cleanly and prints 'String Version'. This is a classic Java overload resolution rule.
A,B,C
Correct answer
Explanation
Java has 50 reserved keywords including 'new' (instantiation), 'break' (loop exit), and 'default' (switch/annotation default). 'goto' is a reserved keyword but not used in Java - it's reserved for potential future use but has no functionality. Option D is incorrect because goto is indeed a reserved keyword in Java.
-
final
-
finally
-
finalize
-
finalised
D
Correct answer
Explanation
In Java, 'final' is a keyword for variables/methods/classes that cannot be changed/extended. 'finally' is a keyword used in try-catch blocks for code that always executes. 'finalize()' is a method name (deprecated) for garbage collection cleanup. However, 'finalised' is NOT part of the Java language - it's a common misspelling. The correct answer is D because it's the only option that is not a valid Java keyword or method.
-
Vector
-
ArrayList
-
Hashtable
-
HashMap
A
Correct answer
Explanation
Vector is a legacy class (introduced in JDK 1.0) that implements a synchronized resizable array as part of the List interface. Every method in Vector is synchronized, making it thread-safe but slower than non-synchronized alternatives. ArrayList is the unsynchronized modern alternative. Hashtable is synchronized but implements Map, not List. HashMap is unsynchronized and also implements Map.
-
Boxing and UnBoxing
-
Generics
-
Enhanced for
-
Iterator
A,B,C
Correct answer
Explanation
Java 1.5 (Tiger) introduced autoboxing/unboxing for automatic primitive-wrapper conversion, generics for type-safe collections, and the enhanced for-loop for cleaner iteration. The Iterator interface existed since Java 1.2, so it was not a new feature in Java 1.5. The claimed answer correctly identifies A, B, and C.
B
Correct answer
Explanation
Static methods belong to the class itself, not to any instance, so they cannot access instance members (non-static fields or methods) without an object reference. Attempting to do so results in a compilation error. The statement is False, making the claimed answer correct.
-
Wait
-
notify
-
notifyAll
-
Sleep
A,B,C
Correct answer
Explanation
Java's Object class provides wait(), notify(), and notifyAll() for inter-thread communication using the monitor pattern. These methods must be called within synchronized code. The sleep() method pauses a thread but is not used for communication. The claimed answer A, B, C is correct.
B
Correct answer
Explanation
IOException and its subclasses are CHECKED exceptions in Java, meaning they must be declared or caught. Only RuntimeException and Error subclasses are unchecked. The statement that IOException is unchecked is False, making the claimed answer correct.
-
String
-
StringBuffer
-
Both
-
None
B
Correct answer
Explanation
StringBuffer is mutable and more efficient for multiple concatenations in loops because it modifies a single buffer. String is immutable - each concatenation creates a new object. For repeated concatenation, StringBuffer (or StringBuilder in newer Java) is preferred. The claimed answer B is correct.
-
String Buffer
-
String
-
both
-
none
A
Correct answer
Explanation
In Java, StringBuffer is mutable and modifies the existing character sequence without creating new objects, whereas String is immutable, meaning each concatenation creates a new String object, which degrades performance during multiple operations.
-
sizeOf ()
-
size()
-
getSize()
-
No such method in java to find size of an object directly.
D
Correct answer
Explanation
Java does not provide any built-in method like sizeof(), size(), or getSize() to directly find the size of an object. The size of an object in memory is implementation-dependent and not exposed through standard APIs. While Java provides methods to find the size of collections and arrays, there is no direct way to get the memory footprint of an object instance.