1Z0-851 - Customize your Java Standard Edition 6 Programmer Certified Professional Exam certification experience.

Go back to Oracle

Exam FAQ

How many questions are there on the 1Z0-851 exam? How long is the 1Z0-851 exam? How much does the 1Z0-851 exam cost? What is the best way to study for the 1Z0-851 exam?

Example Questions

Given classes defined in two different files: package packageA; public class Message { String getText() { return "text"; } } And: package packageB; public class XMLMessage extends packageA.Message { String getText() { return "<msg>text</msg>"; } public static void main(String[] args) { System.out.println(new XMLMessage().getText()); } } What is the result of executing XMLMessage.main? 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.) Given: 11. public static void main(String[] args) { 12. Object obj = new int[] { 1, 2, 3 }; 13. int[] someArray = (int[])obj; 14. for (int i : someArray) System.out.print(i + " "); 15. } What is the result? 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? Given: 11. class Converter { 12. public static void main(String[] args) { 13. Integer i = args[0]; 14. int j = 12; 15. System.out.println("It is " + (j==i) + " that j==i."); 16. } 17. } What is the result when the programmer attempts to compile the code and run it with the command java Converter 12? Given: enum Example { ONE, TWO, THREE } Which statement is true? Given: 11. interface DeclareStuff { 12. public static final int EASY = 3; 13. void doStuff(int t); } 14. public class TestDeclare implements DeclareStuff { 15. public static void main(String [] args) { 16. int x = 5; 17. new TestDeclare().doStuff(++x); 18. } 19. void doStuff(int s) { 20. s += EASY + ++s; 21. System.out.println("s " + s); 22. } 23. } What is the result? Given a valid DateFormat object named df, and 16. Date d = new Date(0L); 17. String ds = "December 15, 2004"; 18. // insert code here What updates d's value with the date represented by ds? Given: 5. class Building { } 6. public class Barn extends Building { 7. public static void main(String[] args) { 8. Building build1 = new Building(); 9. Barn barn1 = new Barn(); 10. Barn barn2 = (Barn) build1; 11. Object obj1 = (Object) build1; 12. String str1 = (String) build1; 13. Building build2 = (Building) barn1; 14. } 15. } Which is true? Given that the current directory is empty, and that the user has read and write permissions, and the following: import java.io.*; public class DOS { public static void main(String[] args) { File dir = new File("dir"); dir.mkdir(); File f1 = new File(dir, "f1.txt"); try { f1.createNewFile(); } catch (IOException e) { ; } File newDir = new File("newDir"); dir.renameTo(newDir); } } Which statement is true? Given: public class Donkey { public static void main(String[] args) { boolean assertsOn = false; assert (assertsOn) : assertsOn = true; 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? Viewpublic class A { public void method1(){ B b = new B(); b.method2(); // more code here } } public class B{ public void method2() { C c = new C(); c.method3(); // more code here } } public class C { public void method3(){ // more code here } } Given: 25. try { 26. A a = new A(); 27. a.method1(); 28. } catch (Exception e) { 29. System.out.print("an error occurred"); 30. } Which two statements are true if a NullPointerException is thrown on line 3 of class C? (Choose two.) Which can appropriately be thrown by a programmer using Java SE technology to create a desktop application? Given: interface Foo {} class Alpha implements Foo {} class Beta extends Alpha {} class Delta extends Beta { public static void main( String[] args ) { Beta x = new Beta(); 16. //insert code here 16 } } Which code, inserted at line 16, will cause a java.lang.ClassCastException? Given: 33. try { 34. //some code here 35. } catch (NullPointerException e1) { 36. System.out.print("a"); 37. } catch (Exception e2) { 38. System.out.print("b"); 39. } finally { 40. System.out.print("c"); 41. } If some sort of exception is thrown at line 34, which output is possible? Given: public class Plant { private String name; public Plant(String name) { this.name = name; } public String getName() { return name; } } public class Tree extends Plant { public void growFruit() { } public void dropLeaves() { } } Which statement is true? Given: 05. class A { 06. void foo() throws Exception { throw new Exception(); } 07. } 08. class SubB2 extends A { 09. 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? Given: 10. interface Foo {} 11. class Alpha implements Foo {} 12. class Beta extends Alpha {} 13. class Delta extends Beta { 14. public static void main( String[] args ) { 15. Beta x = new Beta(); 16. //insert code here 17. } 18. } Which code, inserted at line 16, will cause a java.lang.ClassCastException? Given: public class Mule { public static void main(String[] args) { boolean assert = true; if(assert) { System.out.println("assert is true"); } } } Which command-line invocations will compile? Given that the current directory is empty, and that the user has read and write permissions, and the following: 11. import java.io.*; 12. public class DOS { 13. public static void main(String[] args) { 14. File dir = new File("dir"); 15. dir.mkdir(); 16. File f1 = new File(dir, "f1.txt"); 17. try { 18. f1.createNewFile(); 19. } catch (IOException e) { ; } 20. File newDir = new File("newDir"); 21. dir.renameTo(newDir); 22. } 23. } Which statement is true? Which three statements concerning the use of the java.io.Serializable interface are true? (Choose three.) Given: interface DeclareStuff { public static final int EASY = 3; void doStuff(int t); } public class TestDeclare implements DeclareStuff { public static void main(String[] args) { int x = 5; new TestDeclare().doStuff(++x); } void doStuff(int s) { s += EASY + ++s; System.out.println("s " + s); } } What is the result? 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? Given class Foo { static void alpha() { /* more code here */ } void beta() { /* more code here */ } } Which two statements are true? (Choose two.) Given: public class A { public void doit() { } public String doit() { return "a"; } public double doit(int x) { return 1.0; } } What is the result? Given that Triangle implements Runnable, and: 31. void go() throws Exception { 32. Thread t = new Thread(new Triangle()); 33. t.start(); 34. for(int x = 1; x < 100000; x++) { 35. //insert code here 36. if(x%100 == 0) System.out.print("g"); 37. } } 38. public void run() { 39. try { 40. for(int x = 1; x < 100000; x++) { 41. // insert the same code here 42. if(x%100 == 0) System.out.print("t"); 43. } 44. } catch (Exception e) { } 45. } 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.) 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? Given: 11. public class Person { 12. private String name, comment; 13. private int age; 14. public Person(String n, int a, String c) { 15. name = n; age = a; comment = c; 16. } 17. public boolean equals(Object o) { 18. if (! (o instanceof Person)) return false; 19, Person p = (Person)o; 20. return age == p.age && name.equals(p.name); 21. } 22. } What is the appropriate definition of the hashCode method in class Person? Given: public interface A { public void m1(); } class B implements A { } class C implements A { public void m1() { } } class D implements A { public void m1(int x) { } } abstract class E implements A { } abstract class F implements A { public void m1() { } } abstract class G implements A { public void m1(int x) { } } What is the result? Viewpublic class A { public void method1(){ B b = new B(); b.method2(); // more code here } } public class B{ public void method2() { C c = new C(); c.method3(); // more code here } } public class C { public void method3(){ // more code here } } 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.) 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.) Given: 35. String #name = "Jane Doe"; 36. int $age = 24; 37. Double _height = 123.5; 38. double ~temp = 37.5; Which two statements are true? (Choose two.) 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? 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? Given: Float pi = new Float(3.14f); if (pi > 3) { System.out.print("pi is bigger than 3. "); } else { System.out.print("pi is not bigger than 3. "); } finally { System.out.println("Have a nice day."); } What is the result? Which two statements are true about the hashCode method? (Choose two.) Given: public class TestFive { private int x; public void foo() { int current = x; x = current + 1; } public void go() { for(int i = 0; i < 5; i++) { new Thread() { public void run() { foo(); System.out.print(x + ", "); } }.start(); } } } Which two changes, taken together, would guarantee the output: 1, 2, 3, 4, 5, ? (Choose two.) Given classes defined in two different files: package util; public class BitUtils { public static void process(byte[] b) { /* more code here */ } } package app; public class SomeApp { public static void main(String[] args) { byte[] bytes = new byte[256]; // insert code here } } What is required at line 6 in class SomeApp to use the process method of BitUtils? 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? 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? public class A{ private int counter = 0; public static int getInstanceCount() { return counter; } public A() { counter++; } } Given: 11. public static void main(String[] args) { 12. try { 13. args = null; 14. args[0] = "test"; 15. System.out.println(args[0]); 16. } catch (Exception ex) { 17. System.out.println("Exception"); 18. } catch (NullPointerException npe) { 19. System.out.println("NullPointerException"); 20. } 21. } What is the result? Given: String[] elements = { "for", "tea", "too" }; String first = (elements.length > 0) ? elements[0] : null; What is the result? Given the following directory structure: bigProject |--source | |--Utils.java | |--classes And the following command line invocation: javac -d classes source/Utils.java Assume the current directory is bigProject, what is the result? Given: import java.io.IOException; class A { public void process() { System.out.print("A,"); } } 13. class B extends A { 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? Given two files, GrizzlyBear.java and Salmon.java: 01. package animals.mammals; 02. 03. public class GrizzlyBear extends Bear { 04. void hunt() { 05. Salmon s = findSalmon(); 06. s.consume(); 07. } 08. } 01. package animals.fish; 02. 03. public class Salmon extends Fish { 04. public void consume() { /* do stuff */ } 05. } If both classes are in the correct directories for their packages, and the Mammal class correctly defines the findSalmon() method, which change allows this code to compile? 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? Given: 1. package test; 2. 3. class Target { 4. public String name = "hello"; 5. } What can directly access and change the value of the variable name? Given: class One { public One foo() { return this; } } class Two extends One { public One foo() { return this; } } class Three extends Two { // insert method here } Which two methods, inserted individually, correctly complete the Three class? (Choose two.) 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.) Which code, inserted at line 14, will allow this class to correctly serialize and deserialize? 01. import java.io.*; 02. public class Foo implements Serializable { 03. public int x, y; 04. public Foo(int x, int y){ 05. this.x = x; this.y = y; 06. } 07. 08. private void writeObject(ObjectOutputStream s) 09. throws IOException{ 10. s.writeInt(x); s.writeInt(y); 11. } 12. 13. private void readObject(ObjectInputStream s) 14. throws IOException, ClassNotFoundException { 15. //insert code here 16. } 17. }

Study Guides