Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public static void main(String[] args ) { char A = 65535, B = 0; System. out .pr

ID: 3897712 • Letter: P

Question

public static void main(String[] args) {

char A = 65535, B = 0;

System.out.println((int)++A);

    System.out.println((int)--B);

}

Unsigned Integer Representation
Consider the following Java program to answer questions 14 – 15.

Java implements char data type as
unsigned integer with size 2 bytes.
Note that, 216 = 65536    

1.       What will be printed in the first line? Why?

a.       65536, because A will be pre-incremented before printing                    

b.      65535, because 65535 is the largest possible number in 2-byte unsigned representation. A cannot be incremented.

c.       0, because 65535 is the largest possible number in 2-byte unsigned representation. ++A will overflow to the smallest possible number

d.      Nothing will be printed.

2.       What will be printed in the second line? Why?

a.       -1, because B will be pre-decremented before printing          

b.      0, because 0 is the smallest possible number in 2-byte unsigned representation. B cannot be decremented

c.       65535, because 0 is the smallest possible number in 2-byte unsigned representation. --B will overflow to the largest possible number.

d.      Nothing will be printed.

3.       What is FFFF-FFFE in 2-byte unsigned integer representation?

a.       1                       

b.      10                          

c.       111

d.      110

Integer Representation – Signed Magnitude & Two’s Compliment

4.       What is the disadvantage of signed-magnitude representation (compared to 2’s compliment representation)?

a.       Two zeros      

b.      Requires separate hardware units for Adding & Subtracting

c.       May need to compare the magnitudes of the numbers before adding or subtracting

d.      All of the above

public static void main(String[] args) {

int x = 32767; y = -32768;

System.out.println(++x);

    System.out.println(--y);

}

Consider the following Java program to answer questions 18 – 19.

Java implements Short data type as
2’s complement integer with size 2 bytes.
Note that, 215 = 32767    

5.       What will be printed in the first line? Why?

a.       32768, because x will be pre-incremented before printing                     

b.      32767, because 32767 is the largest possible number in 2-byte 2’s compliment representation. x cannot be incremented.

c.       0, because 32767 is the largest possible positive number in 2-byte 2’s compliment representation. ++x will overflow to the smallest possible positive number

d.      -32768, because 32767 is the largest possible positive number in 2-byte 2’s compliment representation. ++x will overflow to the smallest possible number

public static void main(String[] args) {

char A = 65535, B = 0;

System.out.println((int)++A);

    System.out.println((int)--B);

}

Explanation / Answer

1. c: 0
0, because 65535 is the largest possible number in 2-byte unsigned representation. ++A will overflow to the smallest possible number.

2. c: 65535
65535, because 0 is the smallest possible number in 2-byte unsigned representation. --B will overflow to the largest possible number.

3. a. 1
FFFF- FFFE results in 1.

4.
d. All of the above