OCJO Examen 8
|
|
Título del Test:
![]() OCJO Examen 8 Descripción: Preparacion para certificacion JAVA |



| Comentarios |
|---|
NO HAY REGISTROS |
|
Given: 11.public class Rainbow { 12. public enum MyColor { 13. RED(0xff0000), GREEN(0x00ff00),BLUE(0x0000ff); 14. private final int rgb; 15. MyColor(int rgb) { this.rgb = rgb; } 16. public int getRGB(){ return rgb; } 17. }; 18. public static void main(String[] args) { 19. // insert code here 20. } 21. } Which code fragment, inserted at line 19, allows the Rainbow class to compile?. MyColor skyColor = BLUE;. MyColor treeColor = MyColor.GREEN;. if(RED.getRGB() < BLUE.getRGB()) { }. Compilation fails due to other error(s) in the code. MyColor purple = new MyColor(0xff00ff);. MyColor purple = MyColor.BLUE + MyColor.RED;. Given: 10. class Nav{ 11. public enum Direction { NORTH, SOUTH, EAST, WEST } 12. } 13. public class Sprite{ 14. // insert code here 15. } Which code, inserted at line 14, allows the Sprite class to compile?. Direction d = NORTH;. Nav.Direction d = NORTH;. Direction d = Direction.NORTH;. Nav.Direction d = Nav.Direction.NORTH;. Given: 5. class Atom { 6. Atom() { System.out.print("atom "); } 7. } 8. class Rock extends Atom { 9. Rock(String type) { System.out.print(type); } 10. } 11. public class Mountain extends Rock { 12. Mountain(){ 13. super("granite "); 14. new Rock("granite "); 15. } 16. public static void main(String[] a) { new Mountain(); } 17. } What is the result?. Compilation fails. atom granite. granite granite. atom granite granite. An exception is thrown at runtime. atom granite atom granite. Given: 1. interface TestA { String toString(); } 2. public class Test { 3. public static void main(String[] args) { 4. System.out.println(new TestA() { 5. public String toString() { return "test"; } 6. }); 7. } 8. } What is the result?. test. null. An exception is thrown at runtime. Compilation fails because of an error in line 1. Compilation fails because of an error in line 4. Compilation fails because of an error in line 5. Given: 11. public abstract class Shape { 12. private int x; 13. private int y; 14. public abstract void draw(); 15. public void setAnchor(int x, int y) { 16. this.x = x; 17. this.y = y; 18. } 19. }. public class Circle implements Shape { private int radius; }. public abstract class Circle extends Shape { private int radius; }. public class Circle extends Shape { private int radius; public void draw(); }. public abstract class Circle implements Shape { private int radius; public void draw(); }. public class Circle extends Shape { private int radius; public void draw() {/* code here */} }. public abstract class Circle implements Shape { private int radius; public void draw() { / code here */ } }. Given: 11. class Mud { 12. // insert code here 13. System.out.println("hi"); 14. } 15. } And the following five fragments: public static void main(String...a) { public static void main(String.* a) { public static void main(String... a) { public static void main(String[]... a) { public static void main(String...[] a) { How many of the code fragments, inserted independently at line 12, compile?. 0. 1. 2. 3. 4. 5. Given: 10. class One { 11. void foo() {} 12. } 13. class Two extends One { 14. //insert method here 15. } Which three methods, inserted individually at line 14, will correctly complete class Two? (Choose three.). int foo() { /* more code here */ }. void foo() { /* more code here */ }. public void foo() { /* more code here */ }. private void foo() { /* more code here */ }. protected void foo() { /* more code here */ }. Given: 11. public class Barn { 12. public static void main(String[] args) { 13. new Barn().go("hi", 1); 14. new Barn().go("hi", "world", 2); 15. } 16. public void go(String... y, int x) { 17. System.out.print(y[y.length 1]+ " "); 18. } 19. } What is the result?. hi hi. hi world. world world. Compilation fails. An exception is thrown at runtime. Given: 11. public static void parse(String str) { 12. try { 13. float f= Float.parseFloat(str); 14. } catch (NumberFormatException nfe) { 15. f= 0; 16. } finally { 17. System.out.println(f); 18. } 19. } 20. public static void main(String[] args) { 21. parse(”invalid”); 22. } What is the result?. 0.0. Compilation fails. A ParseException is thrown by the parse method at runtime. A NumberFormatException is thrown by the parse method at runtime. 11. class Person { 12. String name = "No name"; 13. public Person(String nm) { name = nm; } 14. } 15. 16. class Employee extends Person { 17. String emplD = “0000”; 18. public Employee(String id) { empID = id; } 19. } 20. 21. public class EmployeeTest { 22. public static void main(String[] args) { 23. Employee e = new Employee(”4321”); 24. System.out.println(e.empID); 25. } 26. } What is the result?. 4321. 0000. An exception is thrown at runtime. Compilation fails because of an error in line 18. 1. public interface A { 2. public void doSomething(String thing); 3. } 1. public class AImpl implements A { 2. public void doSomething(String msg) { } 3. } 1. public class B { 2. public A doit() { 3. // more code here 4. } 5. 6. public String execute() { 7. // more code here 8. } 9. } 1. public class C extends B { 2. public AImpl doit() { 3. // more code here 4. } 5. 6. public Object execute() { 7. // more code here 8. } 9. } Which statement is true about the classes and interfaces in the exhibit?. Compilation will succeed for all classes and interfaces. Compilation of class C will fail because of an error in line 2. Compilation of class C will fail because of an error in line 6. Compilation of class AImpl will fail because of an error in line 2. Given: 33. try { 34. // some code here 35. } catch (NullPointerException e1) { 36. System.out.print(”a”); 37. } catch (RuntimeException e2) { 38. System.out.print(”b”); 39. } finally { 40. System.out.print(”c”); 41. }. c. a. ab. ac. bc. abc. Given: 1. public class Boxer1 { 2. Integer i; 3. int x; 4. public Boxer1(int y) { 5. x=i+y; 6. System.out.println(x); 7. } 8. public static void main(String[] args) { 9. new Boxer1(new Integer(4)); 10. } 11. } What is the result?. The value “4” is printed at the command line. Compilation fails because of an error in line 5. Compilation fails because of an error in line 9. A NullPointerException occurs at runtime. A NumberFormatException occurs at runtime. An IllegalStateException occurs at runtime. Given: 10.int x=0; 11.int y = 10; 12. do { l3. y--; 14. ++x; 15. } while (x < 5); 16. System.out.print(x + “,“ + y); What is the result?. 5,6. 5,5. 6,5. 6,6. Given: 11. public class Test { 12. public static void main(String [] args) { 13. int x =5; 14. boolean b1 = true; 15. boolean b2 = false; 16. 17.if((x==4) && !b2) 18. System.out.print(”1“); 19. System.out.print(”2 “); 20. if ((b2 = true) && b1) 21. System.out.print(”3 “); 22. } 23. } What is the result?. 2. 3. 1 2. 2 3. 1 2 3. Compilation fails. An exceptional is thrown at runtime. 4. Given: 31. // some code here 32. try { 33. // some code here 34. } catch (SomeException se) { 35. // some code here 36. } finally { 37. // some code here 38. } Under which three circunstances will the code on line 37 be executed? (Choose three.). The instance gets garbage collected. The code on line 33 throws an exception. The code on line 35 throws an exception. The code on line 31 throws an exception. The code on line 33 executes successfully. 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?. Alpha a = x;. Foo f= (Delta)x;. Foo f= (Alpha)x;. Beta b = (Beta)(Alpha)x;. 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 3 14.159,26?. b = nf.parse( input);. b = nf.format( input);. b = nf.equals( input);. b = nf.parseObject( input);. Given: 1. public class LineUp{ 2. public static void main(String[] args){ 3. double d = 12.345; 4. //insert code here 5. } 6. } Which code fragment, inserted at line 4, produces the output 12.345?. System.out.printf("%7d \n", d);. System.out.printf("%7f \n", d);. System.out.printf("%3.7d \n", d);. System.out.printf("%3.7f \n", d);. System.out.printf("%7.3d \n", d);. System.out.printf("%7.3f \n", d);. 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?. Compilation fails. The file system has a new empty directory named dir. The file system has a new empty directory named newDir. The file system has a directory named dir, containing a file f1.txt. The file system has a directory named newDir, containing a file f1.txt. RuntimeException occurs. |





