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
-
notify()
-
wait()
-
InputStream access
-
sleep()
A
Correct answer
Explanation
The notify() method is used to wake up a thread that is waiting on an object's monitor, but it does not cause the current thread to stop or terminate. In contrast, wait() blocks the thread, sleep() pauses it, and blocking I/O operations like InputStream access can also stop thread progress until data is available.
-
Object
-
Thread
-
Runnable
-
Class
A
Correct answer
Explanation
The wait(), notify(), and notifyAll() methods are defined in the java.lang.Object class. Since these methods deal with the monitor/lock status of a specific object, they are made available to all objects through the root Object class rather than being specific to Thread or Runnable.
-
null
-
transient
-
assert
-
const
-
Double precision floating-point (double)
-
Character (char)
-
Byte integer (byte)
-
Boolean (boolean)
B
Correct answer
Explanation
The char primitive type in Java is designed to store Unicode characters using 16-bit UTF-16 encoding. This allows Java to support international characters. Double is for floating-point numbers, byte for small integers, and boolean for true/false values.
-
Short integer to character
-
Integer to long integer
-
Floating-point to double precision floating-point
-
Character to floating-point
A
Correct answer
Explanation
Widening conversions occur when a value is moved to a type that can accommodate its range. Short to char is not a widening conversion because short is signed (-32768 to 32767) and char is unsigned (0 to 65535); thus, negative short values cannot be represented in char.
-
Iterator
-
Map
-
Enumeration
-
Iterable
D
Correct answer
Explanation
The enhanced for-loop (for-each) works with arrays and any object implementing the Iterable interface. The Iterable interface requires the implementation of an iterator() method. While Maps and Iterators are related, the expression itself must implement Iterable (or be an array).
-
Compilation fails
-
one two three four
-
four three two one
-
four one three two
-
one two three four one
-
one four three two one
D
Correct answer
Explanation
TreeSet stores elements in a sorted, ascending order (natural ordering for Strings). It also rejects duplicates. The strings 'four', 'one', 'three', 'two' are added, and the second 'one' is ignored. The natural alphabetical order is 'four', 'one', 'three', 'two'.
-
_jspService()
-
jspInit()
-
jspDestroy()
-
getParameter()
A
Correct answer
Explanation
In the JSP lifecycle, the _jspService() method is generated by the container and corresponds to the body of the JSP page. The JSP specification forbids the user from overriding this method, whereas jspInit() and jspDestroy() can be overridden.
-
doPost()
-
doGet()
-
System.out.println()
-
System.exit(0);
D
Correct answer
Explanation
In a container-managed environment like a Servlet, calling System.exit(0) terminates the entire JVM. This shuts down the web server or application server, affecting all other users and applications, causing catastrophic and unpredictable system-wide failure.
-
Float
-
None of the above
-
byte
-
String
-
integer
C
Correct answer
Explanation
In Java, 'byte' is a primitive type. 'String' is a class (reference type). 'integer' is not a valid keyword (it's 'Integer' wrapper or 'int' primitive). 'Float' is a wrapper class (primitive is 'float'). Therefore, 'byte' is the only primitive type listed.
-
Strings are a primitive type in Java and the StringBuffer is used as the matching wrapper type
-
The size of a string can be retrieved using the length property.
-
Strings are a primitive type in Java that overloads the + operator for concatenation
-
The String class is implemented as a char array, elements are addressed using the stringname[] convention
A
Correct answer
Explanation
These methods require owning the object's monitor lock, which only happens within synchronized code on that object. Calling them without synchronization throws IllegalMonitorStateException at runtime.
A
Correct answer
Explanation
The statement is True.
The "synchronized" keyword in Java is used to achieve thread synchronization. When a method or a block of code is declared as synchronized, it means that only one thread can access that method or block at a time.
When a thread encounters a synchronized method or block, it first checks if the lock associated with the object is available. If the lock is available, the thread grabs the lock and proceeds with executing the code inside the synchronized method or block. After the execution is complete, the lock is released, allowing other threads to access the synchronized code.
So, the "synchronized" keyword ensures that a thread grabs an object lock before continuing execution, making the statement True.
The answer is: A. True
-
1.java
-
2.javaw
-
3.javapath
-
4.javadoc
C
Correct answer
Explanation
javapath is not a standard Java tool. java (executes programs), javaw (windowless java), and javadoc (generates documentation) are all valid JDK command-line tools. javapath does not exist in the standard Java toolkit.
-
1.add(Object x)
-
2.remove(Object x)
-
3.contains(Object x)
-
4.insert(int i,Object x)
-
5.set(int I,Object x)
D
Correct answer
Explanation
ArrayList has add(), remove(), contains(), and set() methods, but does not have an insert() method - the equivalent functionality is provided by add(int index, E element). Option D correctly identifies the non-existent method.