Section 1: 1: Declarations, Initialization and Scoping - Objective 1.1 Test Question 1 Given the two source files: 1. package com.sun; 2. public class PkgAccess { 3. public static int tiger = 1414; 4. } And: 1. import static com.sun.PkgAccess.*; 2. 3. public class PkgAccess2 { 4. 5. int x1 = PkgAccess.tiger; 6. int x2 = tiger; 7. int x3 = com.sun.PkgAccess.tiger; 8. int x4 = sun.PkgAccess.tiger; 9. } Which two are true? (Choose two.) Answer: B and E are true. Explanation : When you say import static com.sun.PkgAccess.*; you are importing every static value of the class PkgAccess, so you can call that value using com.sun.PkgAccess.tiger // full path to access static "tiger" variable tiger // use directly the static variable (because we import it !)
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Section 1: 1: Declarations, Initialization and Scoping - Objective 1.1Test Question 1Given the two source files:
1. package com.sun;2. public class PkgAccess {3. public static int tiger = 1414;4. }
And:
1. import static com.sun.PkgAccess.*;2.3. public class PkgAccess2 {4.5. int x1 = PkgAccess.tiger;6. int x2 = tiger;7. int x3 = com.sun.PkgAccess.tiger;8. int x4 = sun.PkgAccess.tiger;9. }
Which two are true? (Choose two.)
Answer: B and E are true.
Explanation : When you say import static com.sun.PkgAccess.*; you are importing every static value of the class PkgAccess, so you can call that value using
com.sun.PkgAccess.tiger // full path to access static "tiger" variabletiger // use directly the static variable (because we import it !)
Section 1: 1: Declarations, Initialization and Scoping - Objective 1.1Test Question 2Given three different source files:
1. package com.sun2;2. public enum Seasons {SUMMER, FALL, WINTER, SPRING}
And:
1. import com.sun2.Seasons;2. import static com.sun2.Seasons.*;3. class Enum3a {4. Seasons s = FALL;5. }
And:
1. import com.sun2.*;2. import static com.sun2.Seasons.FALL;3. class Enum3b {4. Seasons s = FALL;5. }
Which is true?
Option A is correct. Both import styles are valid for enum types.
3. public class Buckets {4. public static void main(String[] args) {5. int[] x = {1,2,3};6. int[] y = {4,5,6};7. int[] z = {7,8,9};8. go(x, y, z);9. go(y, z);10. go(z);11. }12. static void go(int[] a, int[]... b) {13. System.out.print(a[1] + " ");14. }15. }
What is the result?
Option C is correct. It is legal to use var-args to pass arrays, and arrays use zero based indexes
1. class SuperFoo {2. SuperFoo doStuff(int x) {3. return new SuperFoo();4. }5. }6.7. class Foo extends SuperFoo {8. // insert code here9. }
Which three, inserted independently at line 8, will compile? (Choose three.)
Options A, C, and E are correct. Options B and D are incorrect because an override cannot change the return type unless it's a valid covariant return. Option C is a valid covariant return.
6. class Tack {7. static short s = 17;8. public Tack(short ss) {9. new Tack();10. s *= ss;11. }12. public Tack() { ; }13. }14. public class Bridle extends Tack {15. public Bridle(int s) { System.out.println(s + 1); }16. public static void main(String[] args) {17. Bridle b = new Bridle(3);18. }19. }
What is the result?
Option B is correct. The Tack constructor with one argument is never invoked in this code
Section 2: 2: Flow Control - Objective 2.1Test Question 10Given:
1. class Test4 {2. public static void main(String [] args) {3. boolean x = true;4. boolean y = false;5. short z = 42;6.7. if((z++ == 42) && (y = true)) z++;8. if((x = false) || (++z == 45)) z++;9.10. System.out.println("z = " + z);11. }12. }
What is the result?
Option D is correct. Line 7 does NOT test y, it sets it to true. Line 8 pre-increments z in the if test.
Section 2: 2: Flow Control - Objective 2.1Test Question 11Given:
1. enum Days {MONDAY, TUESDAY, WEDNESDAY, THURSDAY}2.3. class Test {4. public static void main(String [] args) {5. int x = 0;6. Days d = Days.TUESDAY;7. switch(d) {8. case MONDAY: x++;9. case TUESDAY: x = x + 10;10. case WEDNESDAY: x = x + 100;11. case THURSDAY: x = x + 1000;12. }13. System.out.println("x = " + x);14. }15. }
What is the result?
Option C is correct. This is the standard switch fall-through logic, and as of Java 1.5 enums can be used in a switch.
Section 2: 2: Flow Control - Objective 2.2Test Question 12Given:
23. int x = 7;24. switch (x) {25. case 8: System.out.print("8");26. case 7: System.out.print("7");27. case 6: System.out.print("6");28. default: System.out.print("def");29. case 9: System.out.print("9");30. }
What is the result?
Option C is correct. This is the standard switch fall-through logic, and as of Java 1.5 enums can be used in a switch.
Section 2: 2: Flow Control - Objective 2.2Test Question 13Given:
Option D is correct. It is legal to nest try/catches and normal flow rules apply.
Section 2: 2: Flow Control - Objective 2.6Test Question 19Given:
1. class Adder {2. static Short s1,s2;3. public static void main(String [] args) {4. int x;5. s1 = 4;6. x = s1 + s2;7. System.out.print(x);8. }9. }
What is the result?
Option D is correct. s2 is a reference to a wrapper that is never initialized. If s2 had been initialized, the autoboxing would be correct.
Section 2: 2: Flow Control - Objective 2.6Test Question 20Given:
1. class Calc {2. public static void main(String [] args) {3. try {4. int x = Integer.parseInt("42a");5. // insert code here6. System.out.print("oops ");7. }8. }9. }
Which two, inserted independently at line 5, cause the output to be "oops "? (Choose two.)
Options C and D are correct. NumberFormatException extends IllegalArgumentException.
Section 3: 3: API Contents - Objective 3.1Test Question 21Given:
1. class WideLoad {2. public static void main(String [] args) {3. float f = 3.14f;4. new WideLoad().doIt(f);5. }6. void doIt(Float f) {7. System.out.println("Float");8. }9. void doIt(double d) {10. System.out.println("double");11. }12. }
What is the result?
Option B is correct. The JVM will widen before it boxes, so the method on line 9 is invoked.
Section 3: 3: API Contents - Objective 3.1Test Question 22Given:
5. public class Ali {6. public static void main(String[] args) {7. // insert code here8. jab(y);9. }10. static void jab(Long x) {11. System.out.println("Long");12. }13. }
And the following four code fragments:
I. int y = 7;II. long y = 7;III. Long y = 7;IV. Integer y = 7;
Which code fragments, inserted independently at line 7, will compile?
Options A is correct. Fragment I would require both widening and boxing. Fragment IV would require widening from one wrapper type to another. Fragment III would compile if 7 had been 7L.
Section 3: 3: API Contents - Objective 3.2Test Question 23Which two are true about the java.io.Console class? (Choose two.)
Options A and D are correct. B, C, and E are true only for some of the class's methods.
Section 3: 3: API Contents - Objective 3.2Test Question 24Given:
- f is a reference to a valid java.io.File instance- fr is a reference to a valid java.io.FileReader instance- br is a reference to a valid java.io.BufferedReader instance
Which code, inserted at line 36, will loop through a text file and output a line at a time from the text field?
Option F is correct. Of the three classes, only BufferedReader has a readLine method, and read is for characters, not lines.
Section 3: 3: API Contents - Objective 3.3Test Question 25Which two about using the java.io.Serializable class are true? (Choose two.)
Options B and C are correct. A class's superclasses don't have to implement Serializable in order to be serialized, and if a superclass doesn't implement Serializable then it's constructor will run during deserialization. A transient variable's state is lost during serialization, but a volatile variable's state is not lost.
Section 3: 3: API Contents - Objective 3.4Test Question 26Given:
12. NumberFormat n = new NumberFormat();13. n.setMaximumFractionDigits(2);14. System.out.println((String) n.format(765.4321));
What is the result?
Option D is correct. Compilation fails because NumberFormat is abstract. You must use the getInstance method to get a NumberFormat instance.
Section 3: 3: API Contents - Objective 3.4Test Question 27Given that he is a valid language codeAnd given the following code fragments:
I. Date d = new Date();II. Calendar c = new Calender();III. Locale loc = new Locale("he");IV. DateFormat df = new DateFormat();
What is true?
Option D is correct. Calendar and DateFormat instances are created using getXxx() factory methods.
Section 3: 3: API Contents - Objective 3.5Test Question 28Which regex pattern finds both 0x4A and 0X5 from within a source file?
Option D is correct. The + quantifier finds 1 or more occurrences of hex characters after an 0x is found.
Section 3: 3: API Contents - Objective 3.5Test Question 29Given:
1. import java.util.*;2. class ScanStuff {3. public static void main(String [] args) {4. String s = "x,yy,123";5. Scanner sc = new Scanner(s);6. while (sc.hasNext())7. System.out.print(sc.next() + " ");8. }9. }
What is the result?
Option D is correct. Option C is incorrect because the default separator is a blank NOT a comma.
5. public class Pins implements Runnable {6. public static void main(String[] args) {7. Pins p = new Pins();8. Thread t1 = new Thread(p);9. Thread t2 = new Thread(p);10. t1.start();11. t2.start();12. t1.start();13. }14. public void run() { System.out.print("x"); }15. }
What is the result?
Option F is correct. Multiple threads can be created using the same Runnable object, but a given thread can be started only once.
3. public class Traffic implements Runnable {4. static String s = "";5. public void run() {6. for(int j = 0; j < 90000000; j++) {7. if((j % 10000000) == 0) Thread.yield(); // optional?8. if((j % 10000000) == 0) s += "2";9. } }10. public static void main(String[] args) {11. Thread t = new Thread(new Traffic()); t.start();12. for(int k = 0; k < 90000000; k++) if((k%10000000) == 0) s += "1";13. try { t.join(); } catch (Exception e) { }14. System.out.println(s);15. } }
What would be the most likely impact to the output if line 7 was removed?
Option F is correct. As the code stands the yield() will tend to give priority to the main thread, which will tend to place more "2" characters near the end of the output.
5. class NoGo implements Runnable {6. private static int i;7. public synchronized void run() {8. if (i%10 != 0) { i++; }9. for(int x=0; x<10; x++, i++)10. { if (x == 4) Thread.yield(); }11. }12. public static void main(String [] args) {13. NoGo n = new NoGo();14. for(int x=0; x<101; x++) {15. new Thread(n).start();16. System.out.print(i + " ");17. } } }
Which is true?
Option E is correct. To make sure that line 16 is never executed when an invocation of the run method is partially complete, it (line 16) should be placed inside the run method.
1. class Waiting implements Runnable { 2. boolean flag = false; 3. public synchronized void run() { 4. if (flag) { 5. flag = false; 6. System.out.print("1 "); 7. try { this.wait(); } catch (Exception e) { } 8. System.out.print("2 "); 9. }10. else {11. flag = true;12. System.out.print("3 ");13. try { Thread.sleep(2000); } catch (Exception e) { }14. System.out.print("4 ");15. notify();16. }17. }18. public static void main(String [] args) {19. Waiting w = new Waiting();20. new Thread(w).start();21. new Thread(w).start();22. }23. }
Which two are true? (Choose two.)
Options B and E are correct. Because run is synchronized, the first thread completes the entire else block before the second thread can enter run. The notify is issued before the wait, so the code hangs.
Section 5: 5: OO Concepts -Test Question 36
Which set of characteristics would be considered the most desirable in the design of an object oriented software program?
Option B is correct. Low coupling (indicating that classes know the least possible about each other), is preferable to tight coupling. High cohesion (indicating that a system's classes each have well focused responsibilities) is preferable to low cohesion.
4. public class Utility {5. public static void main(String[] args) {6. System.out.print(x + " ");7. new Utility().go(x);8. System.out.println(x);9. }10. private void go(int x) {11. x += 5;12. }13. int x = 7;14. }
What is the result?
Option D is correct. Instance variable x cannot be referenced from static method main. If x had been static, this would have been a case of variable shadowing.
2. class Cat {3. Cat(int c) { System.out.print("cat" + c + " "); }4. }5. class SubCat extends Cat {6. SubCat(int c) { super(5); System.out.print("cable "); }7. SubCat() { this(4); }8. public static void main(String [] args) {9. SubCat s = new SubCat();10. }11. }
What is the result?
Option D is correct. Type Cat does NOT need a no-argument constructer because SubCat's no-argument constructor calls SubCat's one argument constructor.
Section 5: 5: OO Concepts - Objective 5.5Test Question 44A programmer wants to develop an application in which Fizzlers are a kind of Whoosh, and Fizzlers also fulfill the contract of Oompahs. In addition, Whooshes are composed with several Wingits.
Which code represents this design?
Option B is correct. 'Kind of' translates to extends, 'contract' translates to implements, and 'composed' translates to a has-a implementation.
Which method, inserted at line 6, correctly implements java.util.Comparable?
Option B correctly implements the compareTo() method necessary to implement the java.util.Comparable interface.
Section 6: 6: Collections / Generics - Objective 7.5Test Question 46A programmer wants to work from a directory that contains:- A subdirectory named jarDir that contains a JAR file called MyJar.jar.- A file named Test.java that uses MyJar.jar.Which two will allow the programmer to compile the program? (Choose two.)
Options A and D are correct. These two approaches correctly match the javac command with the classpath information it needs.
3. public class Doctor {4. public static void main(String[] args) {5. String s = "";6. int x = 2;7. if((7 > (4*2)) ^ (5 != 4)) s += "1st ";8. if((5 < 7) ^ (7 > 5)) s += "2nd ";9. if(((4 * x++) < 9) ^ (x > 3)) s += "3rd ";10. System.out.println(s);11. }12. }
What is the result?
Option E is correct. The ^ operator returns true when exactly one operand is true.
Section 6: 6: Collections / Generics - Objective 6.2Test Question 49If a class X correctly overrides equals() and hashCode(), and if a and b are valid references to instances of class X, which two are true? (Choose two.)
Options A and C are correct. Option E is incorrect because the equals() method must take an Object.
2. class Marjoram {3. public static void main(String [] args) {4. Marjoram g = new Marjoram();5. g.go(1);6. }7. <A extends Alpha> Alpha go(int i) {8. if (i == 1) return new Alpha();9. else return new Beta();10. }11. }12. class Alpha { }13. class Beta extends Alpha { }
What is the result?
Option A is correct. Line 7 is valid syntax for a method with a generic return type.
4. import java.util.*;5. class Looking {6. public static void main(String[] args) {7. String[] sa = {"d", "c", "a", "b" };8. int x = Arrays.binarySearch(sa, "b");9. Arrays.sort(sa);10. int y = Arrays.binarySearch(sa, "b");11. System.out.println(x + " " + y);12. }13. }
Which two results are possible? (Choose two.)
Options B and E are correct. The results of searching an unsorted array are undefined, and sorted arrays have zero-based indexes. Note: Answer e is almost certainly the most common output that would be produced.
1. package x;2. public class X {3. // insert code here4. }7. package x;8. public class Find4 {9. public static void main(String [] args) {10. X myX = new X();11. myX.doX();12. }13. }
1. class Flibitz {2. public static void main(String [] args) {3. int grop = 7;4. new Flibitz().go(grop);5. System.out.print(grop);6. }7. void go(int grop) {8. if(++grop > 7) grop++;9. System.out.print(grop);10. }11. }
What is the result?
Option C is correct. The grop on line 7 is different from the grop on line 9.
Section 6: 6: Collections / Generics - Objective 7.4Test Question 60Which two are true? (Choose two.)
Options A and D are correct. An object can be collected even if it has a valid reference, if no live thread has access to the object; objects reside in the heap; and the finalize method can send an object reference to another object, thereby saving the otherwise doomed object.