# doubt on java fundamental mock exam

R .sourav nayak

Ranch Hand

Posts: 67

posted 9 years ago

hi,

the following mock question is from DanC mock exam site and could some explain me how to calculate the output of this type of questions

class JJF3 {

public static void main(String args[]) {

System.out.print(Integer.toBinaryString(Byte.MAX_VALUE)+",");

System.out.print(Integer.toOctalString(Byte.MAX_VALUE)+",");

System.out.print(Integer.toString(Byte.MAX_VALUE)+",");

System.out.print(Integer.toHexString(Byte.MAX_VALUE));

}}

the result is

. Prints: 1111111,177,127,7f

thanks in advance

the following mock question is from DanC mock exam site and could some explain me how to calculate the output of this type of questions

class JJF3 {

public static void main(String args[]) {

System.out.print(Integer.toBinaryString(Byte.MAX_VALUE)+",");

System.out.print(Integer.toOctalString(Byte.MAX_VALUE)+",");

System.out.print(Integer.toString(Byte.MAX_VALUE)+",");

System.out.print(Integer.toHexString(Byte.MAX_VALUE));

}}

the result is

. Prints: 1111111,177,127,7f

thanks in advance

Keith Lynn

Ranch Hand

Posts: 2409

posted 9 years ago

Conversions from binary to octal and binary to hexadecimal and vice versa are pretty simple.

For example, if you have a binary string and you want to convert it to

1. octal, then left pad the String with 0 until the number of digits is a multiple of 3. Then divide the String into groups of 3 and convert each to its octal equivalent (000 = 0, 001 = 1, 010=2, 011=3, 100=4, 101=5, 110=6, 111=7).

2. hexadecimal, then left pad the String with 0 until the number of digits is a multiple of 4. Then divide the String into groups of 4 and convert each to its hexadecimal equivalent (0000 = 0, 0001 = 1, 0010 = 2, 0011 = 3, 0100 = 4, 0101 = 5, 0110 = 6, 0111 = 7, 1000 = 8, 1001 = 9, 1010 = A, 1011 = B, 1100 = C, 1101 = D, 1110 = E, 1111 = F).

For the reverse, its even easier.

To convert from

1. octal to binary, replace each digit with its three digit binary equivent.

2. hexadecimal to binary, replace each digit with its four digit binary equivalent.

For example, if you have a binary string and you want to convert it to

1. octal, then left pad the String with 0 until the number of digits is a multiple of 3. Then divide the String into groups of 3 and convert each to its octal equivalent (000 = 0, 001 = 1, 010=2, 011=3, 100=4, 101=5, 110=6, 111=7).

2. hexadecimal, then left pad the String with 0 until the number of digits is a multiple of 4. Then divide the String into groups of 4 and convert each to its hexadecimal equivalent (0000 = 0, 0001 = 1, 0010 = 2, 0011 = 3, 0100 = 4, 0101 = 5, 0110 = 6, 0111 = 7, 1000 = 8, 1001 = 9, 1010 = A, 1011 = B, 1100 = C, 1101 = D, 1110 = E, 1111 = F).

For the reverse, its even easier.

To convert from

1. octal to binary, replace each digit with its three digit binary equivent.

2. hexadecimal to binary, replace each digit with its four digit binary equivalent.

sadasiva kumar

Ranch Hand

Posts: 91

posted 9 years ago

A byte is an 8 bit signed value. The left most bit is the sign bit. The sign bit is set to zero for positive numbers and is set to one for negative numbers. The most positive byte value is represented as a sign bit that is set to zero and all of the other bits set to one. The Integer.toBinaryString method does not print leading zeros; so only seven bits are printed. An eight bit binary value is represented as three octal digits. The first of the three digits represents the left most two bits of the binary value. In this case, the left most two bits are zero and one. The second octal digit represents the next three bits of the binary value. The last of the octal digits represents the right most three bits of binary value. Note that the Integer.toOctalString method does not print a leading zero as is required for an octal literal value. An eight bit binary value is represented as two hexadecimal digits. The left hex digit represents the left most four bits of the binary value. The right hex digit represents the right most four bits of the binary value.

So the output 1111111,177,127,7f

So the output 1111111,177,127,7f

SADASIVAKUMAR UTTI, SCJP1.4

A bend in the road is not the end of the road ... unless you fail to make the turn.

Bert Bates

author

Sheriff

Sheriff

Posts: 8919

11