SCJP / OCJP dumps 1 - Exam D




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 D
QUESTION 1 Given: 
interface Foo { int bar(); } 
public class Sprite {
public int fubar( Foo foo ) { return foo.bar(); } 
public void testFoo() {
fubar(
//insert code here 15 ); } } 

Which code, inserted at line 15, allows the class Sprite to compile?

A. Foo { public int bar() { return 1; } 
B. new Foo { public int bar() { return 1; } 
C. new Foo() { public int bar() { return 1; } 
D. new class Foo { public int bar() { return 1; }
Correct Answer: C 

QUESTION 2 Given:
11. public enum Title { 
12. MR("Mr."), MRS("Mrs."), MS("Ms."); 
13. private final String title; 
14. private Title(String t) { title = t; } 
15. public String format(String last, String first) { 
16. return title + " " + first + " " + last; 
17. } 
18. }
public static void main(String[] args) {
System.out.println(Title.MR.format("Doe", "John")); }

What is the result?
A. Mr. John Doe 
B. An exception is thrown at runtime. 
C. Compilation fails because of an error in line 12. 
D. Compilation fails because of an error in line 15. 
E. Compilation fails because of an error in line 20.
Correct Answer: A 

QUESTION 3 Given the following six method names:
addListener
registerMouseListener
How many of these method names follow JavaBean Listener naming rules?
A. 1 
B. 2 
C. 3 
D. 4 
E. 5
Correct Answer: B 
Explanation/Reference:
addMouseListener and removeMouseListener

QUESTION 4 Given: class Line {
public static class Point {} }
class Triangle {
public Triangle(){
// insert code here } } 
Which code, inserted at line 15, creates an instance of the Point class defined in Line?

A. Point p = new Point(); 
B. Line.Point p = new Line.Point(); 
C. The Point class cannot be instatiated at line 15. 
D. Line l = new Line() ; l.Point p = new l.Point();
Correct Answer: B 

QUESTION 5 Given 
11. public interface Status { 
12. /* insert code here */ int MY_VALUE = 10; 
13. } Which three are valid on line 12? (Choose three.)
A. final 
B. static 
C. native 
D. public 
E. private 
F. abstract
addMouseListener
setMouseListener
deleteMouseListener
removeMouseListener
G. protected
Correct Answer: ABD 

QUESTION 6 Click the Exhibit button. Given this code from Class B:
25. A a1 = new A(); 
26. A a2 = new A(); 
27. A a3 = new A(); 
28. System.out.println(A.getInstanceCount());
What is the result?
1. public class A{ 
2. 
3. private int counter = 0; 
4. 
5. public static int getInstanceCount() { 
6. return counter; 
7. } 
8. 
9. public A() { 
10. counter++; 11. } 
12. 
13.}

A. Compilation of class A fails. 
B. Line 28 prints the value 3 to System.out. 
C. Line 28 prints the value 1 to System.out. 
D. A runtime error occurs when line 25 executes. 
E. Compilation fails because of an error on line 28.
Correct Answer: A 

Error in line 6: "Cannot make a static reference to the non-static field counter"

QUESTION 7 Given classes defined in two different files: 
1. package util; 
2. public class BitUtils { 
3. public static void process(byte[] b) { /* more code here */ }
4. }
1. package app; 
2. public class SomeApp {
 3. public static void main(String[] args) { 
4. byte[] bytes = new byte[256]; 
5. // insert code here 
6. } 
7. } What is required at line 5 in class SomeApp to use the process method of BitUtils?
A. process(bytes); 
B. BitUtils.process(bytes); 
C. util.BitUtils.process(bytes); 
D. SomeApp cannot use methods in BitUtils. 
E. import util.BitUtils.*; process(bytes);
Correct Answer: C 

QUESTION 8 Click the Exhibit button. 
Which three code fragments, added individually at line 29, produce the output 100? (Choose three.)
class Inner {
private int x; 
public void setX( int x ){ this.x = x; } 
public int getX(){ return x;} 
}
class Outer {
private Inner y; 
public void setY( Inner y ){ this.y = y; } 
public Inner getY() { return y; } 
}
public class Gamma {
public static void main(String[] args) {
Outer o = new Outer(); 
Inner i = new Inner(); 
int n = 10; 
i.setX(n); 
o.setY(i); // insert code here 29 
System.out.println(o.getY().getX());
}
A. n = 100; 
B. i.setX( 100 ); 
C. o.getY().setX( 100 ); 
D. i = new Inner(); i.setX( 100 ); 
E. o.setY( i ); i = new Inner(); i.setX( 100 ); 
F. i = new Inner(); i.setX( 100 ); o.setY( i );
Correct Answer: BCF 

QUESTION 9 Given:
class Snoochy {
Boochy booch;
public Snoochy() { booch = new Boochy(this); } }
class Boochy {
Snoochy snooch; public Boochy(Snoochy s) { snooch = s; } }
And the statements:
public static void main(String[] args) {
Snoochy snoog = new Snoochy(); snoog = null; //Línea 23 // more code here }

Which statement is true about the objects referenced by snoog, snooch, and booch immediately after line 23 executes?

A. None of these objects are eligible for garbage collection. 
B. Only the object referenced by booch is eligible for garbage collection. 
C. Only the object referenced by snoog is eligible for garbage collection. 
D. Only the object referenced by snooch is eligible for garbage collection. 
E. The objects referenced by snooch and booch are eligible for garbage collection.
Correct Answer: E 

QUESTION 10 Given:
class Payload {
private int weight; 
public Payload (int w) { weight = w; } 
public void setWeight(int w) { weight = w; } 
public String toString() { return Integer.toString(weight); } 

public class TestPayload {
static void changePayload(Payload p) { /* insert code */ } //Línea 12 
public static void main(String[] args) {
Payload p = new Payload(200); 
p.setWeight(1024); 
changePayload(p); 
System.out.println("p is " + p); 
}
Which code fragment, inserted at the end of line 12, produces the output p is 420?
A. p.setWeight(420); 
B. p.changePayload(420); 
C. p = new Payload(420); 
D. Payload.setWeight(420); 
E. p = Payload.setWeight(420);
Correct Answer: A 

QUESTION 11 Given:
public static void test(String str) {
int check = 4; 
if (check = str.length()) {
System.out.print(str.charAt(check -= 1) +", "); 
else {
System.out.print(str.charAt(0) + ", "); 
}
and the invocation:
test("four"); test("tee"); test("to");
What is the result?
A. r, t, t, 
B. r, e, o, 
C. Compilation fails.
D. An exception is thrown at runtime.
Correct Answer: C 
Explanation/Reference:
It gives the error "Type mismatch: cannot convert from int to boolean" on the line with the if command because of the = instead of ==

QUESTION 12 Given classes defined in two different files:
package util; 
public class BitUtils {
private static void process(byte[] b) {} 

package app; 
public class SomeApp {
public static void main(String[] args) {
byte[] bytes = new byte[256]; // insert code here Linea 5
}
}
What is required at line 5 in class SomeApp to use the process method of BitUtils?
A. process(bytes); 
B. BitUtils.process(bytes); 
C. app.BitUtils.process(bytes); 
D. util.BitUtils.process(bytes); E. import util.BitUtils.*; process(bytes); 
F. SomeApp cannot use the process method in BitUtils.
Correct Answer: F 
Explanation/Reference:
Indeed because the process methos in BitUtils class is private

QUESTION 13 Given:

public class Pass2 {
    public void main(String[] args) {
          int x = 6;
          Pass2 pnew Pass2();
          p.doStuff(x);
          System.out.print(" main x = "x);
    }

    void doStuff(int x) {
          System.out.print(" doStuff x = "x++);
    }
}


And the command-line invocations:
javac Pass2.java java Pass2 5
What is the result?
A. Compilation fails. 
B. An exception is thrown at runtime. 
C. doStuff x = 6 main x = 6 
D. doStuff x = 6 main x = 7 
E. doStuff x = 7 main x = 6 
F. doStuff x = 7 main x = 7
Correct Answer: B 
Explanation/Reference:
It gives the error "Exception in thread "main" java.lang.NoSuchMethodError: main" because the main
method isn't static

QUESTION 14 Given:

public class Test {
    public enum Dogs {
          collieharrier
    };

    public static void main(String[] args) {
          Dogs myDog = Dogs.collie;
          switch (myDog) {
          case collie:
                 System.out.print("collie ");
          case harrier:
                 System.out.print("harrier ");
          }
    }
}


What is the result?
A. collie 
B. harrier 
C. Compilation fails. 
D. collie harrier 
E. An exception is thrown at runtime.
Correct Answer: D 
Explanation/Reference:
Tested and confirmed

QUESTION 15 Given:
public class Donkey {
    public static void main(String[] args) {
          boolean assertsOnfalse;
          assert (assertsOn) : assertsOntrue;
          if (assertsOn) {
                 System.out.println("assert is on");
          }
    }
}


If class Donkey is invoked twice, the first time without assertions enabled, and the second time with assertions enabled, what are the results?
A. no output 
B. no output
assert is on 
C. assert is on 
D. no output
An AssertionError is thrown. 
E. assert is on
An AssertionError is thrown.
Correct Answer: D 

QUESTION 16 Given: 



    static void test() {
          try {
                 String xnull;
                 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 
Explanation/Reference:
x.toString() throws an exception because x is null

QUESTION 17 Given: 
static void test() throws Error {
if (true) throw new AssertionError(); 
System.out.print("test "); } 
public static void main(String[] args) {
try { test(); } 
catch (Exception ex) { System.out.print("exception "); } 
System.out.print("end "); 
}
}

What is the result?
A. end 
B. Compilation fails. 
C. exception end 
D. exception test end 
E. A Throwable is thrown by main. 
F. An Exception is thrown by main.
 Correct Answer: E 

QUESTION 18 Given:

class TestException extends Exception {
}

class A {
    public String sayHello(String namethrows TestException {
          if (name == null)
                 throw new TestException();
          return "Hello "name;
    }
}

public class TestA {
    public static void main(String[] args) {
          new A().sayHello("Aiko");
    }
}


Which statement is true?
A. Compilation succeeds. 
B. Class A does not compile. 
C. The method declared on line 9 cannot be modified to throw TestException. 
D. TestA compiles if line 10 is enclosed in a try/catch block that catches TestException.
Correct Answer: D 

QUESTION 19 Given:
public static Collection get() {
Collection sorted = new LinkedList(); sorted.add("B"); sorted.add("C"); sorted.add("A"); 
return sorted; 
public static void main(String[] args) {
for (Object obj: get()) {
System.out.print(obj + ", "); 
}
What is the result?
A. A, B, C, 
B. B, C, A, 
C. Compilation fails. 
D. The code runs with no output. 
E. An exception is thrown at runtime.
Correct Answer: B 

QUESTION 20 Given: 

static class A {
    void process() throws Exception {
          throw new Exception();
    }
}

static class B extends A {
    void process() {
          System.out.println("B");
    }

    
    public static void main(String[] args) {
          new B().process();
}
}

What is the result?
A. B 
B. The code runs with no output. 
C. Compilation fails because of an error in line 12. 
D. Compilation fails because of an error in line 15. 
E. Compilation fails because of an error in line 18.
Correct Answer: A 

QUESTION 21 Given: 
public class Foo {
    static int[] a;
    static {
          a[0] = 2;
    }

    public static void main(String[] args) {
    }
}

Which exception or error will be thrown when a programmer attempts to run this code?
A. java.lang.StackOverflowError 
B. java.lang.IllegalStateException 
C. java.lang.ExceptionInInitializerError 
D. java.lang.ArrayIndexOutOfBoundsException
Correct Answer: C 

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

QUESTION 23 Given:
public static Iterator reverse(List list) {
Collections.reverse(list); 
return list.iterator(); 
public static void main(String[] args) {
List list = new ArrayList(); 
list.add("1"); 
list.add("2"); 
list.add("3"); 
for (Object obj: reverse(list))
System.out.print(obj + ", "); 
}

What is the result?
A. 3, 2, 1, 
B. 1, 2, 3, 
C. Compilation fails. 
D. The code runs with no output. 
E. An exception is thrown at runtime.
Correct Answer: C 
Explanation/Reference:
In the line of the for it gives the compilation error "Can only iterate over an array or an instance of java.lang. Iterable" for the text "reverse(list)"

QUESTION 24 Given:
1. public class TestString3 { 
2. public static void main(String[] args) { 
3. // insert code here 
4. 
5. System.out.println(s); 
6. } 
7. }
Which two code fragments, inserted independently at line 3, generate the output 4247? (Choose two.)
A. String s = "123456789";
s = (s-"123").replace(1,3,"24") - "89"; 
B. StringBuffer s = new StringBuffer("123456789"); 
C. s.delete(0,3).replace(1,3,"24").delete(4,6); 
D. StringBuffer s = new StringBuffer("123456789"); E. substring(3,6).delete(1,3).insert(1, "24"); 
F. StringBuilder s = new StringBuilder("123456789"); 
G. substring(3,6).delete(1,2).insert(1, "24"); 
H. StringBuilder s = new StringBuilder("123456789"); 
I. delete(0,3).delete(1,3).delete(2,5).insert(1, "24");
Correct Answer: BC 

QUESTION 25 Given:
1. d is a valid, non-null Date object 
2. df is a valid, non-null DateFormat object set to the current locale
What outputs the current locale's country name and the appropriate version of d's date?
A. Locale loc = Locale.getLocale();
System.out.println(loc.getDisplayCountry()
+ " " + df.format(d)); 
B. Locale loc = Locale.getDefault();
System.out.println(loc.getDisplayCountry()
+ " " + df.format(d)); 
C. Locale loc = Locale.getLocale();
System.out.println(loc.getDisplayCountry()
+ " " + df.setDateFormat(d)); 
D. Locale loc = Locale.getDefault();
System.out.println(loc.getDisplayCountry()
+ " " + df.setDateFormat(d));
eEdit
Must read for you :