Page: 5
3. Operators
22. Given: class Knowing { static final long tooth = 343L; static long doIt(long tooth) { System.out.print(++tooth + " "); return ++tooth; } public static void main(String[] args) { System.out.print(tooth + " "); final long tooth = 340L; new Knowing().doIt(tooth); System.out.println(tooth); } } What is the result? A. 343 340 340 B. 343 340 342 C. 343 341 342 D. 343 341 340 E. 343 341 343 F. Compilation fails. G. An exception is thrown at runtime. Answer: -> D is correct. There are three different long variables named tooth. Remember that you can apply the final modifier to local variables, but in this case the 2 versions of tooth marked final are not changed. The only tooth whose value changes is the one not marked final. This program demonstrates a bad practice known as shadowing. ->A, B, C, E, F, and G are incorrect based on the above. (Objective 7.3) 23. Given: 1. class Bigger { 2. public static void main(String[] args) { 3. // insert code here 4. } 5. } 6. class Better { 7. enum Faster {Higher, Longer}; 8. } Which, inserted independently at line 3, will compile? (Choose all that apply.) A. Faster f = Faster.Higher; B. Faster f = Better.Faster.Higher; C. Better.Faster f = Better.Faster.Higher; D. Bigger.Faster f = Bigger.Faster.Higher; E. Better.Faster f2; f2 = Better.Faster.Longer; F. Better b; b.Faster = f3; f3 = Better.Faster.Longer; Answer: -> C and E are correct syntax for accessing an enum from another class. ->A, B, D, and F are incorrect syntax.
Page: 5
1
2
3
4
5
|