• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sun Sample Question

 
liqiang yang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given:

5. public class Lockdown implements Runnable {
6. public static void main(String[] args) {
7. new Thread(new Lockdown()).start();
8. new Thread(new Lockdown()).start();
9. }
10. public void run() { locked(Thread.currentThread().getId()); }
11. synchronized void locked(long id) {
12. System.out.print(id + "a ");
13. System.out.print(id + "b ");
14. }
15. }
What is true about possible sets of output from this code?
A. Set 6a 7a 7b 8a and set 7a 7b 8a 8b are both possible.
B. Set 7a 7b 8a 8b and set 6a 7a 6b 7b are both possible.
(Two different Lockdown objects are using the locked() method.)
C. It could be set 7a 7b 8a 8b but set 6a 7a 6b 7b is NOT possible.
D. It could be set 7a 8a 7b 8b but set 6a 6b 7a 7b is NOT possible.



I pick up C. I don't know how its ANS is B. Anybody can explain it? Thanks.
[ March 24, 2008: Message edited by: Ben Souther ]
 
Jun Wu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must follow rule :a is frist access ,b is second access .
 
Nevin kumar
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,
May anyone,share me the url for sun sample questions.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
liqiang yang,

Please be sure to always QuoteYourSources when posting mock questions to JavaRanch.

Once an accurate link to the source of this question is posted, I'll be happy to restore the question.

Thanks,
Ben
 
liqiang yang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually this question came from sun's sample questions for 310-065

Anybody who register and login in sun web site can get it but only 10 questions (all those things are totally free).

All those information are public to everybody legally, Make sure look posts very carefully. It is the "SAMPLE" questions "NOT REAL" questions. Just like all mock exam questions on the web. It's hard to understand why the question should be taken away.
 
liqiang yang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naveen,
You can goto http://faq.javaranch.com/java/ScjpMockTests
and follow the 7th links in that list

7 / Sun's SCJP2 Site / 10 / Sun offers a few example SCJP2 test questions from their certification site. Don't miss them.

just take a little bit time to do registeration and get a student ID for free at SUN training center and login then you got it. It's easy.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by liqiang yang:
Actually this question came from sun's sample questions for 310-065

Anybody who register and login in sun web site can get it but only 10 questions (all those things are totally free).

All those information are public to everybody legally, Make sure look posts very carefully. It is the "SAMPLE" questions "NOT REAL" questions. Just like all mock exam questions on the web. It's hard to understand why the question should be taken away.



Thank you.
-Ben
 
liqiang yang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still stuck to this question. Can anyone can help? Thanks.

Because "synchronized void locked(long id)", so at the certain moment only one Thread can exclusively access locked method. Therefore, it's impossible to get output like:
6a 7a 6b 7b(Thread 7 enter locked() before Thread finished to print out "b")
which means 2 Threads concurrently access locked method. Am I right?
 
Irina Goble
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by liqiang yang:
I still stuck to this question. Can anyone can help? Thanks.


Liqiang, the answer is right in your first post.
(Two different Lockdown objects are using the locked() method.)

May be Ben Souther's helped you?
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
yang

Option B is correct answer

Explanation:
1.two thread objects created
2.method is synchronized but actually it does not affect two threads
3.each thread having its own lock both are diffrent objects
4.control goes sequentially therefore print order is always "a" and then "b"
5.b is not printed before a this is never possible

If I am wrong anywhere in explanation please notify me for that

Regards
Ninad
 
Jart Bo
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi liqiang yang,

I tried to go to the Sun Certification site and registered for a Student ID. I am currently logged in already but cannot find the link where the questions are. Could I ask you to please tell me where exactly the free sample questions can be taken for SCJP 5.0 & 6.0?

Thanks,
Jart
 
liqiang yang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot.

The "a" must be followed by "b". This is perfectly right for sure.

I just can't understand why those two threads (identified by its ID#, like 6, 7, 8 or 9 and so on) for example, how to translate this : 6a 7a 6b 7b

6a -> Thread6 print out "a", just locked's first line and not finished locked
yet, Now Thread6 still hold the lock and try to execute second line of
locked.
7a -> But at this point, Thread7 coming up to execute the first line of
locked(print out the "a")


So Thread6 and Thread7 access the method locked which was marked as synchronized. Also I put this code on my IDE and make a big loop number to run it and the output can support my thoughts. This is why I am so confuse about this question.
 
liqiang yang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this http://www.sun.com/training/catalog/courses/WGS-PREX-10-QUEST.xml
and click on add the course to shopping cart (0.00$) then follow the links

The link for 310-055 is not on the list. I don't know why.
 
Jart Bo
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you liqiang yang!
 
Niala Nirell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

If I change the question to

public class Lockdown implements Runnable {
public static void main(String[] args) {
Lockdown lock = new Lockdown();
new Thread(lock).start();
new Thread(lock).start();
}
public void run() { locked(Thread.currentThread().getId()); }
synchronized void locked(long id) {
System.out.print(id + "a ");
System.out.print(id + "b ");
}
}

The answer would be:
C. It could be set 7a 7b 8a 8b but set 6a 7a 6b 7b is NOT possible.

Am I right?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic