aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Sun Sample Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Sun Sample Question" Watch "Sun Sample Question" New topic
Author

Sun Sample Question

liqiang yang
Ranch Hand

Joined: Jan 20, 2008
Posts: 92
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 ]

DY.
SCJP 5.0 (100%), SCWCD 5.0 (79%), SCBCD 5.0 (preparing...)
Jun Wu
Greenhorn

Joined: Mar 20, 2008
Posts: 14
You must follow rule :a is frist access ,b is second access .
Nevin kumar
Ranch Hand

Joined: Mar 15, 2008
Posts: 93
hey,
May anyone,share me the url for sun sample questions.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
liqiang yang
Ranch Hand

Joined: Jan 20, 2008
Posts: 92
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

Joined: Jan 20, 2008
Posts: 92
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Jan 20, 2008
Posts: 92
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

Joined: May 09, 2004
Posts: 91
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

Joined: Aug 31, 2007
Posts: 784

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


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Jart Bo
Ranch Hand

Joined: Oct 22, 2007
Posts: 144
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


SCJP, SCWCD
liqiang yang
Ranch Hand

Joined: Jan 20, 2008
Posts: 92
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

Joined: Jan 20, 2008
Posts: 92
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

Joined: Oct 22, 2007
Posts: 144
Thank you liqiang yang!
Niala Nirell
Ranch Hand

Joined: Mar 12, 2008
Posts: 46
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sun Sample Question
 
Similar Threads
thread question
Multithreading: Two errors with education.oracle.com sample exam question
synchronized
how does getId() gives value to thread
Thread Question from Sun's Free Mock Test