Ocjp Mock Jgi - II

63 Questions | Total Attempts: 70

SettingsSettingsSettings
Please wait...
Ocjp Mock Jgi - II


Questions and Answers
  • 1. 
    Given: 12. import java.io.*; 13. public class Forest implements Serializable { 14. private Tree tree = new Tree(); 15. public static void main(String [] args) { 16. Forest f = new Forest(); 17. try { 18. FileOutputStream fs = new FileOutputStream("Forest.ser"); 19. ObjectOutputStream os = new ObjectOutputStream(fs); 20. os.writeObject(f); os.close(); 21. } catch (Exception ex) { ex.printStackTrace(); } 22. } } 23. 24. class Tree { } What is the result?
    • A. 

      Compilation fails.

    • B. 

      An exception is thrown at runtime.

    • C. 

      An instance of Forest is serialized.

    • D. 

      An instance of Forest and an instance of Tree are both serialized.

  • 2. 
    Which capability exists only in java.io.FileWriter?
    • A. 

      Closing an open stream.

    • B. 

      Flushing an open stream.

    • C. 

      Writing to an open stream.

    • D. 

      Writing a line separator to an open stream.

  • 3. 
    Given: 5. import java.io.*; 6. public class Talk { 7. public static void main(String[] args) { 8. Console c = new Console(); 9. String pw; 10. System.out.print("password: "); 11. pw = c.readLine(); 12. System.out.println("got " + pw); 13. } 14. } If the user types the password aiko when prompted, what is the result?
    • A. 

      Password: got

    • B. 

      Password: got aiko

    • C. 

      Password: aiko got aiko

    • D. 

      An exception is thrown at runtime.

    • E. 

      Compilation fails due to an error on line 8.

  • 4. 
    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|?
    • A. 

      System.out.printf("|%7d| \n", d);

    • B. 

      System.out.printf("|%7f| \n", d);

    • C. 

      System.out.printf("|%3.7d| \n", d);

    • D. 

      System.out.printf("|%3.7f| \n", d);

    • E. 

      System.out.printf("|%7.3d| \n", d);

    • F. 

      System.out.printf("|%7.3f| \n", d);

  • 5. 
    Given: 11. String test = "Test A. Test B. Test C."; 12. // insert code here 13. String[] result = test.split(regex); Which regular expression, inserted at line 12, correctly splits test into "Test A", "Test B", and "Test C"?
    • A. 

      String regex = "";

    • B. 

      String regex = " ";

    • C. 

      String regex = ".*";

    • D. 

      String regex = "\\s";

    • E. 

      String regex = "\\.\\s*";

    • F. 

      String regex = "\\w[ \.] +";

  • 6. 
    Given: 1. interface A { public void aMethod(); } 2. interface B { public void bMethod(); } 3. interface C extends A,B { public void cMethod(); } 4. class D implements B { 5. public void bMethod(){} 6. } 7. class E extends D implements C { 8. public void aMethod(){} 9. public void bMethod(){} 10. public void cMethod(){} 11. } What is the result?
    • A. 

      Compilation fails because of an error in line 3.

    • B. 

      Compilation fails because of an error in line 7.

    • C. 

      Compilation fails because of an error in line 9.

    • D. 

      If you define D e = new E(), then e.bMethod() invokes the version of bMethod() defined in Line 5.

    • E. 

      If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 5.

    • F. 

      If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 9.

  • 7. 
    What is the result ? 1.   public class SimplCalc { 2.      public int value; 3.      public void calculate() { value += 7; } 4.   } And: 1.   public class MultiCalc extends SimpleCalc { 2.      public void calculate() { value -=3; } 3.      public void calculate(int multiplier) { 4.        calculate(); 5.        super.calculate(); 6.        value *= multiplier; 7.      } 8.      public static void main(String[] args) { 9.        MultiCalc calculator = new MultiCalc(); 10.       calculator.calculate(2); 11.       System.out.println("Value is: "+calculator.value); 12.     } 13.  }
    • A. 

      Value is: 8

    • B. 

      Compilation fails.

    • C. 

      Value is: 12

    • D. 

      Value is: -12

    • E. 

      The code runs with no output.

    • F. 

      An exception is thrown at runtime.

  • 8. 
    Given: 1. public class Base { 2. public static final String FOO = "foo"; 3. public static void main(String[] args) { 4. Base b = new Base(); 5. Sub s = new Sub(); 6. System.out.print(Base.FOO); 7. System.out.print(Sub.FOO); 8. System.out.print(b.FOO); 9. System.out.print(s.FOO); 10. System.out.print(((Base)s).FOO); 11. } } 12. class Sub extends Base {public static final String FOO="bar";} What is the result?
    • A. 

      Foofoofoofoofoo

    • B. 

      Foobarfoobarbar

    • C. 

      Foobarfoofoofoo

    • D. 

      Foobarfoobarfoo

    • E. 

      Barbarbarbarbar

    • F. 

      Foofoofoobarbar

    • G. 

      Foofoofoobarfoo

  • 9. 
    Given: 11. class Mammal { } 12. 13. class Raccoon extends Mammal { 14. Mammal m = new Mammal(); 15. } 16. 17. class BabyRaccoon extends Mammal { } Which four statements are true? (Choose four.)
    • A. 

      Raccoon is-a Mammal.

    • B. 

      Raccoon has-a Mammal.

    • C. 

      BabyRaccoon is-a Mammal.

    • D. 

      BabyRaccoon is-a Raccoon.

    • E. 

      BabyRaccoon has-a Mammal.

    • F. 

      BabyRaccoon is-a BabyRaccoon.

  • 10. 
    Given: 10. interface A { void x(); } 11. class B implements A { public void x() {} public void y() {} } 12. class C extends B { public void x() {} } And: 20. java.util.List<A> list = new java.util.ArrayList<A>(); 21. list.add(new B()); 22. list.add(new C()); 23. for (A a : list) { 24. a.x(); 25. a.y(); 26. } What is the result?
    • A. 

      The code runs with no output.

    • B. 

      An exception is thrown at runtime.

    • C. 

      Compilation fails because of an error in line 20.

    • D. 

      Compilation fails because of an error in line 21.

    • E. 

      Compilation fails because of an error in line 23.

    • F. 

      Compilation fails because of an error in line 25.

  • 11. 
    Given: 2. public class Hi { 3. void m1() { } 4. protected void() m2 { } 5. } 6. class Lois extends Hi { 7. // insert code here 8. } Which four code fragments, inserted independently at line 7, will compile? (Choose four.)
    • A. 

      Public void m1() { }

    • B. 

      Protected void m1() { }

    • C. 

      Private void m1() { }

    • D. 

      Void m2() { }

    • E. 

      Public void m2() { }

    • F. 

      Protected void m2() { }

    • G. 

      Private void m2() { }

  • 12. 
    Which four statements are true? (Choose four.)
    • A. 

      Has-a relationships should never be encapsulated.

    • B. 

      Has-a relationships should be implemented using inheritance.

    • C. 

      Has-a relationships can be implemented using instance variables.

    • D. 

      Is-a relationships can be implemented using the extends keyword.

    • E. 

      Is-a relationships can be implemented using the implements keyword.

    • F. 

      The relationship between Movie and Actress is an example of an is-a relationship.

    • G. 

      An array or a collection can be used to implement a one-to-many has-a relationship.

  • 13. 
    Given: 10: public class Hello { 11: String title; 12: int value; 13: public Hello() { 14: title += " World"; 15: } 16: public Hello(int value) { 17: this.value = value; 18: title = "Hello"; 19: Hello(); 20: } 21: } and: 30: Hello c = new Hello(5); 31: System.out.println(c.title); What is the result?
    • A. 

      Hello

    • B. 

      Hello World

    • C. 

      Compilation fails

    • D. 

      Hello World 5

    • E. 

      The code runs with no output

    • F. 

      An exception is thrown at runtime

  • 14. 
    Given: 1. package geometry; 2. public class Hypotenuse { 3. public InnerTriangle it = new InnerTriangle(); 4. class InnerTriangle { 5. public int base; 6. public int height; 7. } 8. } Which statement is true about the class of an object that can reference the variable base?
    • A. 

      It can be any class.

    • B. 

      No class has access to base.

    • C. 

      The class must belong to the geometry package.

    • D. 

      The class must be a subclass of the class Hypotenuse.

  • 15. 
    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.

  • 16. 
    Given: 10. interface Data { public void load(); } 11. abstract class Info { public abstract void load(); } Which class correctly uses the Data interface and Info class?
    • A. 

      Public class Employee extends Info implements Data { public void load() { /*do something*/ } }

    • B. 

      Public class Employee implements Info extends Data { public void load() { /*do something*/ } }

    • C. 

      Public class Employee extends Info implements Data public void load(){ /*do something*/ } public void Info.load(){ /*do something*/ } }

    • D. 

      Public class Employee implements Info extends Data { public void Data.load(){ /*do something*/ } public void load(){ /*do something*/ } }

    • E. 

      Public class Employee implements Info extends Data { public void load(){ /*do something*/ } public void Info.load(){ /*do something*/ } }

    • F. 

      Public class Employee extends Info implements Data{ public void Data.load() { /*do something*/ } public void Info.load() { /*do something*/ } }

  • 17. 
    Given: 1. class Alligator { 2. public static void main(String[] args) { 3. int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}}; 4. int [][]y = x; 5. System.out.println(y[2][1]); 6. } 7. } What is the result?
    • A. 

      2

    • B. 

      3

    • C. 

      4

    • D. 

      6

    • E. 

      7

    • F. 

      Compilation fails

  • 18. 
    Given: 21. abstract class C1 { 22. public C1() { System.out.print(1); } 23. } 24. class C2 extends C1 { 25. public C2() { System.out.print(2); } 26. } 27. class C3 extends C2 { 28. public C3() { System.out.println(3); } 29. } 30. public class Ctest { 31. public static void main(String[] a) { new C3(); } 32. } What is the result?
    • A. 

      3

    • B. 

      23

    • C. 

      32

    • D. 

      123

    • E. 

      321

    • F. 

      Compilation fails.

    • G. 

      An exception is thrown at runtime.

  • 19. 
    Given: 10. class One { 11. public One foo() { return this; } 12. } 13. class Two extends One { 14. public One foo() { return this; } 15. } 16. class Three extends Two { 17. // insert method here 18. } Which two methods, inserted individually, correctly complete the Three class? (Choose two.)
    • A. 

      Public void foo() {}

    • B. 

      Public int foo() { return 3; }

    • C. 

      Public Two foo() { return this; }

    • D. 

      Public One foo() { return this; }

    • E. 

      Public Object foo() { return this; }

  • 20. 
    Which two classes correctly implement both the java.lang.Runnable and the java.lang. Cloneable interfaces? (Choose two.)
    • A. 

      Public class Session implements Runnable, Cloneable { public void run(); public Object clone(); }

    • B. 

      Public class Session extends Runnable, Cloneable { public void run() { /* do something */ } public Object clone() { /* make a copy */ }

    • C. 

      Public class Session implements Runnable, Cloneable { public void run() { /* do something */ } public Object clone() { /* make a copy */ }

    • D. 

      Public abstract class Session implements Runnable, Cloneable { public void run() { /* do something */ } public Object clone() { /*make a copy */ }

    • E. 

      Public class Session implements Runnable, implements Cloneable { public void run() { /* do something */ } public Object clone() { /* make a copy */ }

  • 21. 
    Given: 11. public interface A { public void m1(); } 12. 13. class B implements A { } 14. class C implements A { public void m1() { } } 15. class D implements A { public void m1(int x) { } } 16. abstract class E implements A { } 17. abstract class F implements A { public void m1() { } } 18. abstract class G implements A { public void m1(int x) { } } What is the result?
    • A. 

      Compilation succeeds.

    • B. 

      Exactly one class does NOT compile.

    • C. 

      Exactly two classes do NOT compile.

    • D. 

      Exactly four classes do NOT compile.

    • E. 

      Exactly three classes do NOT compile.

  • 22. 
    Given: 10. class Line { 11. public class Point { public int x,y;} 12. public Point getPoint() { return new Point(); } 13. } 14. class Triangle { 15. public Triangle() { 16. // insert code here 17. } 18. } Which code, inserted at line 16, correctly retrieves a local instance of a Point object?
    • A. 

      Point p = Line.getPoint();

    • B. 

      Line.Point p = Line.getPoint();

    • C. 

      Point p = (new Line()).getPoint();

    • D. 

      Line.Point p = (new Line()).getPoint();

  • 23. 
    Given: 1. class TestA { 2. public void start() { System.out.println("TestA"); } 3. } 4. public class TestB extends TestA { 5. public void start() { System.out.println("TestB"); } 6. public static void main(String[] args) { 7. ((TestA)new TestB()).start(); 8. } 9. } What is the result?
    • A. 

      TestA

    • B. 

      TestB

    • C. 

      Compilation fails.

    • D. 

      An exception is thrown at runtime.

  • 24. 
    Which statement is true about the classes and interfaces in the exhibit? 1.   public interface A { 2.      public void doSomething(String thing); 3.   } 1.   public class AImpl implements A { 2.      public void doSomething(String thing){ } 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.   }
    • A. 

      Compilation will succeed for all classes and interfaces.

    • B. 

      Compilation of class C will fail because of an error in line 2.

    • C. 

      Compilation of class C will fail because of an error in line 6.

    • D. 

      Compilation of class AImpl will fail because of an error in line 2.

  • 25. 
    Given the following six method names: addListener addMouseListener setMouseListener deleteMouseListener removeMouseListener registerMouseListener How many of these method names follow JavaBean Listener naming rules?
    • A. 

      1

    • B. 

      2

    • C. 

      3

    • D. 

      4

    • E. 

      5

Back to Top Back to top