This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads question." Watch "Threads question." New topic
Author

Threads question.

Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 119
Hi! I am studying Bates and Sierra's book page 770 question 16.

The answer says "c has not been assigned to an object". But isn't c assigned to new Chicks() on line 17?

Please advice. Thanks.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3060
    
  38

Alan, not everyone has that book. Please post the entire question and ShowSomeEffort


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Rohini Shirpewar
Greenhorn

Joined: Jan 25, 2011
Posts: 15

Here is the complete question.




Which are true? (Choose all that apply.)
A. Compilation fails
B. The output could be 4 4 2 3
C. The output could be 4 4 2 2
D. The output could be 4 4 4 2
E. The output could be 2 2 4 4
F. An exception is thrown at runtime


The c at line 17 is assigned, however it belongs to the Thread main. Notice that, two new Threads are provided with new ChicksYack(), they have their own c, which has not been initializes anywhere.

HTS.

Rohini.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Rohini Shirpewar wrote:

The c at line 17 is assigned, however it belongs to the Thread main.

Nope, it's belong to an instance of ChicksYack.
Rohini Shirpewar wrote:
which has not been initializes anywhere.

Then, what about the go method? what's happening inside that?

|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Rohini Shirpewar
Greenhorn

Joined: Jan 25, 2011
Posts: 15
Hello Abimaran,

Yes you are correct the c at line 17 belongs to the instance of ChicksYack.

However, at line 18 and 19 the new Thread is provided with new ChicksYack, which have their own c and those are not assigned anywhere.
Right? or my understanding isn't correct here ?

Rohini.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Rohini Shirpewar wrote:
However, at line 18 and 19 the new Thread is provided with new ChicksYack, which have their own c and those are not assigned anywhere.
Right? or my understanding isn't correct here ?

Rohini.


mm, That's correct. You'll get NullPointerException.
fadi aboona
Ranch Hand

Joined: Apr 25, 2010
Posts: 71
Abimaran Kugathasan wrote:
Rohini Shirpewar wrote:
However, at line 18 and 19 the new Thread is provided with new ChicksYack, which have their own c and those are not assigned anywhere.
Right? or my understanding isn't correct here ?

Rohini.


mm, That's correct. You'll get NullPointerException.


if i provide a constructor for ChicksYack and inside it i assign new Chicks() to c then that would have no effect on the synchronized yack(long id) because now i have two different Chicks instances, please confirm.

Thanks,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads question.