• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question With Respect to Java Champ

 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,

 
Henry Wong
author
Marshal
Pie
Posts: 21225
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops!!

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

Best Regards,
 
Muneeswaran Balasubramanian
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prithvi,
What about your result?
 
Muneeswaran Balasubramanian
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prithvi,
I try to run this code.This yields the output as 77777.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When are you planning your exam?

Best Regards,
 
Muneeswaran Balasubramanian
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Prithvi,
Thanks for your reply.I got your point.
Thanks alot.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Munee,

You are welcome.

Best Regards,
 
Samrat Debroy
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Samrat,

That was valuable and thoughtful. Thanks for sharing.

Best Regards,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic