*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dont�t understand the result Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dont�t understand the result" Watch "Dont�t understand the result" New topic
Author

Dont�t understand the result

Stephan Deve
Greenhorn

Joined: Sep 21, 2004
Posts: 13
Hello,
i have a problem with the following question (from javacertivicate.com):

Meanwhile i know that the answer is 31. So i can choose the right answer everytime i see the question, but i still don�t understand what is happening.
After x += x << 3; x is 54
After y += z << 2; y should be 25
If i add 54 and 25 i think this should be 79. So why is 31 correct ??



greetings
Stephan
Rajasekar Elango
Ranch Hand

Joined: Sep 13, 2004
Posts: 105
Hi,

Carefully look at test method.


SCJP 1.4
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
a bit tricky

do so :-- public void test(int d(or anyother identifier except x), int z)
and you'll get your answer 79.

do:-- public void test(int x, int z)
you get x= 6, z = 25
Rajasekar Elango
Ranch Hand

Joined: Sep 13, 2004
Posts: 105
Hi,

Sorry for incomplete previous post.

Look at


Within the test method parameter variable x is redeclared, So only the local variable x is modified and not static variable x defined in class Pass.

But variable y is not declared within test and y += z << 2, modified y to 25.

So in the System.out.println(x+y) is 6 + 25 = 31 (x has the value assigned to 6 in main() before calling test())

The variable x in test is said to shadowed the same variable declared in class.

- Rajasekar
Celso Serrano Araujo
Greenhorn

Joined: Dec 10, 2004
Posts: 3
Note que x += x << 3; is assingment 54 in the automatic variable x and not variable member static x
Stephan Deve
Greenhorn

Joined: Sep 21, 2004
Posts: 13
I got it now.
Thanks a lot.

Changing

to

helped a lot to make clear how this code works.


greetings
Stephan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dont�t understand the result
 
Similar Threads
how to compare the values in arrays
Qn on assertion.
Doubt In Kathy& Bates book questions at page 198
two possible answers Q18 ch 4 page 282 from KB
Chapter 5 K&B -Doubt