(ct) MasterExam ANSWERS 1. C and E are correct. A, B, and D are incorrect. method is not a keyword, Long is a wrapper class different from the primitive long, and finalize is a method name, not a keyword. 2. B is correct. The word default is a valid keyword. A, D, and E are not keywords. C is wrong because the keyword for subclassing in Java is extends, not 'subclass'. 3. C, and F are correct. C uses a redundant cast, but is legal. F is the proper syntax to declare a null String. A, B, D, and E are all incorrect declarations for a String. A is a legal char literal. 4. C, and F are correct. C is a legal boolean expression which evaluates to false. F is the correct syntax to declare a boolean. A, B, D, and E are all incorrect. B and E may be legal in other languages, but not Java. 5. F is correct. Line 4 only declared the top level, array of array references. Line 7 attempts to reference a second level array that has not been declared. A, B, C, D, and E are incorrect based on the logic described above. 6. D is correct. It is a legal way to create an array of four integers, named 'array'. A, B, C, E, and F, are invalid attempts to declare and initialize an array, none of them will compile. 7. D is correct. The elements of the instance variable array d are automatically initialized to 0.0 (double and float defaults are 0.0 not 0). A, B, C, E, and F are incorrect based on the logic described above. 8. C is correct. Arrays are zero based, so argh[1] contains the second argument, 2. A, B, D, E, and F are incorrect based on the logic described above. 9. E is correct. Default access will provide this protection. A is wrong because native is not an access modifier. B and D are wrong because public and protected are less restrictive than default. C is too restrictive. 10. E is correct. protected is less restrictive than default. B is wrong because static is not an access modifier. A, and D are wrong because these modifiers are less restrictive. C is wrong because private is more restrictive. 11. F is correct. HashMap is always fully qualified so it does not need an import statement. A, B, C, D, and E are incorrect because of the logic described above. B and D are incorrect syntax. 12. C and D are valid declarations within an interface. A and B are incorrect because variable declarations within an interface cannot be modified with the protected or abstract modifiers. E and F are wrong because method declarations within an interface cannot be static or final. 13. C is correct. This is the valid method signature for the required run( ) method. A, B, D, E, and F are incorrect because of the logic described above. 14. E is correct. E is correct because interfaces and abstract classes do not need to fully implement the interfaces they extend or implement (respectively). A is incorrect because a class cannot extend an interface. B is incorrect because an interface cannot implement anything. C and D are incorrect because the methods being implemented have mismatching signatures. 15. D is correct. The code will not compile because in line 5, the line will work only if we cast the operation back to a short: short s3 = (short) (s1 - s2); A, B, C, and E are incorrect because the code does not get as far as compiling. 16. B is correct. The ~ operator flips all the bits. The ^ operator produces a 1 bit when exactly one bit is 1. The & operator produces a 1 bit when both bits are 1. A, C, D, and E, are incorrect based on the program logic described above. 17. C and F both evaluate to 1. A evaluates to 16. B evaluates to 20. C looks like 2 to the 3rd power, but ^ is the Exclusive OR operator so it evaluates to 1. D evaluates to 32. E evaluates to 3. F evaluates to 1 (8 >> 3 is not 8 to the 3rd). 18. D is correct. The variable x is incremented on every iteration. The variable y is doubly decremented on the first two iterations and singlely decremented on the last two iterations because of the short circuit operator. A, B, C, E, and F are incorrect based on the program logic described above. 19. D is correct. Line 6 is not testing the values of b2 and b1, it is setting them to false. A, B, C, E, and F are incorrect based on the program logic described above. 20. D is correct. The reference variables a1 and a3 refer to the same long array object. When the [2] element is updated in the adjust() method, it is updating the array referred to by a1. A, C, D, E, and F are incorrect based on the program logic described above. 21. C is correct. When the alter() method is first entered, it is passed a copy of int x. The alter() method works on the copy, not the original. A, B, D, E, and F are incorrect based on the program logic described above. 22. E is correct. case expressions must be constant expressions. Since y is marked final, lines 8 and 7 are legal; however x is not a final so the compiler will fail at line 9. A, B, C, D, and F, are incorrect based on the program logic described above. 23. B is correct. The for loop runs 4 times. The first time cases 3, 2, and 1 execute. The second time cases 2 and 1 execute. The third time case 1 executes, and the fourth time case 0 executes. A, C, D, E, and F are incorrect based on the program logic described above. 24. D is correct. Using the int x as a boolean expression in the while statement, or any other looping or conditional construct, will result in a compiler error. This is old C syntax, not valid Java. A, B, C, E, and F are incorrect because line 1 is valid (Java is case sensitive so While is a valid class name. 25. E is correct. Line 16 won't compile because y is out of scope. A, B, C, D, and F are incorrect based on the program logic described above. 26. C is correct. Because m2( ) doesn't handle its exception it is passed to main( ) which does handle it. A, B, D, E, and F are incorrect based on the program logic described above 27. A is correct. The main() method properly catches and handles the RuntimeException in the catch block, and then the code returns to normal. B, C, D, E, and F are incorrect based on the program logic described above. Remember that properly handled exceptions do not cause the program to stop executing. 28. A is correct. Both assert statements are true so the bar( ) and foo( ) methods are not called. B, C, D, E, and F are incorrect based on the program logic described above. 29. C and D are correct. B is incorrect because it is not recommended for assertions to cause side effects, which setter methods would do. A is incorrect. One of the most common uses of assert statements in debugging is to verify that locations in code that have been designed to be unreachable are in fact never reached. E is incorrect. 30. D and F are correct. If a class's instance variables are marked private, the class can be said to be encapsulated. A, B, C, and E are incorrect based on the program logic described above. public getter and setter methods are compatible with the concept of encapsulation. 31. A is correct. B is incorrect because Wings does not implement Raptor. C is incorrect because Hawk has three instance variables, size, name, and w. D is incorrect because the Hawk hunt( ) method is already implemented. 32. B is correct. Reference variable 'b' is of type Bar, but it refers to an object of type Foo. The line b.m1() is a polymorphic call, and the VM will use the version of the m1() method that is in the class that the reference variable refers to at that point. A, C, D, and E are incorrect because of the logic described above. 33. B and D are correct. B is a simple override (protected is less restrictive than default). D is a simple override, the overriding method can be final. A is an illegal override because the private modifier is more restrictive than doStuff()'s default modifier in class Alpha. C and E are illegal overrides because you can't change an overridden method's return type, or C and E are illegal overloads because you must change an overloaded method's arguments. 34. D. Line D is the only line of code that 1) matches the constructor's argument list AND 2) uses proper inner class syntax. A, B, C, E, and F are incorrect based on the program logic described above. 35. E. The class elements declared in lines 5, 8, and 12 are badly named methods, not constructors. Line 3 is attempting to make a call to a constructor that doesn't exist (i.e. a constructor with a long argument.) A, B, C, D, and F are incorrect because of the logic described above. 36. B, E, and F are correct. B and E are overloads (taking floats as opposed to float arrays). . F is a valid override. A is not a method because it has no return type. C is wrong because it alters only the return type, and D has an access level that is too restrictive. 37. C is correct. Default access is more restrictive than protected. A and D are overloads, and B is an override. 38. A is correct. Line 7 creates a new String object with the value "bamm bamm" but this String has no reference variable, and the reference variable s3 still refers to the String with the value "bamm". B, C, D, and E are incorrect based on the logic described above. 39. C is correct. Lines 7 and 8 create new longer Strings but they have no reference variables so they are lost. A, B, D, E, and F are incorrect based on the logic described above. 40. B is different. Line 2 evaluates to 5. A, C, D, and E all evaluate to 4, (remember the int cast always loses everything after the decimal point). 41. E and F are roughly equal. The Math class's trigonometry methods expect their arguments to be in radians, not degrees. So, for instance, E can be decoded: "Convert 33 (degrees) into radians, then find the sine of that result." A, B, C, and D are incorrect based on the logic described above. 42. E is correct. In line 6 the valueOf( ) method returns a Double object, not a double primitive. A, B, C, D, and F are incorrect based on the logic described above. 43. C is correct. The valueOf() method on line 4 is expecting a String formatted as a long, with no decimal points, a NumberFormatException will be thrown. A, B, D, and E are incorrect based on the logic described above. 44. C is correct. Class Test2 didn't override equals() so the default implementation (==) is used. A, B, D, E, and E are incorrect based on the logic described above. 45. C is correct. Two different wrapper objects of the same class with the same value will cause equals( ) to be true and == to be false. A and E are incorrect based on the logic described above. B is incorrect because == is the strictest test of equality. D is incorrect because wrapper objects of different classes can never be equal. 46. C. By contract, if two objects are equivalent according to the equals() method, then their hashCode() method must evaluate them to be ==. A is incorrect because if the hashCode() values are equal, the two objects might not be equal. B is incorrect because if equals() yields true, the hashcodes will be equal. D is incorrect because if the hashCode()s are not equal the, equals() will be false. 47. D is correct. The equals() and hashCode() methods are joined at the hip when it comes to overriding. A, B, C, and E are incorrect based on the logic described above. 48. B is correct. Of the classes listed, only TreeSet and HashSet prohibit duplicate entries, but HashSet is not ordered. A, C, D, E, and F are incorrect based on the logic described above. 49. C is correct. LinkedList allows duplicate entries (which a stack might contain), and has methods appropriate for pushing or popping the top element in the stack (addFirst() and getFirst() ). A, B, D, and E are incorrect based on the logic described above. 50. C is correct. By the time line 6 has run, the only object with at least one reference is the object created in line 3. The object created in line 4 loses its only reference when the m1() method completes, and the object created in line 5 loses its only reference when line 6 executes. A, B, D, and E are incorrect based on the logic described above. 51. D. All objects are placed in the garbage collectible heap. A is incorrect because the garbage collector doesn't touch the stack. B is incorrect because no such promise can be guaranteed. C is incorrect because the garbage collector has no such mystical powers of prognostication. E is incorrect because a Java program might finish with many objects not garbage collectible. 52. A is correct. A copy of a reference to the line 13 object is passed to the doStuff2() method. The doStuff2() method abandons the line 13 object and creates a new object, returning a reference to the new object, which ultimately gets passed back to x on line 13. B, C, D, E, and F are incorrect based on the logic described above. 53. A and C are correct. These are both legal inner classes. B is incorrect because inner classes cannot have static methods. D is incorrect because it attempts to access instance variable x from a static inner class. 54. B and D are correct. B is correct because a static nested class is not tied to an instance of the enclosing class, and thus can't access the nonstatic members of the class (just as a static method can't access nonstatic members of a class). D is correct because non-final local variables can be removed from the stack while an instance of the anonymous inner class still exists. A is incorrect because Java syntax allows only a single interface declaration. C is incorrect because no such restriction exists. E is incorrect because there is no such restriction. 55. A is correct. It defines an anonymous inner class instance, which also means it creates an instance of that new anonymous class at the same time. The anonymous class is an implementer of the Runnable interface, so it must implement the run() method of Runnable. C is incorrect because it doesn't implement the run() method, so it violates the rules of interface implementation. B, D, and E use incorrect syntax. 56. A is correct. It makes an anonymous inner class from the method-local inner class Treat. B, C, D, and E are incorrect based on the logic described above. 57. A and D are correct. A is correct because a method local inner class can be abstract, although it means a subclass of the inner class must be created within the method, if the abstract class is to be used (so a method-local inner class is probably not useful). D is correct because a method-local inner class works like any other inner class-it has a special relationship to an instance of the enclosing class, thus it can access all members of the enclosing class. B is incorrect because method-local classes can't have access modifiers. C is incorrect because a method-local inner class can, optionally, be marked final. E is incorrect because no such restriction exists. 58. C is correct. This code is legal, but polymorphism does not apply to member variables. 'a' is a reference variable of type Animal so the Animal's size variable is used on line 9. A, B, D, E, and F are incorrect based on the program logic described above. 59. C is correct. Line 12 attempts to join the two threads, but the join() method throws InterruptedException which is not handled in the code. A, B, D, and E are incorrect based on the logic described above. 60. E is correct. notify() is a method of the Object class. A, B, C and D are all methods in the Thread class. 61. A is correct. The 'Stimpy' thread joins to the end of the 'Ren' thread and executes once the 'Ren' thread has completed. B, C, D, and E are incorrect based on the logic described above. 62. D and E. D is correct because a thread that calls notify() does not have to stop running. E is correct because entering an unlocked, synchronized block of code won't stop the currently executing thread. A, B and C all stop the currently running thread. 63. A is correct. Because of the join() method, it doesn't matter how slowly the 'Ren' thread executes, the 'Stimpy' thread will join to the end of 'Ren' and run after "Ren' has completed. B, C, D, E, and F are incorrect based on the logic described above. 64. C is correct. A and B are wrong because you lock an object, not a thread. D and E are wrong because a lock is not needed to call these methods. 65. C and E are correct. C is correct because the wait() method is overloaded to take one or two timeout arguments. E is correct because a thread that calls notify() does not have to stop running. A is incorrect because no such argument or capability exists. B is incorrect because the wait() method causes the currently executing thread to stop executing. D is incorrect because wait() only makes sense from a synchronized context.