SCJP / OCJP dumps 1 - Exam H




You are here : Home / Core Java Tutorials /  SCJP / OCJP dumps

Sun.RealExamQuestions.310-065.Oracle.1Z0-851.v2012-01-28.by.Carlos.239q
Number: 1Z0-851 Passing Score: 610 Time Limit: 150 min File Version: 2012-01-28
Sun.RealExamQuestions.310-065.Oracle.1Z0-851.v2012-01-28.by.Carlos.239q
Exam : Sun 310-065 / Oracle 1Z0-851
Version : 2012-01-28
By Carlos - Brazil
Questions : 239 9 Exams (from A - I)
This file is based on Sun RealExamQuestions 310-065 v2011-11-08 by Stephen 306q.vce (http:// www.examcollection.com/sun/Sun.RealExamQuestions.310-065.v2011-11-08.by.Stephen.306q.vce. file.html). See the comments on its page for more infos.
Changes made from original file: - Removed duplicated questions. - Many answers were tested and better explained. - Removed "Exam J" because it had only drag and drop questions and other questions that had drag and drop (see posts from "Certification Forum Moderator - Brandye Barrington" on https:// forums.oracle.com/forums/thread.jspa?messageID=9640688&tstart=0#9640688)
----------------

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;
Correct Answer: D 

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.
Correct Answer: E 

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() {
    }
}

classextends A {
    void a1() {
    }

    void a2() {
    }
}

classextends 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();
Correct Answer: ABDF 

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);
Correct Answer: AE 

QUESTION 5 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 { }
Correct Answer: AC 

QUESTION 6 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 pizzanew 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.
Correct Answer: A 
Explanation/Reference:
Error msg: "Cannot override the final method from Pizza" on the line "public void addTopping (String topping) {" from the class PepperoniPizza

QUESTION 7 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.
Correct Answer: BEF 

QUESTION 8 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;
Correct Answer: DE 

QUESTION 9 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.
Correct Answer: D 

QUESTION 10 Given:

public class Person {
    private String namecomment;
    private int age;

    public Person(String nint a, String c) {
          namen;
          agea;
          commentc;
    }

    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;
Correct Answer: B 

QUESTION 11 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)
Correct Answer: AD 

QUESTION 12 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) {
Correct Answer: BC 

QUESTION 13 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
Correct Answer: D 

QUESTION 14 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.
Correct Answer: AC 

QUESTION 15 Given:
1. import java.util.*;

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]
Correct Answer: B 
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 16 Given:
1. import java.util.*;

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.
Correct Answer: C 

QUESTION 17 Given:
 1. class A { 
2. void foo() throws Exception { 
3. throw new Exception(); 
4. } 
5. }

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
Correct Answer: D 
Explanation/Reference:
Unhandled exception type Exception

QUESTION 18 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
Correct Answer: B 

 QUESTION 19 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(); 
// line 27 } 
catch (Exception e) {
System.out.print("an error occurred"); 
// line 29 }

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.
Correct Answer: BE 

QUESTION 20 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. }
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.
Correct Answer: C 

QUESTION 21 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.
Correct Answer: D 
Explanation/Reference:
Test.java:10: illegal start of expression
case default:
^ Test.java:11: : expected
System.out.print("retriever "); ^ 2 errors

QUESTION 22
 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
Correct Answer: E 

QUESTION 23 Given:
1. public class Breaker2 { 
2. static String o = "";
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.} 

What is the result?
A. 2 
B. 24 
C. 234 
D. 246 
E. 2346 
F. Compilation fails.
 Correct Answer: B 

QUESTION 24 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.
Correct Answer: D 
Explanation/Reference:
The first "else" must be "else if"

QUESTION 25 Given:
1. import java.io.IOException;

3. class A {
5. public void process() { 
6. System.out.print("A,"); 
7. }
9. }
12.class B extends A {
14. public void process() throws IOException { 
15. super.process(); 
16. System.out.print("B,"); 
17. throw new IOException();
18. }

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.
Correct Answer: D 
Explanation/Reference:
Exception IOException is not compatible with throws clause in A.process()
-------------------------
To work, line 5 should have throws IOException (ZZZ)
QUESTION 26 Given:
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.
Correct Answer: D 

QUESTION 27 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

Correct Answer: D 

QUESTION 28 Given:
public class Spock {
    public static void main(String[] args) {
          Long tail = 2000L;
          Long distance = 1999L;
          Long story = 1000L;
          if ((taildistance) ^ ((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.
Correct Answer: E 

eEdit
Must read for you :