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
-
init()
-
start()
-
run()
-
resume()
B
Correct answer
Explanation
In Java threading, the start() method is used to begin execution of a thread. When start() is called, the JVM creates a new thread and invokes the run() method within that new thread. The init() method is not a standard thread method, run() contains the thread's body but doesn't start execution, and resume() is deprecated. The start() method is specifically designed for thread initialization and execution.
-
Object
-
Thread
-
Runnable
-
Class
A
Correct answer
Explanation
The wait(), notify(), and notifyAll() methods are defined in the Object class in Java, not in Thread or Runnable. This design allows all objects to be used as locks or monitors for thread synchronization. Every object in Java inherits these methods from Object. The Thread class provides methods for thread management, while Runnable is an interface for defining thread tasks. Only Object defines these fundamental synchronization methods.
-
void run()
-
public void run()
-
public void start()
-
void run(int priority)
B
Correct answer
Explanation
The Runnable interface requires implementing the run() method with public void run() signature. The method must be public (to be called by the thread) and void (no return value). Option A (void run()) lacks the public modifier, making it inaccessible. Option C (public void start()) is incorrect because Runnable doesn't define start(). Option D (void run(int priority)) has wrong signature and parameters. The exact signature public void run() is mandated by the interface contract.
-
One is not a method of the String object.
-
substr() takes three arguments, substring() only two.
-
Only one accepts a desired string length as an argument.
-
Besides the spelling, nothing
C
Correct answer
Explanation
substr() accepts a start position and length parameter, while substring() takes start and end positions. Only substr() allows specifying the desired string length directly. Option A is incorrect because both are String methods. Option B is wrong because neither takes three arguments. Option D is false because their parameter semantics differ fundamentally.
-
One is not a method of the String object.
-
substr() takes three arguments, substring() only two.
-
Only one accepts a desired string length as an argument.
-
Besides the spelling, nothing
C
Correct answer
Explanation
substr() accepts a start position and length parameter, while substring() takes start and end positions. Only substr() allows specifying the desired string length directly. Option A is incorrect because both are String methods. Option B is wrong because neither takes three arguments. Option D is false because their parameter semantics differ fundamentally.
-
The Java console
-
LiveConnect
-
LiveWire
-
LiveWire Pro
B
Correct answer
Explanation
LiveConnect was the technology that enabled JavaScript to communicate with Java applets, allowing JavaScript to call Java methods and access Java properties. LiveWire and LiveWire Pro were server-side JavaScript technologies. The Java console is a debugging tool, not an integration mechanism.
-
for(var i = 0; i < myString.length; i++) {
-
var myArray = myString.split('x'); myString = myArray.join('');
-
It wasn't possible until now. Thanks JavaScript1.2!
-
The first two both work fine
D
Correct answer
Explanation
Before JavaScript 1.2's enhanced String.replace(), you could remove characters using two approaches: 1) iterate through the string building a new string without 'x', or 2) split on 'x' and join the array with empty string. Both methods work, though split/join is more concise.
-
Add a TYPE=HIDDEN INPUT to the form
-
Trap the return keypress and return (null)
-
Add 'return false' to onsubmit="..." in the FORM tag
-
Instruct the user not to press the Return key.
C
Correct answer
Explanation
Adding 'return false' to the onsubmit handler in the FORM tag prevents the default form submission behavior when the user presses Enter. This allows custom JavaScript processing to handle the form data instead. The other options - hidden input, trapping return key, or instructing users - don't address the core issue of form submission behavior.
-
NullPointerException
-
ServletException
-
FileNotFoundException
-
IOException
-
none of the above
B,D
Correct answer
Explanation
The doGet() method in HttpServlet explicitly declares ServletException and IOException in its method signature. ServletException is thrown for servlet-specific errors, while IOException handles general I/O errors during request processing. NullPointerException (unchecked) and FileNotFoundException (not thrown by doGet) are not declared exceptions.
-
Getparameter()
-
getParameterValue()
-
getParameterValues()
-
getParamValues()
C
Correct answer
Explanation
The getParameterValues() method returns all values for a parameter that has multiple values (like multi-select dropdowns or checkbox groups). It returns a String array containing all values. getParameter() returns only the first value, while getParameterValue() and getParamValues() are not standard servlet API methods.
-
sendErrorMessage()
-
sendError()
-
sendMessage()
-
sendHttpErrorCode()
B
Correct answer
Explanation
The HttpServletResponse.sendError() method sends an error response to the client with a specific HTTP status code. Methods like sendErrorMessage(), sendMessage(), and sendHttpErrorCode() don't exist in the servlet API. sendError() is the standard way to send error responses.
-
Escape special characters using the specific escape syntax for that interpreter
-
Use of Parameterized API
-
Avoid sending the wrong data at first place as request parameter.
-
Input Validation using Whitelist
C
Correct answer
Explanation
Option C is the correct answer because 'Avoid sending the wrong data' is not a technical injection prevention mechanism - it's a general practice rather than a specific defensive technique. The other options ARE valid prevention mechanisms: escaping special characters (A), using parameterized APIs/prepared statements (B), and input validation with whitelists (D) are all standard techniques for preventing SQL injection and similar attacks.
-
1.6.0 Rev 24
-
1.5.0 Rev 20
-
1.6.0 Rev 23
-
1.6.2 Rev 24
A
Correct answer
Explanation
Java 1.6.0 Update 24 was indeed a recent version when this question was written (around 2007-2008). Java 6 (also known as Java 1.6) was a major release and Update 24 was a significant revision. However, note that Java has progressed significantly since then - current LTS versions are Java 17 and Java 21. This question reflects the version landscape of its era.
-
Always
-
Never
-
When you want to find when the recepient opened the mail.
-
My wish
B
Correct answer
Explanation
Email return receipts are often considered intrusive and unprofessional - they pressure recipients and can be blocked. Best practice is to avoid them unless absolutely necessary for critical business/legal reasons. 'Always' is too aggressive; tracking recipients makes them uncomfortable.
A
Correct answer
Explanation
In Java, the Garbage Collector calls the finalize() method of an object when it determines there are no more references to it. The JVM guarantees that finalize() will be called at most once per object, even if the object is 'resurrected' and then becomes eligible for GC again.