Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

a Q from mugal engine

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
if anyone here saw this Q on the mugal exam and answered it right,please help me and explain it it's totaly ambiguous:

the question asks if any thread can call methods a,b,and c at any time what is the guaranteed values for i.v,j.v and k.v?
i tried it even the main thread can't go in because they are all initialized to null
only those who saw this Q reply
thanks
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per your request, I never saw this Q...but I hope my answer will suffice?
Any thread can call any method on an instance of class Counter, each instance of class Counter has it's own "thread monitor key", what this means is that access to any synchronized method must happen one thread at a time. If more than one thread tries to access one of the synchronized methods, only one will be given access while the others will block until that thread is done processing the synchronized method.
Since each method has the same number of decrement operations as it does increments, this guarantees that at any one time the values for i.v, j.v, and k.v will be zero!
[ February 03, 2002: Message edited by: Rajinder Yadav ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajinder,
the problem that aymen wanted to stress is that the Counter variables i,j and k are not explicitely initialized (that is they will be null by default), thus, as soon as you invoke either method a, b or c on an instance of class Test, there is gonna be a NullPointerException.
Add the following main method to the code provided by aymen:

The first method to be invoked is a() and its first statement is i.inc()
Since i is null, the invocation i.inc() will throw a NullPointerException at runtime.
 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rajinder Yadav:

Since each method has the same number of decrement operations as it does increments, this guarantees that at any one time the values for i.v, j.v, and k.v will be zero!
[ February 03, 2002: Message edited by: Rajinder Yadav ]


Excluding the deal with thowing the null pointers since Counters wouldn't be intialized, but assuming they were, how you guarantee k will always be zero? Since c() isn't declared as a synchronized, the inc() and dec() could be called from different threads meaning it's possible one could get called and another get called more than once, which would make the answer not being equal for k's values.
Am I wrong here? I'm more concerned with this thread issue. I'm assuming that the Counter references are all initialized at some point first.
 
aymen esawey
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
thanks for your interest in the Q but according to
maha ana mock list she has done every Q so can't anyone get her to answer i know she is not a moderator here.
or could any scjp of you take the mugal exam(won't be that hard for you now).

here is the whole Q and thanks:

Given the following code, which statements concerning the objects referenced through the member variables i, j and k are true, given that any thread may call the methods a, b and c at any time?

common it wouldn't hurt me if i found that am stupid
[ February 04, 2002: Message edited by: aymen esawey ]
 
aymen esawey
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
by the way all the methods end up doing nothing to the values in question(they inc then dec whats inc'ed)
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should'nt the inc/dec methods in the Counter class be declared as static? And should'nt the Counter class member variable v should be static too.
Am I missing something here?
[ February 05, 2002: Message edited by: Brian Lugo ]
 
Getting married means "We're in love, so let's tell the police!" - and invite this tiny ad to the wedding:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic