Thread topic analysis in SCJP exam Source: www.chinajavaworld.net
1:
Which Two Cannot Directly Cause A THREAD To StopexecUTING? (Choose TWO)
A.existing from a synchronized block
B.CALLING THE WAIT METHOD ON AN Object
C.Calling Notify Method on an Object
D.calling read method on an inputstream object
E.CALLING The SetPriority Method on A Thread Object
Answer: AC. Top 55 questions
2:
Public class synctest {
Public static void main (String [] args) {
Final StringBuffer S1 = New StringBuffer ();
Final StringBuffer S2 = New StringBuffer ();
New thread () {
Public void run () {
Synchronized (s1) {
S2.Append ("a");
Synchronized (s2) {
S2.Append ("b");
System.out.print (S1);
System.out.print (S2);
}
}
}
} .start ();
New thread () {
Public void run () {
Synchronized (s2) {
S2.Append ("c");
Synchronized (s1) {
S1.Append ("d");
System.out.print (S2);
System.out.print (S1);
}
}
}
} .start ();
}
}
Which Two Statements Are True? (Choose TWO)
A. The Program Prints "abbcad"
B. The Program Prints "CDDACB"
C. The Program Prints "ADCBADBC"
D. The Output is a nconditionon-deterministic Point Because of a Possible Deadlock
E. The Output is Depis Running on. Endent on the threading model of the system the program
Answer: de
3:
What WILL HAPPEN WHEN You Attempt To Compile Andrun The Following Code?
Public class test {
INT i = 0;
Public static void main (String Argv []) {
Test T = New Test ();
T.MYMETHOD ();
}
Public void mymethod () {
While (true) {
Try {
Wait ();
} catch (interruptedexception e) {}
i ;
}
}
}
A. Compile Time Error, No Matching Notify Withnthe Method.
B. Compile and run but an infinite looping of the while method.
C. Compilation and Run without any output.
E. Runtime Exception "IllegalMonitorstatexception" .answer: e
Note:. The wait / notifsynchronized In this casynchronized) and will ty protocol can only be used se calling code does not havhus cause an Exception at ruwithin code that is e a lock on the object (not ntime.
4:
1.10 What is the result of compling and executing the folload code?
Public class threadtest extends thread {
Public void run () {
System.out.println ("in run");
Yield ();
System.out.Println ("Leaving Run");
}
Public static void main (string args []) {
New threadtest ()). START ();
}
}
A. The Code Fails TOCompile in The Main () Method.
B. The Code Fails Tocuest IN THE RUN () Method.
C. Only the text "in run" Will Be displaphied.
D. The text "in Run" FOLLOWED by "Leaving Run" Will Be Displayed.
E. The Code Compiles Correctly, But Nothing is Displayed.
Answer: D
5:
1.12 Which of the folloading Willwait () b. NOTIFY () c. Yield () d. Suspendefinitely stop a thread from executing? A. D () e. Sleep () Answer: ACDE
6:
1.12 Which of The Following Will Definitely Stopa THREAD from EXECUTING?
A. Wait ()
B. Notify ()
C. Yield ()
D. suspend ()
E. SLEEP ()
Answer: ACDE
7:
Which of the folloading statements about true?
A. You Can Only Obtain A Mutuallextens Thread or Implements Runnably Exclusive Lock On Methods in a class That e.
B. You Can Obtain a Mutually Exclusive Lock on Any Object.
C. You can't obtain a mutually exclusive lock onany Object.
D. Thread Schedulingalgorithms Are Platform Depend.
Answer: BD
8:
Consider The Following Statement:
Thread mythread = new thread ();
Which of the folloading statements area true regarding mythread?
A. The Thread Mythread is now in a runnable state.b. The Thread Mythread Has A Priority Of 5.
C. on calling the start () MethodClass Will Be Executed.on Mythread, The Run Method in The Thread
D. on calling the stclass will be executed.Art () Method on Mythread, The Run Method in The Calling
Answer: C
Note: The priority othe constructor. F Mythread Will Be inheritedFrom The Thread That Called
9:
What is the effect of itssuing a Wait () Method on an Object? (MUTIPLE)
1) IF a notify () MeteffECTHOD HAS ALREADY BEEN SENT TOTHAT OBJECT ITEN IT HAS NO
2) The object issuing the call Ta Notify () or notifyall () Methodo Wait () Will Halt Until Another Object Sends
3) An Exception Will Be Raised
4) The Object Issuinwith Any Other Objects Ug The Call to wait () Will Besing The receiving object.Automatically Synchronized
Answer 1)
10:
Pick all the true statements below.
1) IF A Thread Wantsobject's Lock.to Call Wait () on An Object, The Thread Must OWN That
.
3) At the moment Whethe Object for Which it n a thread is notified, it is awaiting.utomatically gets the lock of
Answer 1
11:
Class z {
2. Public static void main (String [] args) {
3. New z ();
4.}
5.
6. z () {
7. z alias1 = this;
8. z alias2 = this;
9. Synchronized (alias1) {
10. Try {
11. alias2.wait ();
12. System.out.println ("DONE WAITI");
13.}
14. Catch (InterruptedExcection E) {
15. System.out.println ("Interrupted");
16.}
17. Catch (Exception E) {
18. System.out.Println ("Other Exception");
19.}
20. Finally {21. System.out.println ("FINALLY");
twenty two. }
twenty three. }
24. System.out.Println ("all done");
25.}
26.}
Mutiple:
1) Compiler Error.
2) The application compiles but doesn't print.
3) The Application Prints "Done Waiting".
4) The Application Prints "Interrupted".
5) The Application Prints "Otherexception".
ANS: 2)
12:
What WILL HAPPEN WHEN You Attempt To Compile and Run The Following Code?
Class mythread extends thread {
Public void run () {
System.out.println ("Mythread: Run ()");
}
Public void start () {Note that after this method is rewritten, the function of the Call Run () method is no longer.
System.out.println ("MythRead: start ()");
}
}
Class MyRunnable Implements Runnable {
Public void run () {
System.out.println ("MyRunnable: Run ()");
}
Public void start () {// runnable Otherwise, perform RUN's start () of Call Thread without this method, so unless the object of this class can call it, ()
System.out.println ("MyRunnable: start ()");
}
}
Public class mytest {
Public static void main (string args []) {
Mythread mythread = new mythread ();
Myrunnable myrunnable = new myrunnable ();
Thread thread = new thread (myrunnable);
mythread.start ();
Thread.start ();
}
}
A.Prints: mythread: start () FOLLOWED by myrunnable: run ()
B.Prints: mythread: Run () FOLLOWED by myrunnable: start ()
C.Prints: mythread: start () FOLLOWED by myrunnable: start ()
D.Prints: mythread: run () FOLLOWED by Myrunnable: Run ()
E.Compile Time Error
F.None of the Above
Answer: a
13:
Multiple Objects of Multiple Threads to create The Following Code? Myclass (Given Below) Are USE WDED INTEGER Count. What WDED in a Program That Uses Ill Happen When Other Threads
Class myclass {
Static private int mycount = 0; int YourNumber;
Private statacy synchronized int nextcount () {
Return mycount; // mycount is static
}
Public void getYournumber () {
YourNumber = nextcount ();
}
}
A. The Code Ill Give OmpiLATION Error
B. The Code Ill Give Runtime Error
C. Each Thread Will Get a Unique Number
D. The Uniqueeness of The Number Different Threads CAN't Be Guaranteed.
C
14:
What is the effect of itssuing a Wait () Method ONAN Object
Mutiple:
1) IF a notify () Method Has Alreeffectady Been Sent To this Object The IT Has NO
2) The Object Issuina Notify () or Notifyall (g the call to wait () Will HA) Methodlt Until Another Object Sends
3) An Exception Will Be Raised
4) The Object Issuing The Call Twith Any Other Objects Using The Reco Wait () Will Be Automatical Synchronized Eiving Object.