• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

OCP Practice Exams Sef-Assessment Test 2 Q8

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't E wrong for case I because for the new thread started in run, the if condition is not satisfied? id holds the name of only the thread started in the main() method and is not reassigned to the new thread's name, right?

2. public class Salmon extends Thread {
3. public static long id;
4. public void run() {
5. for(int i = 0; i < 4; i++) {
6. // insert code here
7. new Thread(new Salmon()).start();
8. throw new Error();
9. }
10. System.out.print(i + " ");
11. } }
12. public static void main(String[] args) {
13. Thread t1 = new Salmon();
14. id = t1.getId();
15. t1.start();
16. } }
And the two code fragments:
I. if(i == 2 && id == Thread.currentThread().getId()) {
II. if(i == 2) {

When inserting either fragment, independently at line 6, which are true? (Choose all that apply.)

A. Both fragments produce the same output.
B. Both fragments will end in about the same amount of time.
C. Compilation fails, regardless of which fragment is inserted.
D. Regardless of which fragment is inserted, output ends once the Error is thrown.
E. Regardless of which fragment is inserted, output continues after the Error is thrown.

Answer (for Objective 4.2):
 E is correct. In either case, before the Error is thrown a new thread is created, and the new
thread will execute independently of the Error.
 A and B are incorrect because fragment II creates a sort of recursive, endless loop. C and D
are incorrect based on the above.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
initially it does but then it is overwritten by the Thread ID value of thread created with the
t1.start() statement in main, which creates a new Thread, therefore new Thread ID.
And yet another Thread ID value is created with the new Thread(new Salmon()).start().

you can easily follow the process with inserting print out statements in the code at each step, like :

 
Jeet Jain
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for your reply. But the output shows that "id if loop before" always prints the same value for case I. That is static variable id never changes from the first t1's id. Which is why my output terminates with few errors but doesnt endlessly produce errors as the right answer says. Case II produces errors endlessly, obviously.(which i understood)
 
kris bako
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you get the first time into the public void run() the first Thread, which in my case has getID() = 8, ends with an ERROR.
at this point id is still 8, since it is only set once : in the main method.
But prior to ending the 1st Thread with ERROR, another Thread is created. In my case the second Thread has getID()=11.
Then you get to the if condition which is not met in this case, because id=8 (never changes) and the 2nd Thread has an getID() of 11.
So the if now reads (if i==2 AND 8==11) - which is not the case. So therefore it continues to print "2" and "3". And then end of game.
The E ansa say 'continues' - which it does, it does not say 'continues endlessly'.
 
Jeet Jain
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohhh thank you so much:) was pretty stupid of me
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic