Java Magazine, March/April 2016
ORACLE COM JAVAMAGAZINE MARCH APRIL 2016 65 fix this Given this its clear that the public modifier must be removed from the constructor However while this is necessary it is not suficient Lets see why In general constructors may not be final they are not inherited so it makes no sense to use this modifier Section 883 states Unlike methods a constructor cannot be abstract static final native strictfp or synchronized So this tells you that its necessary to remove both the public and final keywords from the constructor The only option that provides for this requirement is Option B so this must be part of the correct answer This solution also eliminates Option A Option C calls for removing the public keyword from the enum class as a whole This is unnecessary its perfectly correct to have a public enum Section 811 mentions that the class modifiers of a normal class may include public Later section 89 modifies the information to be specific to enums It prohibits abstract and final but it does not prohibit the others ensuring that public is acceptable Because of this Option C must be incorrect Lastly when an explicit constructor is defined for any class the default constructor is not generated by the compiler section 889 Because of this line n2 as shown in the original will not compile This is because the format it uses has no parentheses and no argument lists therefore it attempts unsuccessfully to invoke that missing default constructor Section 891 notes that when an argument list is provided these arguments are passed to the constructor of the enum It also notes that normal overload matching rules will be applied Because of this and the format specified in this same section the proper format for the invocation is as shown in Option E The form in Option E that uses a new keyword is a syntax error Because of this Option D is the second correct option and Option E is wrong Note for certification exam students You might wonder how an item can be correct when it refers to Color RED and Color BLACK in the argument to the constructor but it does not define or import any Color class In the interest of keeping the amount of code you have to examine within reasonable limits Oracle has documented some assumptions that should be made when considering a question These are listed on the exam information pages In particular the following assumptions are mentioned explicitly Missing package and import statements If sample code does not include package or import statements and the question does not explicitly refer to these missing statements then assume that all sample code is in the same package and import statements exist to support them Given this its clear that its proper to assume that some class Color exists and it provides for these constants If youre unfamiliar with it this is probably the original java awt Color class but such detail isnt important here Most of the notes that Oracle has provided might be considered obvious That is if you didnt make these assumptions many questions would have no plausible answer However its nice that theyre now called out explicitly so you dont have to worry if youre making an unreasonable assumption You can find these notes on the pages of each relevant exam usually by selecting the Exam Topics tab thats about halfway down Question 2 The correct answer is Option C FileNotFound Exception is a subclass of IOException Where exceptions in a class hierarchy are both being caught explicitly such as in Option A the more specific exception must be positioned earlier in the list than the more general exception This ordering rule exists because execution will jump to the first catch block that is applicable section 14201 of the Java Language Specification and if the more general were first the more specific would never be executed In Option A this rule is broken hence the code fails to compile and
You must have JavaScript enabled to view digital editions.