• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Question from examlab practice test 3

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from practice exam 3 of examlab



here answer is D. will compile succesfully if volatile keyword is removed
I. prints 000000

Here i could not understand if the current code gives compiler error then how come it is printing 000000
even if we remove the voatile keyword then i think it should give output as 221210. please clear my doubt.

thanks,
geeta vemula
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will give you a hint.


Concentrate on this line, and do your best to try why? And tell me the answer also.
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh i was so stupid...

First all the instantiation done where x will become 0 in tht line which is same since it is class variable. so printing 000000.

Thanks,
for clearing my doubt..
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

geeta vemula wrote:oh i was so stupid...

First all the instantiation done where x will become 0 in tht line which is same since it is class variable. so printing 000000.

Thanks,
for clearing my doubt..



Thanks for explaination guys.
That line tricked me too.
I also thought the answer should be '000' becuase the print statement is executed three times.
But I manually went through the code step by step and figured out that the static String variable 's' keeps the previous '0's and thus prints out 6-'0's.
Very tricky!
-Thank You
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic