Java Magazine, Nov/Dec 2017
ORACLE COM JAVAMAGAZINE NOVEMBER DECEMBER 2017 95 fix this that the digits 10 in octal represent one batch of eight and no extra units whereas 10 in base 10 represents one batch of ten and no extra units Java source code indicates that the programmer is using octal by putting a leading zero on the digit stream In other words the literal assignment in int a 012 is actually assigning a value of 10 not 12 Thats one batch of eight and two extra units Importantly however the compiler is completely happy with this format see Java Language Specification section 3101 Therefore option A which suggests this assignment results in a compilation failure is incorrect The parseInt method takes an optional second parameter which is the radix or more simply the base of the representation The single argument overload of this method always assumes a base 10 conversion The two argument overload used here will convert the text to base 10 because I specifically provided a second argument Therefore the value of the variable c will actually be 12 Given that variable a contains the value 10 and both b and c contain 12 its clear that the only true statement is b c which tells you that option D is the correct answer Question 2 The correct answer is option D This question investigates Javas casting and initialization rules These sometimes seem a bit inconsistent but theres actually sound logic to them On line 14 theres an attempt to assign a literal value 1234567890123456789 to a variable of type long The variable is big enough to hold a number that large but unfortunately the expression on the right side of the assignment is taken to be an int expression An int is not big enough to hold that it is limited to a little over 2 x 109 so this line fails to compile You could fix this by appending the single letter L to the numeric literal as shown below Although its somewhat unusual for Java the L is not case sensitive in this situation but its generally a bad idea to use lowercase because it looks too much like a 1 in many fonts 14 long i1 1234567890123456789L That sufix tells the compiler its building a long literal and then things work as expected
You must have JavaScript enabled to view digital editions.