File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question With Respect to Java Champ 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 "Question With Respect to Java Champ" Watch "Question With Respect to Java Champ" New topic
Author

Question With Respect to Java Champ

Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Dear All,

I was just trying to attempt some questions over javachamp.

I noticed at times there are wrong answers stored. Is that the case with javachamp? For example



The options presented were given below

1-99777
2-97777
3-77777
4-79999
5-99999

I picked out the answer as 2. But when i pressed Evaluate my answer, it says, Wrong guess. I tried every other even, all answers wrong except 79999
which cannot be a possible output i suppose. Can it be? Or the exam engine has some problem at javachamp.

Looking forward to have a feedback.

Best Regards,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

Prithvi Sehgal wrote:
I picked out the answer as 2. But when i pressed Evaluate my answer, it says, Wrong guess. I tried every other even, all answers wrong except 79999
which cannot be a possible output i suppose. Can it be? Or the exam engine has some problem at javachamp.


Maybe you misread the question -- and it asked which output was not possible?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Oops!!

Thanks Henry. It's been my mistake i believe.

Best Regards,
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi Prithvi,
What about your result?


Cheers Munees
My Blog
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi Prithvi,
I try to run this code.This yields the output as 77777.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Dear Munee,

Actually, i read the question wrong. It was what output is not possible. So yes 7777 is a possible output.
Thread behavior is not really guarenteed. On my JVM it was 97777.
I was busy at some project deadlines at office so late sittings and therefore couldn't carry on
with scjp preparation and was away from ranch also. So continued back, hopefully, next week i will take my
exam date or end of May. What about yourself?

Best Regards,
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi Prithvi,
First of all best of luck to your exam.
Then,Now only i am march through scjp.
My preparation is going on.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
When are you planning your exam?

Best Regards,
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi Prithvi,
I have one doubt.Please clarify this.
Whene ever we run the below code

I think,
It calls the run method and assign the value 7 to code by code=7.
Then when ever we try to access the thread.code it yields only 7 always.

Thread behavior is not really guarenteed. On my JVM it was 97777.

How it is possible?
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Dear Munee,

It is possible because of the following reason.

1- Please note that there are two threads currently. One is the main thread itself and other is the one referred by thread instance.
2- Now though we say something like thread.start(), it just moves the thread to the runnable state. So it is possible that first main
thread moves ahead and enters the loop and prints the value of thread.code which will be at this stage 9.
3- Then main method is kicked out and thread gets a chance to run. At this point it sets the value of code to 7 inside the run method.
4- Now even if main thread starts executing, the value printed will still be 7. Thats how i got the result as 97777.

Clue, in my JVM, main thread was the first one enter the loop to print the first value, and then scheduler kicked it and the other thread
moved from runnable to running and changed the value of code to 7. Then whoever takes the turn, 7 will be printed always after the first.

Hope this helps,
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Dear Prithvi,
Thanks for your reply.I got your point.
Thanks alot.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Dear Munee,

You are welcome.

Best Regards,
Samrat Debroy
Greenhorn

Joined: Oct 23, 2009
Posts: 13
One trivial observation.
If you mark the integer variable "code" as volatile:

then the result will always be 99999.
Please excuse my ranting but i thought this might be useful.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Dear Samrat,

That was valuable and thoughtful. Thanks for sharing.

Best Regards,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question With Respect to Java Champ