Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# doubt on java fundamental mock exam

R .sourav nayak
Ranch Hand
Posts: 67
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

Keith Lynn
Ranch Hand
Posts: 2409
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.

sadasiva kumar
Ranch Hand
Posts: 91
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

Bert Bates
author
Sheriff
Posts: 8945
17
Hi Guys,

If this is in scope at all, it's just ever-so-barely in scope, and then only for the 1.4 exam.

R .sourav nayak
Ranch Hand
Posts: 67
hi,
i am writing 1.4 on April, so i have to go over all these topics

 It is sorta covered in the JavaRanch Style Guide.