This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread 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 Java 8 in Action this week in the Java 8 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: 118
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: 2891
    
  19

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


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
My Home Page -- Twitter Profile -- JavaRanch FAQ -- 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.
 
Similar Threads
Exam 7, Q18 Dan Chisholm
Operator precedence and arrays
JQ+ question
Dan's question about declarations and access control
Dan's Mock: Field declaration Q 7