About Question enthuware.ocajp.i.v7.2.1159 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
The_Nick

Re: About Question enthuware.ocajp.i.v7.2.1159 :

Post by The_Nick »

I would like to add this post from stackoverflow.com
It helps to clarify the point of this question in my view.
http://stackoverflow.com/questions/1253 ... en-compare

DesRenthuware
Posts: 32
Joined: Wed Aug 28, 2013 6:12 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1159 :

Post by DesRenthuware »

Very interesting question - assumed that the answer would be an overflow (so got it wrong/right?). Remembered the min and max vals would be one out, but expected mathematical rules to take precedence (i.e. -(-x) = +x) and so would overflow.

javanaut
Posts: 22
Joined: Wed Aug 21, 2013 12:29 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1159 :

Post by javanaut »

Please let me know if I am wrong, but I think the explanation for this question is a little misleading when describing two's complement.

The second step is to add 1 but it shows adding 32 1's to the complemented number, which is not correct if I am not mistaken. In other words it is like the explanation is saying to add the MAX_VALUE in binary of the number to the complemented (everything switched to the opposite) binary value. :shock:

Regards,

Javanaut

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1159 :

Post by admin »

I don't see it adding 32 1's. It shows the number that you get after adding 1 to step 1.
Step 1 (complement the bits of 1): ///11111111 11111111 11111111 11111110///
Step 2 (add 1 to step 1): ///11111111 11111111 11111111 11111111///.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

javanaut
Posts: 22
Joined: Wed Aug 21, 2013 12:29 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1159 :

Post by javanaut »

Oh yeah, I just added it on paper in binary and adding one to the complemented bits in step one does produce 32 1's. I see what the explanation is saying now.

Thanks Paul. :D

Regards,

Javanaut

javanaut
Posts: 22
Joined: Wed Aug 21, 2013 12:29 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1159 :

Post by javanaut »

Code: Select all

// java program by javanaut :D

public class Foo {

        static public void main(String[] args) {

                Integer x = 0b11111111_11111111_11111111_11111111;
                System.out.println(x);

                Integer x1 = Integer.MAX_VALUE;
                System.out.println(x1);

                System.out.println(Integer.toBinaryString(x));
                System.out.println(Integer.toBinaryString(x1));

        }



}
 

This is the output on my machine:

Code: Select all

-1
2147483647
11111111111111111111111111111111
1111111111111111111111111111111
Hey all,

I made this program to try and experiment with some of this - binary numbers, two's complement, etc. I noticed that the first toBinaryString() method in my program prints out one more 1 that the second call to this method. Can someone please explain?

The arguments to the toBinaryString() method are -1 in x and MAX_VALUE found in x1.

Regards,

Javanaut

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1159 :

Post by admin »

That is because there is one more 1 in the binary representation of -1 than in MAX_VALUE. Please read how -1 is represented in a 32 bit int.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

javanaut
Posts: 22
Joined: Wed Aug 21, 2013 12:29 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1159 :

Post by javanaut »

Paul,

Thank-you for the reply. How odd that MAX_VALUE has only 31 bits when it is an int that is a 32 bit data-type. :o

This part of Java is not really on the OCAJP7 exam though?

Thank-you again for helping me understand what I was missing with the output of my program.

Regards,

Javanaut

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1159 :

Post by admin »

I didn't say MAX_VALUE has only 31 bits. I said it has 31 1s. The MSB is 0 and is probably not printed because of the way toBinaryString method is coded.

No, this is not required for the exam.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

javanaut
Posts: 22
Joined: Wed Aug 21, 2013 12:29 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1159 :

Post by javanaut »

Intense.

I had to look up MSB https://en.wikipedia.org/wiki/Most_significant_bit

I can see guess the MSB is dropped since it is a zero or something sort of like how octals are printed by Java if I am not mistaken.

Yes, I am glad this is not on the exam. :mrgreen:

thupten
Posts: 4
Joined: Thu Jan 09, 2014 3:21 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1159 :

Post by thupten »

Has this question ever appeared on the test?
I think most would choose the Overflow exception as the answer. This is totally unexpected behavior. There probably are many programs out there with bug related to this. :(

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1159 :

Post by admin »

As mentioned in previous posts, this much detail about 1s and 2s complement is not required for the exam.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Sergey
Posts: 39
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1159 :

Post by Sergey »

I did not understand the explanation in programm, but i find this in the web:

Code: Select all

Basically, because Integer.MAX_VALUE is actually only 2147483647, so -Integer.MIN_VALUE, which would be +2147483648, actually overflows the capacity of the internal binary representation of integers. Thus, the result "loops around" back to Integer.MIN_VALUE, or -2147483648.
is it correct?

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1159 :

Post by admin »

Yes, the explanation explains the reason why "loop around" happens.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 45 guests