k and b 3 vyas

43
LearnKey Study Material for: SCJP Sun® Certified Programmer for Java 5 Study Guide (Exam 310-055) Vyas Question 1: 33 Given: import java.util.regex.*; class Regex2 { public static void main(String[] args) { Pattern p = Pattern.compile(args[0]); Matcher m = p.matcher(args[1]); boolean b = false; while(b = m.find()) { System.out.print(m.start() + m.group() + " "); } } } And the command line: java Regex2 "\d\s\w" "12s 4 w 33 1" What is the result? Correct Answer: 44 w 93 1 References: EXPLANATION: A is correct. The \d is looking for digits, the \s is looking for whitespace, and the \w is looking for any "word" character. The start() method returns the starting position of a match, and the group() method returns the group that was matched, in this case two groups match: "4 w" and "3 1". B, C, D, E, and F are incorrect based on the above. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun® Certified Programmer for Java 5 Study Guide (Exam 310- 055)". New York, Osborne/McGraw-Hill, 2006. Chapter 6: Parsing OBJECTIVE: Parse Strings (Objective 3.5) Question 2: 19 Given: 1. public class Test { 2. public static void throwex () {

Upload: api-19982339

Post on 18-Nov-2014

652 views

Category:

Documents


0 download

TRANSCRIPT

LearnKeyStudy Material for: Vyas SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) Question 1: 33 Given: import java.util.regex.*; class Regex2 { public static void main(String[] args) { Pattern p = Pattern.compile(args[0]); Matcher m = p.matcher(args[1]); boolean b = false; while(b = m.find()) { System.out.print(m.start() + m.group() + " "); } } } And the command line: java Regex2 "\d\s\w" "12s 4 w 33 1" What is the result? Correct Answer: 44 w 93 1 References: EXPLANATION: A is correct. The \d is looking for digits, the \s is looking for whitespace, and the \w is looking for any "word" character. The start() method returns the starting position of a match, and the group() method returns the group that was matched, in this case two groups match: "4 w" and "3 1". B, C, D, E, and F are incorrect based on the above. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 6: Parsing OBJECTIVE: Parse Strings (Objective 3.5)

Question 2: 19 Given: 1. public class Test { 2. public static void throwex () {

3. System.out.print("throwex "); 4. throw new RuntimeException(); 5. } 6. public static void main(String [] args) { 7. try { 8. System.out.print("try "); 9. throwex(); 10. } 11. catch (Exception re ) { 12. System.out.print("catch "); 13. } 14. finally { 15. System.out.print("finally "); 16. } 17. System.out.println("done "); 18. } 19. } What is the result? Correct Answer: try throwex catch finally done References: EXPLANATION: D is correct. The main() method properly catches and handles the RuntimeException in the catch block, then finally runs (as it always does), and then the code returns to normal. A, B, C, 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. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 5: Handling Exceptions OBJECTIVE: Exceptions (Objective 2.4)

Question 3: 9 Given: 1. class Plant { 2. String getName() { return "plant"; } 3. Plant getType() { return this; } 4. } 5. class Flower extends Plant { 6. // insert code here 7. } 8. class Tulip extends Plant { } Which statement(s), inserted independently at line 6, would compile? (Choose all that apply.) Correct Answer: Flower getType() { return this; } Plant getType() { return this; } Tulip getType() { return new Tulip(); }

References: EXPLANATION: A, C, and D are correct. A and D are examples of co-variant returns. Flower and Tulip are both subtypes of Plant. B is incorrect. String is not a subtype of Plant. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 2: Covariant Returns OBJECTIVE: Legal Return Types (Objective 1.5)

Question 4: 8 Given: class Tarsier { static String s = "-"; public static void main(String[] args) { go(); System.out.println(s); } void go() { s+= "s"; } { go(); } static { go(); } static void go() { s+= "s"; } } What is the result? Correct Answer: Compilation fails. References: EXPLANATION: F is correct. The go() method is declared twice (with the same signature)-once as a static method, and once as an instance method. The two init blocks are legal. A, B, C, D, and E are incorrect based on the preceding explanation. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapters 1 and 3: Static Members and Initialization Blocks OBJECTIVE: Objectives 1.3 and 1.4

Question 5: 39 Given: public class Alphabet extends Thread { public synchronized void run() { try { System.out.println("A"); wait(); System.out.println("B"); notify(); System.out.println("C");

wait(1); System.out.println("D"); notifyAll(); System.out.println("E"); } catch (Exception e) { System.out.println("F"); } finally { System.out.println("G"); } } public static void main(String[] args) { new Alphabet().start(); } } Which letters will appear in the output? (Choose all that apply.) Correct Answer: A References: EXPLANATION: A is correct. Since run() is synchronized, a lock has been acquired on the "this" instance, so none of the method calls would throw an IllegalStateException. However, the first call to wait() blocks forever. It's waiting for a notify(), but the notify() is never called because it's blocked on the wait(). In order for a notify() to be useful, it must be sent from a separate thread so that it can be called while the first thread is still waiting at the wait() statement. B, C, D, E, F, G, and H are incorrect based on the preceding explanation. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 9: Synchronizing Code OBJECTIVE: Use Locking (Objective 4.3)

Question 6: 27 Which of the following can be constructed using a String? (Choose all that apply.) Correct Answer: java.io.File java.io.FileWriter java.io.PrintWriter References: EXPLANATION: A, B, and D are correct. C and E are incorrect. These classes don't have constructors that can take a String. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 6: File I/O OBJECTIVE: Navigate File Systems (Objective 3.2)

Question 7: 67 Which are true? (Choose all that apply.) Correct Answer: The -classpath command-line argument can be used with both the java and javac commands. When using the java command, only files ending in .class can be executed. References: EXPLANATION: C and F are correct. A is incorrect because -d is used to specify a directory that is usually different than the directory in which the .java file resides. B is incorrect because this definition applies to the -d argument, not the -D argument. D is incorrect. E is incorrect because only one file can be executed. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 10: Using java OBJECTIVE: Command-line arguments (Objective 7.2)

Question 8: 74 Given: class Silky extends Smooth { int x = 5; int y = 7; public static void main(String[] args) { new Silky().go(); } void go() { if(x > y & (Boolean)(this instanceof Silky)) System.out.print("a"); if(Long.valueOf(4) instanceof Number) System.out.print("b"); } } class Smooth { } Which is the result? Correct Answer: b References: EXPLANATION: B is correct. The first if test explicitly casts the instanceof call to a Boolean, and then the compiler boxes it back to a boolean. The second if test is true because the valueOf() method returns a Long, which extends java.lang.Number. A, C, D, and E are incorrect based on the preceding. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 4: instanceof

OBJECTIVE: Apply Operators (Objective 7.6)

Question 9: 31 Which classes can be instantiated using a Locale? (Choose all that apply.) Correct Answer: java.util.Calendar java.text.DateFormat java.text.NumberFormat References: EXPLANATION: B, C, and D are correct. A is incorrect. Date instances are not affected by locales or time zones. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 6: Dates OBJECTIVE: Use Dates and Numbers (Objective 3.4)

Question 10: 6 Which will declare an array and initialize it with four numbers? (Choose all that apply.) Correct Answer: int x [] = {1,0,2,0}; int array [] = new int [4]; References: EXPLANATION: A and D are correct. Both are legal ways to declare and initialize an array with four elements. B, C, E, and F are invalid attempts to declare and initialize an array. None of them will compile. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 1: Legal and Illegal Array Assignments OBJECTIVE: Array Construction and Initialization (Objective 1.3)

Question 11: 61 Given: class Food { } class Fruit extends Food { } class Apple extends Fruit { } // insert code here public static void main(String[] munch) { Pie p = new Pie(); }

} Which inserted at // insert code here, will compile? (Choose all that apply.) Correct Answer: class Pie { class Pie { class Pie { References: EXPLANATION: A, B, and F are correct. C and D are incorrect because Fruit is not "in bounds" as a type of Apple or Pie when you try to instantiate a new Pie. E is incorrect syntax. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 7: Generic Types OBJECTIVE: Write Generic Classes (Objective 6.4)

Question 12: 59 Given: import java.util.*; public class PQ { public static void main(String[] args) { Comparator cmp = new Comparator() { public int compare(Integer n1, Integer n2) { return n2.compareTo(n1); } }; PriorityQueue q = new PriorityQueue(100, cmp); q.addAll(Arrays.asList(16, 8, 42, 15, 4, 23)); System.out.println(q.poll()); } } What is the result? Correct Answer: 42 References: EXPLANATION: D is correct. The 100 is just the initial capacity of the queue-it's not a number that's in the queue. Normally, a PriorityQueue would sort in natural order (increasing) and poll() would remove the first (lowest) number. But the Comparator cmp effectively reverses the natural order. So the largest number (42) is removed and printed. A, B, C, E, F, and G are incorrect based on the preceding statement. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 7: Sorting

OBJECTIVE: Write Code to Sort and Search (Objective 6.5)

Question 13: 70 Given: class Finalizer { static int x = 0; static boolean gc = false; public static void main(String[] args) { for(x=0; x list) {list.size(); } References: EXPLANATION: B, D, and F are correct. A and C are incorrect because a supertype object (in this case of type Object) can't be inserted into a List of Apples. E is incorrect because the add() method isn't compatible with the ? syntax. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 7: Generic Types OBJECTIVE: Write Generic Methods (Objective 6.4)

Question 38: 1 Which are correct source files? (Choose all that apply.) Correct Answer: package mystuff; import java.sql.*; import yourstuff.HandyClass; class MyGreatClass { } References: EXPLANATION: A is correct. B is incorrect because package must be first, before imports and classes. C is incorrect because all imports must precede all classes. D is incorrect because imports must be declared outside of classes. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 1: Source File Rules OBJECTIVE: Declare Classes (Objective 1.1) import java.util.*;

Question 39: 46 Given: class Animal { } class Gerbil extends Animal { } class Vet { Animal go() { return new Animal(); } } class SmallAnimalVet extends Vet { // Gerbil go() { return new Gerbil(); } // 7 // Animal go() { return new Animal(); } // 8 // Gerbil go() { return new Animal(); } // 9 // Animal go() { return new Gerbil(); } // 10 } Which are true? (Choose all that apply.) Correct Answer: If uncommented, line 7 will compile. If uncommented, line 8 will compile. If uncommented, line 10 will compile. Only one of the lines from 7 to 10 can be uncommented at a time and still compile. References: EXPLANATION: A, B, D, and F are correct. Line 7 is a covariant return. C is incorrect because Animal instances can't necessarily do everything that Gerbil instances can do. E is incorrect because that would create illegal overloads. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 2: Return Types OBJECTIVE: Polymorphism (Objective 5.2)

Question 40: 24 Given: class Under extends Mid { static String s = " "; public static void main(String[] args) { Upper u = new Under(); s = u.go(); Mid m = (Mid)u; System.out.println(s += m.go()); } } class Upper { String go() { return "hi "; } } class Mid extends Upper { } What is the result? Correct Answer: hi hi

References: EXPLANATION: B is correct. The downcast is correct, and the argument to the println() method is correct. A, C, D, E, F, G, and H are incorrect based on the previous statement. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 5: Common Exceptions OBJECTIVE: Recognize Common Exceptions (Objective 2.6)

Question 41: 16 Given: 11. int I = 0; 12. outer: 13. while (true) { 14. I++; 15. inner: 16. for (int j = 0; j < 10; j++) { 17. I += j; 18. if (j == 3) 19. continue inner; 20. break outer; 21. } 22. continue outer; 23. } 24. System.out.println(I); 25. What is the result? Correct Answer: 1 References: EXPLANATION: A is correct. The program flows as follows: I will be incremented after the while loop is entered, and then I will be incremented (by zero) when the for loop is entered. The if statement evaluates to false, and the continue statement is never reached. The break statement tells the JVM to break out of the outer loop, at which point I is printed and the fragment is done. B, C, D, E, and F are incorrect based on the program logic described. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 5: break and continue OBJECTIVE: Develop Loops (Objective 2.2)

Question 42: 55 Given: import java.util.*; class Nearly {

String value; Nearly(String v) { value = v; } public int hashCode() { return 1; } public boolean equals(Nearly n) { if(value.charAt(0) == n.value.charAt(0)) return true; return false; } public static void main(String[] sss) { Nearly n1 = new Nearly("aaa"); Nearly n2 = new Nearly("aaa"); String s = "-"; if(n1.equals(n2)) s += "1"; if(n1 == n2) s += "2"; Set set = new HashSet(); set.add(n1); set.add(n2); System.out.println(s + " " + set.size()); } } What is the result? Correct Answer: -1 2 References: EXPLANATION: B is correct. Two different equals() methods are invoked, because the equals() method shown in the code doesn't properly override Object.equals(), which takes an Object. A, C, D, E, and F are incorrect based on the preceding statement. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 7: Overriding equals() OBJECTIVE: Override equals() (Objective 6.2)

Question 43: 21 Given: 1. import java.io.*; 2. public class Test { 3. public static void main(String [] args){ 4. FileInputStream in = null; 5. try { 6. in = new FileInputStream("test.txt"); 7. int x = in.read(); 8. } 9. catch(IOException io) { 10. System.out.println("IO Error."); 11. } 12. finally { 13. in.close(); 14. } 15. }

16. } And given that all methods of class FileInputStream throw an IOException, which of the following is true? (Choose one.) Correct Answer: This program fails to compile due to an error at line 13. References: EXPLANATION: E is correct. Any method (in this case, the main() method) that throws a checked exception (in this case, in.close()) must be called within a try clause, or the method must declare that it throws the exception. Either main() must declare that it throws an exception, or the call to in.close() in the finally block must fall inside a (in this case nested) try-catch block. A, B, C, and D are incorrect based on the program logic described above. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 5: Handling Exceptions OBJECTIVE: Exceptions (Objective 2.5)

Question 44: 11 Given: 1. class MySuper { 2. public MySuper(int i) { 3. System.out.println("super " + i); 4. } 5. } 6. 7. public class MySub extends MySuper { 8. public MySub() { 9. super(2); 10. System.out.println("sub"); 11. } 12. 13. public static void main(String [] args) { 14. MySuper sup = new MySub(); 15. } 16. } What is the result? Correct Answer: super 2 References: EXPLANATION: B is correct. Class MySuper does not need a no-args constructor because MySub explicitly calls the MySuper constructor with an argument. A is incorrect because other than the implicit calls to super(), constructors run in order from base class to extended class, so MySuper's output will print first. C, D, E, and F are incorrect because the code compiles properly. sub

REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 2: Constructors OBJECTIVE: Develop Constructors (Objective 1.6)

Question 45: 10 Given: 1. 2. 3. 4. 5. 6. 7. 8. public class Felix { protected long cat() { return 7L; } } class Oscar extends Felix { // insert code here }

Which method(s), inserted at line 7, will not compile? (Choose all that apply.) Correct Answer: long cat() { return 8; } References: EXPLANATION: C is correct. Default access is more restrictive than protected. A and D are overloads, and B is an override. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 2: Return Types OBJECTIVE: Return Types (Objective 1.5)

Question 46: 15 Given: 1. public class Test { 2. public static void main(String [] args) { 3. int I = 1; 4. do while ( I < 1 ) 5. System.out.print("I is " + I); 6. while ( I > 1 ) ; 7. } 8. } What is the result? Correct Answer: No output is produced.

References: EXPLANATION: C is correct. There are two different looping constructs in this problem. The first is a do-while loop, and the second is a while loop, nested inside the do-while. The body of the do-while is only a single statementbrackets are not needed. You are assured that the while expression will be evaluated at least once, followed by an evaluation of the do-while expression. Both expressions are false and no output is produced. A, B, D, and E are incorrect based on the program logic described above. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 5: while and do Loops OBJECTIVE: Develop Loops (Objective 2.2)

Question 47: 29 Given a class X with an instance variable: transient int readCount; If you want to serialize and deserialize an instance of class X, AND retain readCount's value through the serialization and deserialization process, what is true about the method you must implement on the deserialization side of the process? (Choose all that apply.) Correct Answer: It must be marked private void. If takes an object of type ObjectInputStream. References: EXPLANATION: B and F are correct. The method you must implement to do custom deserialization is: private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException. A, C, D, and E are incorrect based on the previous statement. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 6: Serialization OBJECTIVE: Serialization (Objective 3.3)

Question 48: 42 Which statements are true? (Choose all that apply.) Correct Answer: The wait() method can take a long argument. The notify() method does not release an object's lock. References: EXPLANATION: 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. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 9: Preventing Execution OBJECTIVE: Use wait and notify (Objective 4.4)

Question 49: 17 Given: 1. public class Test { 2. public static int y; 3. public static int foo(int x) { 4. System.out.print("foo "); 5. return y = x; 6. } 7. public static int bar(int z) { 8. System.out.print("bar "); 9. return y = z; 10. } 11. public static void main(String [] args ) { 12. int t = 2; 13. assert t < 4 : bar(7); 14. assert t > 1 : foo(8); 15. System.out.println("done "); 16. } 17. } What is the result? Correct Answer: done References: EXPLANATION: 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 in the preceding statement. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 5: Working with Assertions OBJECTIVE: Assertions (Objective 2.3)

Question 50: 34 Which are true about format string syntax? (Choose all that apply.) Correct Answer: %c formats chars

%f formats floats References: EXPLANATION: B and D are correct. A is incorrect because %b formats booleans. C is incorrect because %d formats decimal integers. E is incorrect because %i isn't defined. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 6: Formatting OBJECTIVE: Formatting Parameters (Objective 3.5)

Question 51: 71 Given: class History { static Phoenix p2; static void send(Phoenix p) { p2 = p; } public static void main(String[] args) { new Phoenix().finalize(); new Phoenix().finalize(); Phoenix p3 = new Phoenix(); // do time consuming and memory intensive stuff } } class Phoenix { protected void finalize() { History.send(this); } } While the code represented by "// do time consuming and memory intensive stuff" is executing, which are true concerning the three Phoenix objects we know have been created? (Choose all that apply.) Correct Answer: At least one Phoenix object is eligible for garbage collection. The JVM could have invoked the finalize() method at least once. References: EXPLANATION: B and D are correct. The first Phoenix object created is the only Phoenix object eligible for garbage collection. Even though finalize() was invoked explicitly for that object, the JVM can invoke finalize() one more time for that object. A, C, E, F, and G are incorrect based on the preceding. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 3: Garbage Collection OBJECTIVE: Garbage Collection Eligibility (Objective 7.4)

Question 52: 12 Given: 1. public class ThreeConst { 2. public static void main(String [] args) { 3. new ThreeConst(4L); 4. } 5. public ThreeConst(int x) { 6. this(); 7. System.out.print(" " + (x * 2)); 8. } 9. public ThreeConst(long x) { 10. this((int) x); 11. System.out.print(" " + x); 12. } 13. 14. public ThreeConst() { 15. System.out.print("no-arg "); 16. } 17. } What is the result? Correct Answer: no-arg 8 4 References: EXPLANATION: E is correct. The main() method calls the long constructor, which calls the int constructor, which then calls the no-arg constructor, which runs and then returns to the int constructor, which runs and then returns to the long constructor, which runs last. A, B, C, D, and F are incorrect based on the program logic described above. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 2: Constructors OBJECTIVE: Develop Constructors (Objective 1.6)

Question 53: 47 Given: class Car { public static void m1() { System.out.print("a "); } public void m2() { System.out.print("b "); } } class Mini extends Car { public static void m1() { System.out.print("c "); } public void m2() { System.out.print("d "); } public static void main(String[] args) { Car c = new Mini(); c.m1(); c.m2(); } } What is the result?

Correct Answer: ad References: EXPLANATION: B is correct. Remember that polymorphism applies only to instance methods, not to static methods. A, C, D, E, and F are incorrect based on the preceding explanation. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 2: Polymorphism and Statics OBJECTIVE: Static Methods (Objective 5.3)

Question 54: 5 Given: class Bulbs { enum Turn { ON("bright"), OFF("dark"); } public static void main(String[] args) { System.out.println(Turn.ON); } } What is the result? Correct Answer: Compilation fails. References: EXPLANATION: C is correct. The enum constants are declared with a String argument, which means that an enum constructor that takes a String must be supplied. A, B, D, and E are incorrect based on the preceding explanation. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 3: Enumerations OBJECTIVE: Declarations (Objective 1.3)

Question 55: 36 Given: 1. public class MyRunnable implements Runnable { 2. public void run() { 3. System.out.println("running..."); 4. } 5. public static void main(String[] args) { 6. // insert code here 7. }

8. } Which of these, inserted independently at line 6, will create and start a new thread? (Choose all that apply.) Correct Answer: new Thread(new MyRunnable()).start(); References: EXPLANATION: C is correct. Because the class implements Runnable, an instance of it has to be passed to the Thread constructor, and then the instance of the Thread has to be started. A is incorrect. There is no constructor like this for Runnable because Runnable is an interface, and it is illegal to pass a class or interface name to any constructor. B is incorrect for the same reason: you can't pass a class or interface name to any constructor. D is incorrect because MyRunnable doesn't have a start() method, and the only start() method that can start a thread of execution is the start() in the Thread class. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 9: Starting a Thread OBJECTIVE: Start Threads (Objective 4.1)

Question 56: 18 Which are true? (Choose all that apply.) Correct Answer: Use assertions to verify the arguments of private methods. Assertions can be disabled for a particular class. References: EXPLANATION: C and D are correct. 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. B is incorrect because it is not recommended for assertions to cause side effects, which setter methods would do. E is also incorrect. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 5: Working with Assertions OBJECTIVE: Assertions (Objective 2.3)

Question 57: 44 Given a class House, which are true? (Choose all that apply.) Correct Answer: A turnOnStereo() method would NOT be considered cohesive. House has-a Door would be considered low coupling.

References: EXPLANATION: B and D are correct. A turnOnStereo() method would probably be associated with a Stereo class. A is incorrect because a House almost certainly will have a Door, so this is not considered tight coupling. C describes inheritance not encapsulation. E is incorrect because getAddress() would probably represent good cohesion. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 2: Encapsulation, Coupling, and Cohesion OBJECTIVE: Encapsulation, Coupling, and Cohesion (Objective 5.1)

Question 58: 13 Given: public class OrtegusFunction { public int computeDiscontinuous(int x) { int r = 1; r += x; if ((x > 4) && (x < 10)) { r += 2 * x; } else if (x = 0; x--) { 6. switch (x) { 7. case 3: y = y + 100; 8. case 2: y = y + 10; 9. case 1: break; 10. case 0: y = y + 1; 11. }

12. } 13. System.out.println(y); 14. } 15. } What is the result? Correct Answer: 121 References: EXPLANATION: 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. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 5: switch Statements OBJECTIVE: Develop switch Statements (Objective 2.1)

Question 68: 40 Which are true? Correct Answer: A thread must own the lock on the object whose wait() method is to be called. References: EXPLANATION: 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. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 9: wait notify OBJECTIVE: Threads (Objective 4.3)

Question 69: 62 Given: import java.util.*; class Metric { public static void main(String[] args) { String[] s = {"inch", "foot", "yard", "rod", "meter"}; List list = Arrays.asList(s); list.set(4, "chain"); System.out.print(s[4]); s[2] = "meter";

System.out.print(" " + list.get(2)); } } What is the result? Correct Answer: chain meter References: EXPLANATION: C is correct. The asList() method copies an array to a List. Subsequent changes (not additions) to either the List or the Array will be reflected in the other. A, B, D, E, and F are incorrect based on the preceding explanation. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 7: Using Collections OBJECTIVE: Convert an array (Objective 6.5)

Question 70: 51 If class Mulder is-a Scully, and has-a Skinner, which declarations are correct? Correct Answer: class Mulder extends Scully { Skinner s = new Skinner(); } References: EXPLANATION: D is correct. A, B, and C are incorrect because (among other things) has-a relationships are not associated with inheritance. E mixes up the has-a and is-a relationships. F is incorrect because the syntax specifies the bounds of the Mulder class's type parameter, not an inheritance relationship between Mulder and Scully. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 2: Has-A OBJECTIVE: Develop is-a and has-a (Objective 5.5)

Question 71: 43 Which are true? (Choose all that apply.) Correct Answer: Encapsulation limits the consequences of change. Encapsulation allows corrections to a class with minimal impact on users of that class. Encapsulation makes it easier to reuse classes. Encapsulation results in better testing and higher reliability.

References: EXPLANATION: A, B, C, and D are correct statements. E is incorrect because encapsulation ensures that only the appropriate member variables are accessible, and those only through getters and setters. F is incorrect because while several public methods will usually exist in an encapsulated class, but it is not necessary for all of them to be public. G is incorrect because while packaging can provide a layer of encapsulation, that layer is on a larger scale than encapsulation in individual classes. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 2: Encapsulation OBJECTIVE: Implement Encapsulation (Objective 5.1)

Question 72: 4 Given: 1. interface Face1 { 2. int m1(int a1); 3. } 4. interface Face2 { 5. int m2(int a2); 6. } 7. 8. Which code fragments inserted at lines 7 and 8 will compile? (Choose all that apply.) Correct Answer: class Concrete2 implements Face2 { public int m2(int l) { return 7; } } abstract class Concrete2 implements Face2, Face1 { public int m4() { return 7; } } References: EXPLANATION: C and E are correct. C is a valid (if brief) implementation. 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. D is incorrect because the method being implemented is from the wrong interface. REFERENCES: See Bates, Bert and Kathy Sierra. " SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310055)". New York, Osborne/McGraw-Hill, 2006. Chapter 2: Declaring an Interface OBJECTIVE: Declarations (Objective 1.2)