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
-
getAppletContext()
-
getAppletInfo()
-
getCodeBase()
-
getDocumentBase()
-
destroy()
B
Correct answer
Explanation
This method of Applet class is used to return a string that describes the applet.
-
ActionEvent
-
AdjustmentEvent
-
ItemEvent
-
ComponentEvent
-
FocusEvent
C
Correct answer
Explanation
This Event Class of java.awt.event package is generated when a check box or list item is clicked.
-
KeyEvent
-
MouseEvent
-
TextEvent
-
WindowEvent
-
InputEvent
A
Correct answer
Explanation
This Event Class of java.awt.event package is generated when input is received from the keyboard.
-
getInetAddress()
-
getPort()
-
getLocalPort()
-
Socket()
-
getInputStream()
C
Correct answer
Explanation
This method of Socket class in java returns the local port to which Socket object is connected.
-
toBytes()
-
getRawRequest()
-
writeString()
-
error()
-
writeUCE()
D
Correct answer
Explanation
This method is used in java to format an HTML page to send back to web clients who make requests that cannot be completed.
-
getName()
-
getSize()
-
getStyle()
-
isBold()
-
getFontName()
A
Correct answer
Explanation
This method of Font class is used to return the logical name of the invoking font.
-
KeyListener
-
ItemListener
-
TextListener
-
FocusListener
-
ActionListener
C
Correct answer
Explanation
This event listener interface defines one method to recognize when a text value changes.
-
getApplets()
-
getImage()
-
showDocument()
-
getAudioClip()
-
showStatus()
D
Correct answer
Explanation
This method of AppletContext interface returns an audio clip object that encapsulates the audio clip found at the location specified as URL in the method.
-
getAddress()
-
getPort()
-
getData()
-
getLength()
-
close()
C
Correct answer
Explanation
This method of DatagramPacket is used to return the byte array of data contained in the datagram.
-
isActive()
-
init()
-
start()
-
stop()
-
getImage()
A
Correct answer
Explanation
This method of Applet class is used to to verify whether an applet is started or not.
-
void run()
-
public void run()
-
public void start()
-
void run(int priority)
B
Correct answer
Explanation
Option B is correct because in an interface all methods are abstract by default therefore they must be overridden by the implementing class. The Runnable interface only contains 1 method, the void run() method therefore it must be implemented.
Option A and D are incorrect because they are narrowing the access privileges i.e. package(default) access is narrower than public access.
Option C is not method in the Runnable interface therefore it is incorrect.
-
java.util.SortedMap
-
java.util.TreeMap
-
java.util.TreeSet
-
java.util.Hashtable
D
Correct answer
Explanation
Hashtable is the only class listed that provides synchronized methods. If you need synchronization great; otherwise, use HashMap, it's faster.
-
1 and 2
-
3 and 5
-
4 and 6
-
1 and 3
C
Correct answer
Explanation
Statements (4) and (6) are correct. (4) is correct because the wait() method is overloaded to accept a wait duration in milliseconds. If the thread has not been notified by the time the wait duration has elapsed, then the thread will move back to runnable even without having been notified.
(6) is correct because wait()/notify()/notifyAll() must all be called from within a synchronized, context. A thread must own the lock on the object its invokingwait()/notify()/notifyAll() on.
(1) is incorrect because wait()/notify() will not prevent deadlock.
(2) is incorrect because a sleeping thread will return to runnable when it wakes up, but it might not necessarily resume execution right away. To resume executing, the newly awakened thread must still be moved from runnable to running by the scheduler.
(3) is incorrect because synchronization prevents two or more threads from accessing the same object.
(5) is incorrect because notify() is not overloaded to accept a duration.
-
Java.util.Map
-
Java.util.List
-
Java.util.Collection
-
None of the above
A
Correct answer
Explanation
Option A is correct. A Map cannot contain duplicate keys.
Option B is wrong. Lists typically allow duplicate elements.
Option C is wrong. Collection allows duplicate elements.
-
While loop
-
For loop
-
Do while loop
-
Iterator
-
For each loop
C
Correct answer
Explanation
This is the correct option because while loop is at the the bottom of the block of code, so the block gets executed atleast one time irrespective of the condition.