Sunday, December 30, 2012

EXAM H


QUESTION 1

Given:

1. public class Target {
2. private int i = 0;
3. public int addOne() {
4. return ++i;
5. }
6. }

And:

1. public class Client {
2. public static void main(String[] args){
3. System.out.println(new Target().addOne());
4. }
5. }

Which change can you make to Target without affecting Client?

A. Line 4 of class Target can be changed to return i++;
B. Line 2 of class Target can be changed to private int i = 1;
C. Line 3 of class Target can be changed to private int addOne(){
D. Line 2 of class Target can be changed to private Integer i = 0;

Answer: D
Section: All

Explanation/Reference:

QUESTION 2

Given:

class Animal {
     public String noise() {
          return "peep";
     }
}
class Dog extends Animal {
     public String noise() {
          return "bark";
     }
}
class Cat extends Animal {
     public String noise() {
          return "meow";
     }
}
...
30. Animal animal = new Dog();
31. Cat cat = (Cat)animal;
32. System.out.println(cat.noise());

What is the result?

A. peep
B. bark
C. meow
D. Compilation fails.
E. An exception is thrown at runtime.

Answer: E
Section: All

Explanation/Reference:
 Exception in thread "main" java.lang.ClassCastException: Dog cannot be cast to
Cat at Client.main(Client.java:12)
   

QUESTION 3

Given:

abstract class A {
     abstract void a1();
   
     void a2() {
     }
}
class B extends A {
     void a1() {
     }
   
     void a2() {
     }
}
class C extends B {
     void c1() {
     }
}

And:

A x = new B();
C y = new C();
A z = new C();

What are four valid examples of polymorphic method calls? (Choose four.)

A. x.a2();
B. z.a2();
C. z.c1();
D. z.a1();
E. y.c1();
F. x.a1();

Answer: ABDF
Section: All

Explanation/Reference:

QUESTION 4

Given:

class Employee {
     String name;
     double baseSalary;
   
     Employee(String name, double baseSalary) {
          this.name = name;
          this.baseSalary = baseSalary;
     }
}

public class SalesPerson extends Employee {
     double commission;
   
     public SalesPerson(String name, double baseSalary, double commission) {
          // insert code here Line 13
     }
}

Which two code fragments, inserted independently at line 13, will compile? (Choose two.)

A. super(name, baseSalary);
B. this.commission = commission;
C. super(); this.commission = commission;
D. this.commission = commission; super();
E. super(name, baseSalary); this.commission = commission;
F. this.commission = commission; super(name, baseSalary);
G. super(name, baseSalary, commission);

Answer: AE
Section: All

Explanation/Reference:

QUESTION 5

A team of programmers is involved in reviewing a proposed design for a new utility class.
After some discussion, they realize that the current design allows other classes to access
methods in the utility class that should be accessible only to methods within the utility class itself.

What design issue has the team discovered?

A. Tight coupling
B. Low cohesion
C. High cohesion
D. Loose coupling
E. Weak encapsulation
F. Strong encapsulation

Answer: E
Section: All

Explanation/Reference:

QUESTION 6

Given that:

Gadget has-a Sprocket and Gadget has-a Spring and Gadget is-a Widget and Widget has-a Sprocket
Which two code fragments represent these relationships? (Choose two.)

A. class Widget {
        Sprocket s;
   }
 
   class Gadget extends Widget {
        Spring s;
   }
B. class Widget {
   }
 
   class Gadget extends Widget {
        Spring s1;
        Sprocket s2;
   }
C. class Widget {
        Sprocket s1;
        Spring s2;
   }
 
   class Gadget extends Widget {
   }
D. class Gadget {
        Spring s;
   }
 
   class Widget extends Gadget {
        Sprocket s;
   }
E. class Gadget {
   }
 
   class Widget extends Gadget {
        Sprocket s1;
        Spring s2;
   }
F. class Gadget {
        Spring s1;
        Sprocket s2;
   }
 
   class Widget extends Gadget {
   }
 
Answer: AC
Section: All

Explanation/Reference:



QUESTION 7

Given:

class Pizza {
     java.util.ArrayList toppings;
   
     public final void addTopping(String topping) {
          toppings.add(topping);
     }
     public void removeTopping(String topping) {
          toppings.remove(topping);
     }
}
public class PepperoniPizza extends Pizza {
     public void addTopping(String topping) {
          System.out.println("Cannot add Toppings");
     }   
     public static void main(String[] args) {
          Pizza pizza = new PepperoniPizza();
          pizza.addTopping("Mushrooms");
          pizza.removeTopping("Peperoni");
     }   
}

What is the result?

A. Compilation fails.
B. Cannot add Toppings
C. The code runs with no output.
D. A NullPointerException is thrown in Line 4.

Answer: A
Section: All

Explanation/Reference:

QUESTION 8

Which three statements are true? (Choose three.)

A. A final method in class X can be abstract if and only if X is abstract.
B. A protected method in class X can be overridden by any subclass of X.
C. A private static method can be called only within other static methods in class X.
D. A non-static public final method in class X can be overridden in any subclass of X.
E. A public static method in class X can be called by a subclass of X without explicitly referencing the class X.
F. A method with the same signature as a private final method in class X can be implemented in a
   subclass of X.
G. A protected method in class X can be overridden by a subclass of X only if the subclass is in the same package as X.
 
Answer: BEF
Section: All

Explanation/Reference:

QUESTION 9

Click the Exhibit button.

1. public class Car {
2. private int wheelCount;
3. private String vin;
4. public Car(String vin){
5. this.vin = vin;
6. this.wheelCount = 4;
7. }
8. public String drive(){
9. return "zoom-zoom";
10. }
11. public String getInfo() {
12. return "VIN: " + vin + " wheels: " + wheelCount;
13. }
14. }

And

1. public class MeGo extends Car {
2. public MeGo(String vin) {
3. this.wheelCount = 3;
4. }
5. }

What two must the programmer do to correct the compilation errors? (Choose two.)

A. insert a call to this() in the Car constructor
B. insert a call to this() in the MeGo constructor
C. insert a call to super() in the MeGo constructor
D. insert a call to super(vin) in the MeGo constructor
E. change the wheelCount variable in Car to protected
F. change line 3 in the MeGo class to super.wheelCount = 3;

Answer: DE
Section: All

Explanation/Reference:

QUESTION 10

Click the Exhibit button.

1. import java.util.*;
2. public class TestSet{
3. enum Example {ONE, TWO, THREE }
4. public static void main(String[] args) {
5. Collection coll = new ArrayList();
6. coll.add(Example.THREE);
7. coll.add(Example.THREE);
8. coll.add(Example.THREE);
9. coll.add(Example.TWO);
10. coll.add(Example.TWO);
11. coll.add(Example.ONE);
12. Set set = new HashSet(coll);
13. }
14. }

Which statement is true about the set variable on line 12?

A. The set variable contains all six elements from the coll collection, and the order is guaranteed to be preserved.
B. The set variable contains only three elements from the coll collection, and the order is guaranteed to be preserved.
C. The set variable contains all six elements from the coll collection, but the order is NOT guaranteed to be preserved.
D. The set variable contains only three elements from the coll collection, but the order is NOT guaranteed to be preserved.
 
Answer: D
Section: All

Explanation/Reference:

QUESTION 11

Given:

public class Person {
     private String name, comment;
     private int age;
   
     public Person(String n, int a, String c) {
          name = n;
          age = a;
          comment = c;
     }
   
     public boolean equals(Object o) {
          if (!(o instanceof Person))
               return false;
          Person p = (Person) o;
          return age == p.age && name.equals(p.name);
     }
}

What is the appropriate definition of the hashCode method in class Person?

A. return super.hashCode();
B. return name.hashCode() + age * 7;
C. return name.hashCode() + comment.hashCode() / 2;
D. return name.hashCode() + comment.hashCode() / 2 - age * 3;

Answer: B
Section: All

Explanation/Reference:

QUESTION 12

Given:

public class Key {
     private long id1;
     private long id2;   
     // class Key methods
}
A programmer is developing a class Key, that will be used as a key in a standard java.util.HashMap.
Which two methods should be overridden to assure that Key works correctly as a key? (Choose two.)

A. public int hashCode()
B. public boolean equals(Key k)
C. public int compareTo(Object o)
D. public boolean equals(Object o)
E. public boolean compareTo(Key k)

Answer: AD
Section: All

Explanation/Reference:

QUESTION 13

Given:
import java.util.*;
public class Hancock {
          // insert code here Linea 5
          list.add("foo");
     }
}

Which two code fragments, inserted independently at line 5, will compile without warnings? (Choose two.)

A. public void addStrings(List list) {
B. public void addStrings(List<String> list) {
C. public void addStrings(List<? super String> list) {
D. public void addStrings(List<? extends String> list) { B,C

Answer: BC
Section: All

Explanation/Reference:

QUESTION 14

A programmer has an algorithm that requires a java.util.List that provides an efficient implementation of add (0, object), but does NOT need to support quick random access.
What supports these requirements?

A. java.util.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList

Answer: D
Section: All

Explanation/Reference:


QUESTION 15

Given a class whose instances, when found in a collection of objects, are sorted by using the compareTo() method, which two statements are true? (Choose two.)

A. The class implements java.lang.Comparable.
B. The class implements java.util.Comparator.
C. The interface used to implement sorting allows this class to define only one sort sequence.
D. The interface used to implement sorting allows this class to define many different sort sequences.

Answer: AC
Section: All

Explanation/Reference:

QUESTION 16

Given:
1. import java.util.*;
2.
3. public class Explorer3 {
4. public static void main(String[] args) {
5. TreeSet<Integer> s = new TreeSet<Integer>();
6. TreeSet<Integer> subs = new TreeSet<Integer>();
7. for (int i = 606; i < 613; i++)
8. if (i % 2 == 0)
9. s.add(i);
10. subs = (TreeSet) s.subSet(608, true, 611, true);
11. subs.add(629);
12. System.out.println(s + " " + subs);
13. }
14. }

What is the result?

A. Compilation fails.
B. An exception is thrown at runtime.
C. [608, 610, 612, 629] [608, 610]
D. [608, 610, 612, 629] [608, 610, 629]
E. [606, 608, 610, 612, 629] [608, 610]
F. [606, 608, 610, 612, 629] [608, 610, 629]

Answer: B
Section: All

Explanation/Reference:
 Exception in thread "main" java.lang.IllegalArgumentException: key out of
range
     at java.util.TreeMap$NavigableSubMap.put(TreeMap.java:1386)
     at java.util.TreeSet.add(TreeSet.java:238)
     at Explorer3.main(Explorer3.java:11)
   

QUESTION 17

Given:
1. import java.util.*;
2.
3. public class LetterASort {
4. public static void main(String[] args) {
5. ArrayList<String> strings = new ArrayList<String>();
6. strings.add("aAaA");
7. strings.add("AaA");
8. strings.add("aAa");
9. strings.add("AAaa");
10. Collections.sort(strings);
11. for (String s : strings) {
12. System.out.print(s + " ");
13. }
14. }
15. }

What is the result?

A. Compilation fails.
B. aAaA aAa AAaa AaA
C. AAaa AaA aAa aAaA
D. AaA AAaa aAaA aAa
E. aAa AaA aAaA AAaa
F. An exception is thrown at runtime.

Answer: C
Section: All

Explanation/Reference:

QUESTION 18

Given:
1. class A {
2. void foo() throws Exception {
3. throw new Exception();
4. }
5. }
6.
7. class SubB2 extends A {
8. void foo() {
9. System.out.println("B ");
10. }
11. }
12. class Tester {
13. public static void main(String[] args) {
14. A a = new SubB2();
15. a.foo();
16. }
17. }

What is the result?

A. B
B. B, followed by an Exception.
C. Compilation fails due to an error on line 9.
D. Compilation fails due to an error on line 15.
E. An Exception is thrown with no other output

Answer: D
Section: All

Explanation/Reference:
 Unhandled exception type Exception


QUESTION 19

Given a method that must ensure that its parameter is not null:

              11. public void someMethod(Object value) {
              12. // check for null value
              ...
              20. System.out.println(value.getClass());
              21. }
           
What, inserted at line 12, is the appropriate way to handle a null value?

A. assert value == null;
B. assert value != null, "value is null";
C. if (value == null) { throw new AssertionException("value is null"); }
D. if (value == null) { throw new IllegalArgumentException("value is null"); }

Answer: D
Section: All

Explanation/Reference:

QUESTION 20

Given:
1. public class Mule {
2. public static void main(String[] args) {
3. boolean assert = true;
4. if(assert) {
5. System.out.println("assert is true");
6. }
7. }
8. }

Which command-line invocations will compile?

A. javac Mule.java
B. javac -source 1.3 Mule.java
C. javac -source 1.4 Mule.java
D. javac -source 1.5 Mule.java

Answer: B
Section: All

Explanation/Reference:

QUESTION 21

Click the Exhibit button

1. public class A {
2. public void method1(){
3. B b = new B();
4. b.method2();
5. // more code here
6. }
7. }

1. public class B{
2. public void method2() {
3. C c = new C();
4. c.method3();
5. // more code here
6. }
7. }

1. public class C {
2. public void method3(){
3. // more code here
4. }
5. }

Given:
          try {
              A a = new A();
              a.method1();
          } catch (Exception e) {
              System.out.print("an error occurred");
          }
       
Which two statements are true if a NullPointerException is thrown on line 3 of class C? (Choose two.)

A. The application will crash.
B. The code on line 29 will be executed.
C. The code on line 5 of class A will execute.
D. The code on line 5 of class B will execute.
E. The exception will be propagated back to line 27.

Answer: BE
Section: All

Explanation/Reference:

QUESTION 22

Given:
1. public class Venus {
2. public static void main(String[] args) {
3. int[] x = { 1, 2, 3 };
4. int y[] = { 4, 5, 6 };
5. new Venus().go(x, y);
6. }
7.
8. void go(int[]... z) {
9. for (int[] a : z)
10. System.out.print(a[0]);
11. }
12. }

What is the result?

A. 1
B. 12
C. 14
D. 123
E. Compilation fails.
F. An exception is thrown at runtime.

Answer: C
Section: All

Explanation/Reference:

QUESTION 23

Given:
1. public class Test {
2. public enum Dogs {collie, harrier, shepherd};
3. public static void main(String [] args) {
4. Dogs myDog = Dogs.shepherd;
5. switch (myDog) {
6. case collie:
7. System.out.print("collie ");
8. case default:
9. System.out.print("retriever ");
10. case harrier:
11. System.out.print("harrier ");
12. }
13. }
14. }
What is the result?

A. harrier
B. shepherd
C. retriever
D. Compilation fails.
E. retriever harrier
F. An exception is thrown at runtime.

Answer: D
Section: All

Explanation/Reference:
   Test.java:10: illegal start of expression
                      case default:
                            ^
   Test.java:11: : expected
                                System.out.print("retriever ");
                                                                    ^
   2 errors
 

QUESTION 24

Given:
      static void test() {
          try {
              String x = null;
              System.out.print(x.toString() + " ");
          } finally {
              System.out.print("finally ");
          }
     }   
     public static void main(String[] args) {
          try {
              test();
          } catch (Exception ex) {
              System.out.print("exception ");
          }
     }   
   
What is the result?

A. null
B. finally
C. null finally
D. Compilation fails.
E. finally exception

Answer: E
Section: All

Explanation/Reference:

QUESTION 25

Given:
1. public class Breaker2 {
2. static String o = "";
3.
4. public static void main(String[] args) {
5. z: for (int x = 2; x < 7; x++) {
6. if (x == 3)
7. continue;
8. if (x == 5)
9. break z;
10. o = o + x;
11. }
12. System.out.println(o);
13. }
14. }
15.
What is the result?

A. 2
B. 24
C. 234
D. 246
E. 2346
F. Compilation fails.

Answer: B
Section: All

Explanation/Reference:

QUESTION 26

Given:
     public static void main(String[] args) {
          String str = "null";
          if (str == null) {
              System.out.println("null");
          } else (str.length() == 0) {
              System.out.println("zero");
           
          } else {
              System.out.println("some");
          }
     }
   
What is the result?

A. null
B. zero
C. some
D. Compilation fails.
E. An exception is thrown at runtime.

Answer: D
Section: All

Explanation/Reference:

QUESTION 27

Given:
1. import java.io.IOException;
2.
3. class A {
4.
5. public void process() {
6. System.out.print("A,");
7. }
8.
9. }
10.
11.
12. class B extends A {
13.
14. public void process() throws IOException {
15. super.process();
16. System.out.print("B,");
17. throw new IOException();
18. }
19.
20. public static void main(String[] args) {
21. try {
22. new B().process();
23. } catch (IOException e) {
24. System.out.println("Exception");
25. }
26. }
27. }

What is the result?

A. Exception
B. A,B,Exception
C. Compilation fails because of an error in line 20.
D. Compilation fails because of an error in line 14.
E. A NullPointerException is thrown at runtime.

Answer: D
Section: All

Explanation/Reference:
Exception IOException is not compatible with throws clause in A.process()

QUESTION 28

Given:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11. public void genNumbers() {
12. ArrayList numbers = new ArrayList();
13. for (int i = 0; i < 10; i++) {
14. int value = i * ((int) Math.random());
15. Integer intObj = new Integer(value);
16. numbers.add(intObj);
17. }
18. System.out.println(numbers);
19. }


Which line of code marks the earliest point that an object referenced by intObj becomes a candidate for garbage collection?

A. Line 16
B. Line 17
C. Line 18
D. Line 19
E. The object is NOT a candidate for garbage collection.

Answer: D
Section: All

Explanation/Reference:

QUESTION 29

Click the Exhibit button.



Given the fully-qualified class names:

com.foo.bar.Dog
com.foo.bar.blatz.Book
com.bar.Car
com.bar.blatz.Sun

Which graph represents the correct directory structure for a JAR file from which those classes can be used
by the compiler and JVM?

A. Jar A
B. Jar B
C. Jar C
D. Jar D
E. Jar E

Answer: A
Section: All

Explanation/Reference:

QUESTION 30

Given:
1. public class GC {
2. private Object o;
3. private void doSomethingElse(Object obj) { o = obj; }
4. public void doSomething() {
5. Object o = new Object();
6. doSomethingElse(o);
7. o = new Object();
8. doSomethingElse(null);
9. o = null;
10. }
11. }

When the doSomething method is called, after which line does the Object created in line 5 become
available for garbage collection?

A. Line 5
B. Line 6
C. Line 7
D. Line 8
E. Line 9
F. Line 10

Answer: D
Section: All

Explanation/Reference:

QUESTION 31

Given:
public class Spock {
     public static void main(String[] args) {
          Long tail = 2000L;
          Long distance = 1999L;
          Long story = 1000L;
          if ((tail > distance) ^ ((story * 2) == tail))
              System.out.print("1");
          if ((distance + 1 != tail) ^ ((story * 2) == distance))
              System.out.print("2");
     }
}

What is the result?

A. 1
B. 2
C. 12
D. Compilation fails.
E. No output is produced.
F. An exception is thrown at runtime.

Answer: E
Section: All

Explanation/Reference:

Saturday, December 29, 2012

EXAM G


QUESTION 1

Given that the current directory is empty, and that the user has read and write privileges to the current directory, and the following:

1. import java.io.*;
2. public class Maker {
3. public static void main(String[] args) {
4. File dir = new File("dir");
5. File f = new File(dir, "f");
6. }
7. }

Which statement is true?

A. Compilation fails.
B. Nothing is added to the file system.
C. Only a new file is created on the file system.
D. Only a new directory is created on the file system.
E. Both a new file and a new directory are created on the file system.

Answer: B
Section: All

Explanation/Reference:

QUESTION 2

Given:

 NumberFormat nf = NumberFormat.getInstance();
 nf.setMaximumFractionDigits(4);
 nf.setMinimumFractionDigits(2);
 String a = nf.format(3.1415926);
 String b = nf.format(2);

Which two statements are true about the result if the default locale is Locale.US? (Choose two.)

A. The value of b is 2.
B. The value of a is 3.14.
C. The value of b is 2.00.
D. The value of a is 3.141.
E. The value of a is 3.1415.
F. The value of a is 3.1416.
G. The value of b is 2.0000.

Answer: CF
Section: All

Explanation/Reference:

QUESTION 3

Which three statements concerning the use of the java.io.Serializable interface are true? (Choose three.)

A. Objects from classes that use aggregation cannot be serialized.
B. An object serialized on one JVM can be successfully deserialized on a different JVM.
C. The values in fields with the volatile modifier will NOT survive serialization and deserialization.
D. The values in fields with the transient modifier will NOT survive serialization and deserialization.
E. It is legal to serialize an object of a type that has a supertype that does NOT implement java.io.
   Serializable.
 
Answer: BDE
Section: All

Explanation/Reference:

QUESTION 4

Given:
12. String csv = "Sue,5,true,3";
13. Scanner scanner = new Scanner( csv );
14. scanner.useDelimiter(",");
15. int age = scanner.nextInt();

What is the result?

A. Compilation fails.
B. After line 15, the value of age is 5.
C. After line 15, the value of age is 3.
D. An exception is thrown at runtime.

Answer: D
Section: All

Explanation/Reference:

QUESTION 5

Given that c is a reference to a valid java.io.Console object, which two code fragments read a line of text from the console? (Choose two.)

A. String s = c.readLine();
B. char[] c = c.readLine();
C. String s = c.readConsole();
D. char[] c = c.readConsole();
E. String s = c.readLine("%s", "name ");
F. char[] c = c.readLine("%s", "name ");

Answer: AE
Section: All

Explanation/Reference:

QUESTION 6

Given:

11. String test = "a1b2c3";
12. String[] tokens = test.split("\\d");
13. for(String s: tokens) System.out.print(s + " ");

What is the result?

A. abc
B. 123
C. a1b2c3
D. a1 b2 c3
E. Compilation fails.
F. The code runs with no output.
G. An exception is thrown at runtime.

Answer: A
Section: All

Explanation/Reference:

QUESTION 7

Given:

33. Date d = new Date(0);
34. String ds = "December 15, 2004";
35. // insert code here
36. try {
37. d = df.parse(ds);
38. }
39. catch(ParseException e) {
40. System.out.println("Unable to parse " + ds);
41. }
42. // insert code here too

What creates the appropriate DateFormat object and adds a day to the Date object?

A. 35. DateFormat df = DateFormat.getDateFormat();
     42. d.setTime( (60 * 60 * 24) + d.getTime());
B. 35. DateFormat df = DateFormat.getDateInstance();
     42. d.setTime( (1000 * 60 * 60 * 24) + d.getTime());
C. 35. DateFormat df = DateFormat.getDateFormat();
     42. d.setLocalTime( (1000*60*60*24) + d.getLocalTime());
D. 35. DateFormat df = DateFormat.getDateInstance();
     42. d.setLocalTime( (60 * 60 * 24) + d.getLocalTime());
 
Answer: B
Section: All

Explanation/Reference:

QUESTION 8

Given:
1. public class KungFu {
2. public static void main(String[] args) {
3. Integer x = 400;
4. Integer y = x;
5. x++;
6. StringBuilder sb1 = new StringBuilder("123");
7. StringBuilder sb2 = sb1;
8. sb1.append("5");
9. System.out.println((x == y) + " " + (sb1 == sb2));
10. }
11. }
What is the result?

A. true true
B. false true
C. true false
D. false false
E. Compilation fails.
F. An exception is thrown at runtime.

Answer: B
Section: All

Explanation/Reference:

QUESTION 9

Given:
class Converter {
     public static void main(String[] args) {
          Integer i = args[0];
          int j = 12;
          System.out.println("It is " + (j == i) + " that j==i.");
     }
}

What is the result when the programmer attempts to compile the code and run it with the command java Converter 12?

A. It is true that j==i.
B. It is false that j==i.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 13.

Answer: D
Section: All

Explanation/Reference:
 Integer i = args[0]; Type mismatch: cannot convert from String to Integer


QUESTION 10

Given:
class Foo {
     static void alpha() {
          /* more code here */
     }
   
     void beta() {
          /* more code here */
     }
}
Which two statements are true? (Choose two.)

A. Foo.beta() is a valid invocation of beta().
B. Foo.alpha() is a valid invocation of alpha().
C. Method beta() can directly call method alpha().
D. Method alpha() can directly call method beta().

Answer: BC
Section: All

Explanation/Reference:

QUESTION 11

Click the Exhibit button.
1. public class A {
2. public String doit(int x, int y){
3. return "a";
4. }
5.
6. public String doit(int... vals){
7. return "b";
8. }
9. }

Given:
      25. A a = new A();
      26. System.out.println(a.doit(4, 5));
   
What is the result?

A. Line 26 prints "a" to System.out.
B. Line 26 prints "b" to System.out.
C. An exception is thrown at line 26 at runtime.
D. Compilation of class A will fail due to an error in line 6.

Answer: A
Section: All

Explanation/Reference:

QUESTION 12

Which two code fragments correctly create and initialize a static array of int elements? (Choose two.)

A. static final int[] a = { 100,200 };
B. static final int[] a;
     static { a=new int[2]; a[0]=100; a[1]=200; }
C. static final int[] a = new int[2]{ 100,200 };
D. static final int[] a;
     static void init() { a = new int[3]; a[0]=100; a[1]=200; }
 
Answer: AB
Section: All

Explanation/Reference:

QUESTION 13

Given:
1. public class Plant {
2. private String name;
3.
4. public Plant(String name) {
5. this.name = name;
6. }
7.
8. public String getName() {
9. return name;
10. }
11. }

1. public class Tree extends Plant {
2. public void growFruit() {
3. }
4.
5. public void dropLeaves() {
6. }
7. }

Which statement is true?

A. The code will compile without changes.
B. The code will compile if public Tree() { Plant(); } is added to the Tree class.
C. The code will compile if public Plant() { Tree(); } is added to the Plant class.
D. The code will compile if public Plant() { this("fern"); } is added to the Plant class.
E. The code will compile if public Plant() { Plant("fern"); } is added to the Plant class.

Answer: D
Section: All

Explanation/Reference:

QUESTION 14

Click the Exhibit button.

1. public class GoTest {
2. public static void main(String[] args) {
3. Sente a = new Sente(); a.go();
4. Goban b = new Goban(); b.go();
5. Stone c = new Stone(); c.go();
6. }
7. }
8.
9. class Sente implements Go {
10. public void go(){
11. System.out.println("go in Sente");
12. }
13. }
14.
15. class Goban extends Sente {
16. public void go(){
17. System.out.println("go in Goban");
18. }
19.
20. }
21. class Stone extends Goban implements Go{
22. }
23.
24. interface Go { public void go(); }

What is the result?

A. go in Goban go in Sente go in Sente
B. go in Sente go in Sente go in Goban
C. go in Sente go in Goban go in Goban
D. go in Goban go in Goban go in Sente
E. Compilation fails because of an error in line 17.

Answer: C
Section: All

Explanation/Reference:

QUESTION 15

Which two classes correctly implement both the java.lang.Runnable and the java.lang.
Cloneable interfaces? (Choose two.)

A. public class Session implements Runnable, Cloneable {
        public void run();    
        public Object clone();
   }
B. public class Session extends Runnable, Cloneable {
        public void run() { /* do something */ }
        public Object clone() { /* make a copy */ }
   }
C. public class Session implements Runnable, Cloneable {
        public void run() { /* do something */ }
        public Object clone() { /* make a copy */ }
   }
D. public abstract class Session implements Runnable, Cloneable {
        public void run() { /* do something */ }
        public Object clone() { /*make a copy */ }
   }
E. public class Session implements Runnable, implements Cloneable {
        public void run() { /* do something */ }
        public Object clone() { /* make a copy */ }
   }
 
Answer: CD
Section: All

Explanation/Reference:

QUESTION 16

Given:

public interface A111 {
     String s = "yo";
     public void method1();
}
interface B {
}
interface C extends A111, B {
     public void method1();   
     public void method1(int x);
}

What is the result?

A. Compilation succeeds.
B. Compilation fails due to multiple errors.
C. Compilation fails due to an error only on line 20.
D. Compilation fails due to an error only on line 21.
E. Compilation fails due to an error only on line 22.
F. Compilation fails due to an error only on line 12.

Answer: A
Section: All

Explanation/Reference:


QUESTION 17

Click the Exhibit button.

1.
2.
3.
4.
5.
6.
7.
8.
9.
10. interface Foo{
11. int bar();
12. }
13.
14. public class Beta {
15.
16. class A implements Foo {
17. public int bar(){ return 1; }
18. }
19.
20. public int fubar(Foo foo){ return foo.bar(); }
21.
22. public void testFoo(){
23.
24. class A implements Foo{
25. public int bar(){return 2;}
26. }
27.
28. System.out.println(fubar(new A()));
29. }
30.
31. public static void main(String[] args) {
32. new Beta().testFoo();
33. }
34. }

Which three statements are true? (Choose three.)

A. Compilation fails.
B. The code compiles and the output is 2.
C. If lines 16, 17 and 18 were removed, compilation would fail.
D. If lines 24, 25 and 26 were removed, compilation would fail.
E. If lines 16, 17 and 18 were removed, the code would compile and the output would be 2.
F. If lines 24, 25 and 26 were removed, the code would compile and the output would be 1.

Answer: BEF
Section: All

Explanation/Reference:

QUESTION 18

Given:
class Alpha {
     public void foo() { System.out.print("Afoo "); }
}
public class Beta extends Alpha {
     public void foo() { System.out.print("Bfoo "); }
     public static void main(String[] args) {
          Alpha a = new Beta();
          Beta b = (Beta)a;
          a.foo();
          b.foo();
     }
}
What is the result?

A. Afoo Afoo
B. Afoo Bfoo
C. Bfoo Afoo
D. Bfoo Bfoo
E. Compilation fails.
F. An exception is thrown at runtime.

Answer: D
Section: All

Explanation/Reference:

QUESTION 19

Given:
1. public class TestOne {
2. public static void main (String[] args) throws Exception {
3. Thread.sleep(3000);
4. System.out.println("sleep");
5. }
6. }
What is the result?

A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints "sleep".
D. The code executes normally, but nothing is printed.

Answer: C
Section: All

Explanation/Reference:

QUESTION 20

Given:
1. public class Threads3 implements Runnable {
2. public void run() {
3. System.out.print("running");
4. }
5. public static void main(String[] args) {
6. Thread t = new Thread(new Threads3());
7. t.run();
8. t.run();
9. t.start();
10. }
11. }
What is the result?

A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes and prints "running".
D. The code executes and prints "runningrunning".
E. The code executes and prints "runningrunningrunning".

Answer: E
Section: All

Explanation/Reference:

QUESTION 21

Given:

public class NamedCounter {
     private final String name;
     private int count;
   
     public NamedCounter(String name) {
          this.name = name;
     }
   
     public String getName() {
          return name;
     }
   
     public void increment() {
          count++;
     }
   
     public int getCount() {
          return count;
     }
   
     public void reset() {
          count = 0;
     }
}

Which three changes should be made to adapt this class to be used safely by multiple threads? (Choose three.)

A. declare reset() using the synchronized keyword
B. declare getName() using the synchronized keyword
C. declare getCount() using the synchronized keyword
D. declare the constructor using the synchronized keyword
E. declare increment() using the synchronized keyword

Answer: ACE
Section: All

Explanation/Reference:

QUESTION 22

Given that Triangle implements Runnable, and:

     void go() throws Exception {
          Thread t = new Thread(new Triangle());
          t.start();
          for(int x = 1; x < 100000; x++) {
              //insert code here Linea 35
               if(x%100 == 0) System.out.print("g");
          }
     }
      public void run() {
          try {
               for(int x = 1; x < 100000; x++) {
                   // insert the same code here Linea 41
                    if(x%100 == 0) System.out.print("t");
              }
          } catch (Exception e) {
       
          }
     }
   
Which two statements, inserted independently at both lines 35 and 41, tend to allow both threads to
temporarily pause and allow the other thread to execute? (Choose two.)

A. Thread.wait();
B. Thread.join();
C. Thread.yield();
D. Thread.sleep(1);
E. Thread.notify();

Answer: CD
Section: All

Explanation/Reference:

QUESTION 23

Given:

1. public class TestSeven extends Thread {
2. private static int x;
3. public synchronized void doThings() {
4. int current = x;
5. current++;
6. x = current;
7. }
8. public void run() {
9. doThings();
10. }
11. }

Which statement is true?

A. Compilation fails.
B. An exception is thrown at runtime.
C. Synchronizing the run() method would make the class thread-safe.
D. The data in variable "x" are protected from concurrent access problems.
E. Declaring the doThings() method as static would make the class thread-safe.
F. Wrapping the statements within doThings() in a synchronized(new Object()) { } block would make the class thread-safe.
 
Answer: E
Section: All

Explanation/Reference:

QUESTION 24

Given:
public class Yikes {
     public static void go(Long n) {
          System.out.print("Long ");
     }   
     public static void go(Short n) {
          System.out.print("Short ");
     }   
     public static void go(int n) {
          System.out.print("int ");
     }   
     public static void main(String[] args) {
          short y = 6;
          long z = 7;
          go(y);
          go(z);
     }
}
What is the result?

A. int Long
B. Short Long
C. Compilation fails.
D. An exception is thrown at runtime.

Answer: A
Section: All

Explanation/Reference:

QUESTION 25

Given:

     12. Date date = new Date();
     13. df.setLocale(Locale.ITALY);
     14. String s = df.format(date);
   
The variable df is an object of type DateFormat that has been initialized in line 11.
What is the result if this code is run on December 14, 2000?

A. The value of s is 14-dic-2000.
B. The value of s is Dec 14, 2000.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 13.

Answer: D
Section: All

Explanation/Reference:
 The method setLocale(Locale) is undefined for the type DateFormat

QUESTION 26

Which two scenarios are NOT safe to replace a StringBuffer object with a StringBuilder object? (Choose two.)

A. When using versions of Java technology earlier than 5.0.
B. When sharing a StringBuffer among multiple threads.
C. When using the java.io class StringBufferInputStream.
D. When you plan to reuse the StringBuffer to build more than one string.

Answer: AB
Section: All

Explanation/Reference:

QUESTION 27

Given that c is a reference to a valid java.io.Console object, and:

          11. String pw = c.readPassword("%s", "pw: ");
          12. System.out.println("got " + pw);
          13. String name = c.readLine("%s", "name: ");
          14. System.out.println(" got ", name);

If the user types fido when prompted for a password, and then responds bob when prompted for a name, what is the result?

A. pw: got fido name: bob got bob
B. pw: fido got fido name: bob got bob
C. pw: got fido name: bob got bob
D. pw: fido got fido name: bob got bob
E. Compilation fails.
F. An exception is thrown at runtime.

Answer: E
Section: All

Explanation/Reference:
The method println(String) in the type PrintStream is not applicable for the arguments
(String, String)

QUESTION 28

Given:
          11. String test = "This is a test";
          12. String[] tokens = test.split("\s");
          13. System.out.println(tokens.length);
       
What is the result?

A. 0
B. 1
C. 4
D. Compilation fails.
E. An exception is thrown at runtime.

Answer: D
Section: All

Explanation/Reference:
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )

QUESTION 29

Given:
import java.io.*;
class Animal {
     Animal() {
          System.out.print("a");
     }
}
class Dog extends Animal implements Serializable {
     Dog() {
          System.out.print("d");
     }
}
public class Beagle extends Dog {
}

If an instance of class Beagle is created, then Serialized, then deSerialized, what is the result?

A. ad
B. ada
C. add
D. adad
E. Compilation fails.
F. An exception is thrown at runtime.

Answer: B
Section: All

Explanation/Reference:

QUESTION 30

Given:
          11. double input = 314159.26;
          12. NumberFormat nf = NumberFormat.getInstance(Locale.ITALIAN);
          13. String b;
          14. //insert code here
       
Which code, inserted at line 14, sets the value of b to 314.159,26?

A. b = nf.parse( input );
B. b = nf.format( input );
C. b = nf.equals( input );
D. b = nf.parseObject( input );

Answer: B
Section: All

Explanation/Reference:

QUESTION 31

A team of programmers is involved in reviewing a proposed design for a new utility class.
After some discussion, they realize that the current design allows other classes to access
methods in the utility class that should be accessible only to methods within the utility class itself.

What design issue has the team discovered?

A. Tight coupling
B. Low cohesion
C. High cohesion
D. Loose coupling
E. Weak encapsulation
F. Strong encapsulation

Answer: E
Section: All

Explanation/Reference:


QUESTION 32

Given a method that must ensure that its parameter is not null:
              11. public void someMethod(Object value) {
              12. // check for null value
              ...
              20. System.out.println(value.getClass());
              21. }
           
What, inserted at line 12, is the appropriate way to handle a null value?

A. assert value == null;
B. assert value != null, "value is null";
C. if (value == null) { throw new AssertionException("value is null"); }
D. if (value == null) { throw new IllegalArgumentException("value is null"); }

Answer: D
Section: All

Explanation/Reference:

Friday, December 28, 2012

EXAM F


QUESTION 1

Which two code fragments will execute the method doStuff() in a separate thread? (Choose two.)

A. new Thread() {
        public void run() { doStuff(); }
   };
B. new Thread() {
        public void start() { doStuff(); }
   };
C. new Thread() {
        public void start() { doStuff(); }
   }.run();
D. new Thread() {
        public void run() { doStuff(); }
   }.start();
E. new Thread(new Runnable() {
        public void run() { doStuff(); }
   }).run();
F. new Thread(new Runnable() {
        public void run() { doStuff(); }
   }).start();
 
Answer: DF
Section: All

Explanation/Reference:

QUESTION 2

Given:

public class Person {
     private String name;
     public Person(String name) {
          this.name = name;
     }
     public boolean equals(Object o) {
          if ( ! ( o instanceof Person) ) return false;
          Person p = (Person) o;
          return p.name.equals(this.name);
     }
}

Which statement is true?

A. Compilation fails because the hashCode method is not overridden.
B. A HashSet could contain multiple Person objects with the same name.
C. All Person objects will have the same hash code because the hashCode method is not overridden.
D. If a HashSet contains more than one Person object with name="Fred", then removing another Person,  also with name="Fred", will remove them all.
 
Answer: B
Section: All

Explanation/Reference:

QUESTION 3

Given:

import java.util.*;
public class SortOf {
     public static void main(String[] args) {
          ArrayList<Integer> a = new ArrayList<Integer>();
          a.add(1); a.add(5); a.add(3);
          Collections.sort(a);
          a.add(2);
          Collections.reverse(a);
          System.out.println(a);
     }
}

What is the result?

A. [1, 2, 3, 5]
B. [2, 1, 3, 5]
C. [2, 5, 3, 1]
D. [5, 3, 2, 1]
E. [1, 3, 5, 2]
F. Compilation fails.
G. An exception is thrown at runtime.

Answer: C
Section: All

Explanation/Reference:

QUESTION 4

Given:
public class Person {
     private name;
     public Person(String name) {
          this.name = name;
     }
     public int hashCode() {
          return 420;
     }
}
Which statement is true?

A. The time to find the value from HashMap with a Person key depends on the size of the map.
B. Deleting a Person key from a HashMap will delete all map entries for all keys of type Person.
C. Inserting a second Person object into a HashSet will cause the first Person object to be removed as a  duplicate.
D. The time to determine whether a Person object is contained in a HashSet is constant and does NOT depend on the size of the map.
 
Answer: A
Section: All

Explanation/Reference:

QUESTION 5

Given:

import java.util.TreeSet;
public class Explorer2 {
     public static void main(String[] args) {
          TreeSet<Integer> s = new TreeSet<Integer>();
          TreeSet<Integer> subs = new TreeSet<Integer>();
          for(int i = 606; i < 613; i++)
               if(i%2 == 0) s.add(i);
          subs = (TreeSet)s.subSet(608, true, 611, true);
          s.add(629);
          System.out.println(s + " " + subs);
     }
}

What is the result?

A. Compilation fails.
B. An exception is thrown at runtime.
C. [608, 610, 612, 629] [608, 610]
D. [608, 610, 612, 629] [608, 610, 629]
E. [606, 608, 610, 612, 629] [608, 610]
F. [606, 608, 610, 612, 629] [608, 610, 629]

Answer: E
Section: All

Explanation/Reference:

QUESTION 6

Given:

public class Drink implements Comparable {
     public String name;
     public int compareTo(Object o) {
          return 0;
     }
}

and:

Drink one = new Drink();
Drink two = new Drink();
one.name= "Coffee";
two.name= "Tea";
TreeSet set = new TreeSet();
set.add(one);
set.add(two);

A programmer iterates over the TreeSet and prints the name of each Drink object.
What is the result?

A. Tea
B. Coffee
C. Coffee Tea
D. Compilation fails.
E. The code runs with no output.
F. An exception is thrown at runtime.

Answer: B
Section: All

Explanation/Reference:

QUESTION 7

A programmer must create a generic class MinMax and the type parameter of MinMax must implement
Comparable.
Which implementation of MinMax will compile?

A. class MinMax<E extends Comparable<E>> {
        E min = null;
        E max = null;
        public MinMax() {}
        public void put(E value) { /* store min or max */ }
B. class MinMax<E implements Comparable<E>> {
        E min = null;
        E max = null;
        public MinMax() {}
        public void put(E value) { /* store min or max */ }
C. class MinMax<E extends Comparable<E>> {
        <E> E min = null;
        <E> E max = null;
        public MinMax() {}
        public <E> void put(E value) { /* store min or max */ }
D. class MinMax<E implements Comparable<E>> {
        <E> E min = null;
        <E> E max = null;
        public MinMax() {}
        public <E> void put(E value) { /* store min or max */ }
     
Answer: A
Section: All

Explanation/Reference:

QUESTION 8

Given:
1. import java.util.*;
2. public class Example {
3. public static void main(String[] args) {
4. // insert code here
5. set.add(new Integer(2));
6. set.add(new Integer(1));
7. System.out.println(set);
8. }
9. }
Which code, inserted at line 4, guarantees that this program will output [1, 2]?

A. Set set = new TreeSet();
B. Set set = new HashSet();
C. Set set = new SortedSet();
D. List set = new SortedList();
E. Set set = new LinkedHashSet();

Answer: AB
Section: All

Explanation/Reference:


QUESTION 9

Given:
1.
2.
3.
4.
5. class A {
6. void foo() throws Exception { throw new Exception(); }
7. }
8. class SubB2 extends A {
9. void foo() { System.out.println("B "); }
10. }
11. class Tester {
12. public static void main(String[] args) {
13. A a = new SubB2();
14. a.foo();
15. }
16. }
What is the result?

A. B
B. B, followed by an Exception.
C. Compilation fails due to an error on line 9.
D. Compilation fails due to an error on line 14.
E. An Exception is thrown with no other output.

Answer: D
Section: All

Explanation/Reference:

QUESTION 10

Given:
try {
     ResourceConnection con = resourceFactory.getConnection();
     Results r = con.query("GET INFO FROM CUSTOMER"); // Linea 86
     info = r.getData(); 88. con.close();
} catch (ResourceException re) {
     errorLog.write(re.getMessage());
}
return info;
Which statement is true if a ResourceException is thrown on line 86?

A. Line 92 will not execute.
B. The connection will not be retrieved in line 85.
C. The resource connection will not be closed on line 88.
D. The enclosing method will throw an exception to its caller.

Answer: C
Section: All

Explanation/Reference:

QUESTION 11

Given:

public class Breaker {
     static String o = "";
     public static void main(String[] args) {
          z: o = o + 2;
          for (int x = 3; x < 8; x++) {
               if (x == 4)
                    break;
               if (x == 6)
                    break z;
              o = o + x;
          }
          System.out.println(o);
     }
}

What is the result?

A. 23
B. 234
C. 235
D. 2345
E. 2357
F. 23457
G. Compilation fails.

Answer: G
Section: All

Explanation/Reference:

QUESTION 12

Given:
     public void go(int x) {
          assert (x > 0); //Line 12
          switch(x) {
          case 2: ;
          default: assert false; //Line 15
          }
     }
     private void go2(int x) { assert (x < 0); } //Line 18   
   
Which statement is true?

A. All of the assert statements are used appropriately.
B. Only the assert statement on line 12 is used appropriately.
C. Only the assert statement on line 15 is used appropriately.
D. Only the assert statement on line 18 is used appropriately.
E. Only the assert statements on lines 12 and 15 are used appropriately.
F. Only the assert statements on lines 12 and 18 are used appropriately.
G. Only the assert statements on lines 15 and 18 are used appropriately.

Answer: G
Section: All

Explanation/Reference:

QUESTION 13

Given:
     public static void main(String[] args) {
          try {
              args = null;
              args[0] = "test";
              System.out.println(args[0]);
          } catch (Exception ex) {
              System.out.println("Exception");
          } catch (NullPointerException npe) {
              System.out.println("NullPointerException");
          }
     }
What is the result?

A. test
B. Exception
C. Compilation fails.
D. NullPointerException

Answer: C
Section: All

Explanation/Reference:

QUESTION 14

Given:
     public static void main(String[] args) {
          for (int i = 0; i <= 10; i++) {
               if (i > 6) break;
          }
          System.out.println(i);
     }
What is the result?

A. 6
B. 7
C. 10
D. 11
E. Compilation fails.
F. An exception is thrown at runtime.

Answer: E
Section: All

Explanation/Reference:

QUESTION 15

Given:

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11. class X { public void foo() { System.out.print("X "); } }
12.
13. public class SubB extends X {
14. public void foo() throws RuntimeException {
15. super.foo();
16. if (true) throw new RuntimeException();
17. System.out.print("B ");
18. }
19. public static void main(String[] args) {
20. new SubB().foo();
21. }
22. }

What is the result?

A. X, followed by an Exception.
B. No output, and an Exception is thrown.
C. Compilation fails due to an error on line 14.
D. Compilation fails due to an error on line 16.
E. Compilation fails due to an error on line 17.
F. X, followed by an Exception, followed by B.

Answer: A
Section: All

Explanation/Reference:
 X Exception in thread "main" java.lang.RuntimeException
     at SubB.foo(SubB.java:5)
     at SubB.main(SubB.java:9)
   

QUESTION 16

Given:

public void testIfA() {
     if (testIfB("True")) { //Linea 12
          System.out.println("True");
     } else {
          System.out.println("Not true");
     }
}
public Boolean testIfB(String str) {
     return Boolean.valueOf(str); //Linea 19
}

What is the result when method testIfA is invoked?

A. True
B. Not true
C. An exception is thrown at runtime.
D. Compilation fails because of an error at line 12.
E. Compilation fails because of an error at line 19.

Answer: A
Section: All

Explanation/Reference:

QUESTION 17

Which can appropriately be thrown by a programmer using Java SE technology to create a desktop application?

A. ClassCastException
B. NullPointerException
C. NoClassDefFoundError
D. NumberFormatException
E. ArrayIndexOutOfBoundsException

Answer: D
Section: All

Explanation/Reference:

QUESTION 18

Which two code fragments are most likely to cause a StackOverflowError? (Choose two.)

A. int []x = {1,2,3,4,5};
   for(int y = 0; y < 6; y++)
        System.out.println(x[y]);
B. static int[] x = {7,6,5,4};
   static { x[1] = 8;
   x[4] = 3; }
C. for(int y = 10; y < 10; y++)
        doStuff(y);
D. void doOne(int x) { doTwo(x); }
   void doTwo(int y) { doThree(y); }
   void doThree(int z) { doTwo(z); }
E. for(int x = 0; x < 1000000000; x++)
        doStuff(x);
F. void counter(int i) { counter(++i); }

Answer: DF
Section: All

Explanation/Reference:

QUESTION 19

Given:

public static void main(String[] args) {
     Integer i = new Integer(1) + new Integer(2);
     switch(i) {
     case 3: System.out.println("three"); break;
     default: System.out.println("other"); break;
     }
}

What is the result?

A. three
B. other
C. An exception is thrown at runtime.
D. Compilation fails because of an error on line 12.
E. Compilation fails because of an error on line 13.
F. Compilation fails because of an error on line 15.

Answer: A
Section: All

Explanation/Reference:

QUESTION 20

Given:

public class Tahiti {
     Tahiti t;   
     public static void main(String[] args) {
          Tahiti t = new Tahiti();
          Tahiti t2 = t.go(t);
          t2 = null;
          // more code here 11
     }   
     Tahiti go(Tahiti t) {
          Tahiti t1 = new Tahiti();
          Tahiti t2 = new Tahiti();
          t1.t = t2;
          t2.t = t1;
          t.t = t2;
          return t1;
     }
}

When line 11 is reached, how many objects are eligible for garbage collection?

A. 0
B. 1
C. 2
D. 3
E. Compilation fails.

Answer: A
Section: All

Explanation/Reference:

QUESTION 21

Given:

interface Animal {
     void makeNoise();
}
class Horse implements Animal {
     Long weight = 1200L;   
     public void makeNoise() {
          System.out.println("whinny");
     }
}
public class Icelandic extends Horse {
     public void makeNoise() {
          System.out.println("vinny");
     }   
     public static void main(String[] args) {
          Icelandic i1 = new Icelandic();
          Icelandic i2 = new Icelandic();
          Icelandic i3 = new Icelandic();
          i3 = i1;
          i1 = i2;
          i2 = null;
          i3 = i1;
     } //Linea 14
}

When line 14 is reached, how many objects are eligible for the garbage collector?

A. 0
B. 1
C. 2
D. 3
E. 4
F. 6

Answer: E
Section: All

Explanation/Reference:

QUESTION 22

Given:

public class Commander {
     public static void main(String[] args) {
          String myProp = /* insert code here Linea 13 */
          System.out.println(myProp);
     }
}

and the command line: java -Dprop.custom=gobstopper Commander
Which two, placed on line 13, will produce the output gobstopper? (Choose two.)

A. System.load("prop.custom");
B. System.getenv("prop.custom");
C. System.property("prop.custom");
D. System.getProperty("prop.custom");
E. System.getProperties().getProperty("prop.custom");

Answer: DE
Section: All

Explanation/Reference:

QUESTION 23

Given:
public class ItemTest {
     private final int id;
   
     public ItemTest(int id) {
          this.id = id;
     }
   
     public void updateId(int newId) {
          id = newId;
     }
   
     public static void main(String[] args) {
          ItemTest fa = new ItemTest(42);
          fa.updateId(69);
          System.out.println(fa.id);
     }
}
What is the result?

A. Compilation fails.
B. An exception is thrown at runtime.
C. The attribute id in the ItemTest object remains unchanged.
D. The attribute id in the ItemTest object is modified to the new value.
E. A new ItemTest object is created with the preferred value in the id attribute.

Answer: A
Section: All

Explanation/Reference:
The final field ItemTest.id cannot be assigned

QUESTION 24

A developer is creating a class Book, that needs to access class Paper.
The Paper class is deployed in a JAR named myLib.jar.
Which three, taken independently, will allow the developer to use the Paper class while compiling the Book class? (Choose three.)

A. The JAR file is located at $JAVA_HOME/jre/classes/myLib.jar.
B. The JAR file is located at $JAVA_HOME/jre/lib/ext/myLib.jar..
C. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set that includes /foo/myLib.jar/Paper.class.
D. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set that includes /foo/myLib.jar.
E. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac - cp /foo/myLib.jar/Paper Book.java.
F. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -d /foo/myLib.jar Book.java
G. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac - classpath /foo/myLib.jar Book.java
 
Answer: BDG
Section: All

Explanation/Reference:

QUESTION 25

Given:

public class Yippee {
     public static void main(String[] args) {
          for (int x = 1; x < args.length; x++) {
              System.out.print(args[x] + " ");
          }
     }
}
and two separate command line invocations: java Yippee java Yippee 1 2 3 4

What is the result?

A. No output is produced. 1 2 3
B. No output is produced. 2 3 4
C. No output is produced. 1 2 3 4
D. An exception is thrown at runtime. 1 2 3
E. An exception is thrown at runtime. 2 3 4
F. An exception is thrown at runtime. 1 2 3 4

Answer: B
Section: All

Explanation/Reference:

QUESTION 26

Click the Exhibit button.

class Foo {
     private int x;
     public Foo( int x ){ this.x = x;}
     public void setX( int x ) { this.x = x; }
     public int getX(){ return x;}
}
public class Gamma {
     static Foo fooBar(Foo foo) {
          foo = new Foo(100);
          return foo;
     }   
     public static void main(String[] args) {
          Foo foo = new Foo( 300 );
          System.out.println( foo.getX() + "-");
       
          Foo fooFoo = fooBar(foo);
          System.out.println(foo.getX() + "-");
          System.out.println(fooFoo.getX() + "-");       
          foo = fooBar( fooFoo);
          System.out.println( foo.getX() + "-");
          System.out.println(fooFoo.getX());
     }
}

What is the output of the program shown in the exhibit?

A. 300-100-100-100-100
B. 300-300-100-100-100
C. 300-300-300-100-100
D. 300-300-300-300-100

Answer: B
Section: All

Explanation/Reference:

QUESTION 27

Given classes defined in two different files:

1. package packageA;
2. public class Message {
3. String getText() {
4. return "text";
5. }
6. }

And:

1. package packageB;
2.
3. public class XMLMessage extends packageA.Message {
4. String getText() {
5. return "<msg>text</msg>";
6. }
7.
8. public static void main(String[] args) {
9. System.out.println(new XMLMessage().getText());
10. }
11. }

What is the result of executing XMLMessage.main?

A. text
B. Compilation fails.
C. <msg>text</msg>
D. An exception is thrown at runtime.

Answer: C
Section: All

Explanation/Reference:

QUESTION 28

Given:
interface Fish {
}
class Perch implements Fish {
}
class Walleye extends Perch {
}
class Bluegill {
}
public class Fisherman {
     public static void main(String[] args) {
          Fish f = new Walleye();
          Walleye w = new Walleye();
          Bluegill b = new Bluegill();
          if (f instanceof Perch)
              System.out.print("f-p ");
          if (w instanceof Fish)
              System.out.print("w-f ");
          if (b instanceof Fish)
              System.out.print("b-f ");
     }
}
What is the result?

A. w-f
B. f-p w-f
C. w-f b-f
D. f-p w-f b-f
E. Compilation fails.
F. An exception is thrown at runtime.

Answer: B
Section: All

Explanation/Reference:

QUESTION 29

Given:

1. package com.company.application;
2.
3. public class MainClass {
4. public static void main(String[] args) {
5. }
6. }

And MainClass exists in the /apps/com/company/application directory.
Assume the CLASSPATH environment variable is set to "." (current directory).
Which two java commands entered at the command line will run MainClass? (Choose two.)

A. java MainClass if run from the /apps directory
B. java com.company.application.MainClass if run from the /apps directory
C. java -classpath /apps com.company.application.MainClass if run from any directory
D. java -classpath . MainClass if run from the /apps/com/company/application directory
E. java -classpath /apps/com/company/application:. MainClass if run from the /apps directory
F. java com.company.application.MainClass if run from the /apps/com/company/application directory

Answer: BC
Section: All

Explanation/Reference:

QUESTION 30

Given
class Foo {
     static void alpha() {
          /* more code here */
     }
   
     void beta() {
          /* more code here */
     }
}
Which two statements are true? (Choose two.)

A. Foo.beta() is a valid invocation of beta().
B. Foo.alpha() is a valid invocation of alpha().
C. Method beta() can directly call method alpha().
D. Method alpha() can directly call method beta().

Answer: BC
Section: All

Explanation/Reference:

QUESTION 31

Given:

1. public class TestSeven extends Thread {
2. private static int x;
3. public synchronized void doThings() {
4. int current = x;
5. current++;
6. x = current;
7. }
8. public void run() {
9. doThings();
10. }
11. }

Which statement is true?

A. Compilation fails.
B. An exception is thrown at runtime.
C. Synchronizing the run() method would make the class thread-safe.
D. The data in variable "x" are protected from concurrent access problems.
E. Declaring the doThings() method as static would make the class thread-safe.
F. Wrapping the statements within doThings() in a synchronized(new Object()) { } block would make the class thread-safe.
 
Answer: E
Section: All

Explanation/Reference: