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
-
It is an instance method of Object class.
-
It is a static method of the Object class
-
It is an instance method of Thread class.
-
The Thread must have a lock on the object on which the wait() method is to be invoked
-
An object can have only one Thread in a waiting state at a time
-
It must be called in a synchronized code
A,D,F
Correct answer
Explanation
wait() is an instance method of Object class (A), must be called in synchronized context (F), and requires the calling thread to hold the object's lock (D). It's not static (B), not in Thread class (C), and multiple threads can wait on the same object (E). Three options are correct.
-
Static methods cannot be synchronized
-
Synchronized methods cannot make calls to non-synchronized methods
-
A synchronized method can be overridden to be non-synchronized and vice versa
-
When a thread is executing a synchronized method, other threads can freely access non-synchronized methods of the same object
C,D
Correct answer
Explanation
In Java, synchronized methods can be overridden to be non-synchronized (and vice versa) because synchronization is a property of the implementation, not the method signature. Additionally, while a thread holds a lock on a synchronized method, other threads can freely access non-synchronized methods.
-
Null Pointer Exception at client side
-
Client will receive warning message
-
ConnectException is thrown
-
Compilation Error
C
Correct answer
Explanation
When a client attempts to connect to a server that is not running, Java throws a ConnectException. This is a runtime exception indicating the connection was refused because no server is listening at the specified host and port.
-
final
-
transient
-
public
-
private
B
Correct answer
Explanation
The transient modifier in Java tells the JVM to skip that field during serialization - it won't be written to the persistent stream. This is useful for fields that can be recalculated, are derived from other data, contain sensitive information, or reference non-serializable objects.
B
Correct answer
Explanation
When a Java program starts, the JVM creates several threads beyond the main thread, including garbage collector threads and possibly other JVM-internal threads for monitoring, JIT compilation, and other runtime services. The main thread is just one of many threads running from the beginning.
B
Correct answer
Explanation
In JUnit 3, methods must start with 'test' to be recognized, but JUnit 4+ uses annotations like @Test and allows any method name. The statement is false because method naming conventions vary by framework version.
-
Its return type is String
-
It reads the password from the console
-
It has echoing disabled
-
It takes a single argument
-
None of them
B,C
Correct answer
Explanation
Console.readPassword() reads password input from the console with echoing disabled (characters don't appear on screen). It returns char[] not String for security (can be cleared from memory).
-
ArrayList
-
NameValue Collection
-
StringCollection
-
HashTable
D
Correct answer
Explanation
HashTable stores key-value pairs and allows retrieval by unique key. ArrayList is index-based, StringCollection is string-specific, and NameValueCollection exists but isn't the primary key-access collection.
-
session.clear()
-
session.dispose()
-
Session.Abandon
-
None
C
Correct answer
Explanation
Session.Abandon() explicitly ends and cleans up the current user session. Session.Clear() empties session data but keeps the session alive, and Session.Dispose() isn't a standard session method.
-
Object Handle
-
Size of the buffer assigned to hold the message
-
Length of application data in message
-
Connection Handle
C
Correct answer
Explanation
MQGET API requires input parameters: Object Handle (Hobj), Buffer (to receive message), Buffer Length (size of buffer), and Connection Handle (Hconn). The 'Length of application data in message' is an OUTPUT parameter (returned in the DataLength field) that tells you how much data was actually retrieved, not an input you provide.
-
Connection Handle
-
Object Handle
-
Reason Code
-
Completion Code
B
Correct answer
Explanation
MQCMIT API commits all operations within the current unit of work. Its parameters are: Connection Handle (Hconn), Completion Code (CompCode), and Reason Code (Reason). Object Handle (Hobj) is NOT a parameter for MQCMIT because commit operations work at the connection/transaction level, not on individual queue objects.
B
Correct answer
Explanation
In Java, the synchronized modifier is not part of a method's signature. This means a subclass can override a synchronized method and choose whether to keep or remove the synchronized keyword. The synchronization is an implementation detail, not a contract that must be preserved.
-
static public void main(String []args)
-
public static void main(String heyhey[])
-
public static int main(String[] args)
-
public static void main(String ...args)
-
public static void main (String [], int argv)
A,B,D
Correct answer
Explanation
Valid main method signatures must be public static void with a single String array or varargs parameter. Modifier order (static public vs public static) is flexible. Parameter names can be any valid identifier. Return type must be void, and only one String array/varargs parameter is allowed.
-
all sub-classes of Throwable
-
not the sub classes of RuntimeException
-
all sub-classes of Error
-
None of these
B
Correct answer
Explanation
In Java, checked exceptions are subclasses of Exception but are not subclasses of RuntimeException. Subclasses of Error and RuntimeException are unchecked.