310-065 - Sun Certified Programmer for the Java Platform, Standard Edition 6

Go back to Oracle

Example Questions

Given: package test; class Target { public String name = "hello"; } What can directly access and change the value of the variable name? 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? public class Threads1 { int x = 0; public class Runner implements Runnable { public void run(){ int current = 0; for(int i = 0; i<4; i++){ current = x; System.out.println(current + ", "); x = current + 2; } } } public static void main(String[] args) { new Threads1().go(); } public void go(){ Runnable r1 = new Runner(); new Thread(r1).start(); new Thread(r1).start(); } } Which two are possible results? (Choose two.) 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? Given: import java.util.*; public class Example { public static void main(String[] args) { // insert code here set.add(new Integer(2)); set.add(new Integer(1)); System.out.println(set); } } Which code, inserted at line 4, guarantees that this program will output [1, 2]? Given: 23. Object [] myObjects = { 24. new Integer(12), 25. new String("foo"), 26. new Integer(5), 27. new Boolean(true) 28. }; 29. Arrays.sort(myObjects); 30. for(int i=0; i<myObjects.length; i++) { 31. System.out.print(myObjects[i].toString()); 32. System.out.print(" "); 33. } What is the result? Given that the current directory is empty, and that the user has read and write privileges to the current directory, and the following: import java.io.*; public class Maker { public static void main(String[] args) { File dir = new File("dir"); File f = new File(dir, "f"); } } Which statement is true? Which two scenarios are NOT safe to replace a StringBuffer object with a StringBuilder object? (Choose two.) 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? Given: try { //some code here line 34 } catch (NullPointerException e1) { System.out.print("a"); } catch (Exception e2) { System.out.print("b"); } finally { System.out.print("c"); } If some sort of exception is thrown at line 34, which output is possible? 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? Given two files, GrizzlyBear.java and Salmon.java: package animals.mammals; public class GrizzlyBear extends Bear { void hunt() { Salmon s = findSalmon(); s.consume(); } } package animals.fish; public class Salmon extends Fish { public void consume() { /* do stuff */ } } 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: 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.) Given: //some code here line 31 try { //some code here line 33 } catch (NullPointerException e1) { //some code here line 35 } finally { //some code here line 37 } Under which three circumstances will the code on line 37 be executed? (Choose three.) Given: public class Score implements Comparable<Score> { private int wins, losses; public Score(int w, int l) { wins = w; losses = l; } public int getWins() { return wins; } public int getLosses() { return losses; } public String toString() { return "<" + wins + "," + losses + ">"; } // insert code here } Which method will complete this class? 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 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 5 in class SomeApp to use the process method of BitUtils? Which capability exists only in java.io.FileWriter? Given: public class Threads3 implements Runnable { public void run() { System.out.print("running"); } public static void main(String[] args) { Thread t = new Thread(new Threads3()); t.run(); t.run(); t.start(); } } What is the result? 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? Given: Runnable r = new Runnable() { public void run() { System.out.print("Cat"); } }; Thread t = new Thread(r) { public void run() { System.out.print("Dog"); } }; t.start(); What is the result? 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? 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: 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 PingPong implements Runnable { synchronized void hit(long n) { for (int i = 1; i < 3; i++) System.out.print(n + "-" + i + " "); } public static void main(String[] args) { new Thread(new PingPong()).start(); new Thread(new PingPong()).start(); } public void run() { hit(Thread.currentThread().getId()); } } Which two statements are true? (Choose two.) Given: public class Threads5 { public static void main (String[] args) { new Thread(new Runnable() { public void run() { System.out.print("bar"); }}).start(); } } What is the result? 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. abstract class Vehicle { public int speed() { return 0; } 12. class Car extends Vehicle { public int speed() { return 60; } 13. class RaceCar extends Car { public int speed() { return 150; } ... 21. RaceCar racer = new RaceCar(); 22. Car car = new RaceCar(); 23 Vehicle vehicle = new RaceCar(); 24 System.out.println(racer.speed() + ", " + car.speed() + ", " + vehicle.speed()); What is the result? Which two code fragments correctly create and initialize a static array of int elements? (Choose two.) Given: class Line { public class Point { public int x, y; } public Point getPoint() { return new Point(); } } class Triangle { public Triangle() { // insert code here } } Which code, inserted at line 16, correctly retrieves a local instance of a Point object? 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? public class A { public String doit(int x, int y){ return "a"; } public String doit(int... vals){ return "b"; } } Given: 25. A a = new A(); 26. System.out.println(a.doit(4, 5)); What is the result? Which can appropriately be thrown by a programmer using Java SE technology to create a desktop application? 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: 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: 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.) A programmer must create a generic class MinMax and the type parameter of MinMax must implement Comparable. Which implementation of MinMax will compile? 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? Given: import java.util.Date; import java.text.DateFormat; DateFormat df; Date date = new Date(); //insert code here Linea 23 String s = df.format(date); Which code fragment, inserted at line 23, allows the code to compile? Given: HashMap props = new HashMap(); props.put("key45", "some value"); props.put("key12", "some other value"); props.put("key39", "yet another value"); Set s = props.keySet(); //insert code here What, inserted at line 39, will sort the keys in the props HashMap? Given: public class Threads4 { public static void main (String[] args) { new Threads4().go(); } public void go() { Runnable r = new Runnable() { public void run() { System.out.print("foo"); } }; Thread t = new Thread(r); t.start(); t.start(); } } What is the result? Given: class TestA { public void start() { System.out.println("TestA"); } } public class TestB extends TestA { public void start() { System.out.println("TestB"); } public static void main(String[] args) { ((TestA)new TestB()).start(); } } What is the result? Which three statements concerning the use of the java.io.Serializable interface are true? (Choose three.) 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: class A { void foo() throws Exception { throw new Exception(); } } class SubB2 extends A { void foo() { System.out.println("B "); } } class Tester { public static void main(String[] args) { A a = new SubB2(); a.foo(); } } What is the result? 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? Given: class Nav{ public enum Direction { NORTH, SOUTH, EAST, WEST } } public class Sprite{ // insert code here } Which code, inserted at line 14, allows the Sprite class to compile? Given: class One { void foo() { } } class Two extends One { 14. // insert method here } Which three methods, inserted individually at line 14, will correctly complete class Two? (Choose three.) 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? 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?

Study Guides