jobs4timesLogo jobs4timesLogo

Object Oriented Programming Certification Programs

Q: 01

Click the Task button.

Place the output options in the Actual Output Sequence to indicate the output from this code :

class Alpha{
public void foo(String... args)
 { System.out.print(" Alpha:foo "); }
public void bar(String a)
 { System.out.print(" Alpha:bar "); }
}

public class Beta extends Alpha {
public void foo(String a)
 { System.out.print(" Beta:foo "); }
public void bar(String a)
 { System.out.print(" Beta:bar "); }
public static void main(String[] argv) {
Alpha a=new Beta();
Beta b=(Beta)a;
a.foo("test"); //Alpha:foo
a.bar("test"); //Beta:bar
b.foo("test"); //Beta:foo 
b.bar("test"); //Beta:bar
}

}
Solution:

Alpha.foo Beta.bar Beta.foo Beta.bar

Q: 02

Given:
1. public class Plant {
2. private String name;
3. public Plant(String name) { this.name = name; }
4. public String getName() { return name; }
5. }

1. public class Tree extends Plant {
2. public void growFruit() { }
3. public void dropLeaves() { }
4. }
Which statement is true?
  1. The code will compile without changes.
  2. The code will compile if public Tree() { Plant(); } is added to the Tree class.
  3. The code will compile if public Plant() { Tree(); } is added to the Plant class.
  4. The code will compile if public Plant() { this("fern"); } is added to the Plant class.
  5. The code will compile if public Plant() { Plant("fern"); } is added to the Plant class.
Answer: D

Q: 03

Click the Exhibit button.

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. }
  1. Compilation will succeed for all classes and interfaces.
  2. Compilation of class C will fail because of an error in line 2.
  3. Compilation of class C will fail because of an error in line 6.
  4. Compilation of class AImpl will fail because of an error in line 2.
Answer: C

Q: 04

Given:
public class Yikes {

public static void go(Long n) {System.out.println("Long ");}
public static void go(Short n) {System.out.println("Short ");}
public static void go(int n) {System.out.println("int ");}
public static void main(String [] args) {
short y = 6;
long z = 7;
go(y);
go(z);
}
}
What is the result?
  1. int Long
  2. Short Long
  3. Compilation fails.
  4. An exception is thrown at runtime.
Answer: A

Q: 05

Click the Task button.
class A {
 String name= "A";
 String getName(){
	 return name;
 }
 String greeting(){
	 return "class A";
 }
}

class B extends A {
 String name="B";
 String greeting(){
	 return "class B";
 }
}

public class Client {
 public static void main(String[] args){
  A a=new A();
  B b=new B();
  System.out.println(a.greeting()+ " has name "+a.getName());
  System.out.println(b.greeting()+ " has name "+b.getName());
 }
}
Solution:
class A has name A
class B has name A

Q: 06

Which two statements are true about has-a and is-a relationships? (Choose two.)
  1. Inheritance represents an is-a relationship.
  2. Inheritance represents a has-a relationship.
  3. Interfaces must be used when creating a has-a relationship.
  4. Instance variables can be used when creating a has-a relationship.
Answer: A, D

Q: 07

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?
  1. Hello
  2. Hello World
  3. Compilation fails.
  4. Hello World 5
  5. The code runs with no output.
  6. An exception is thrown at runtime.
Answer: C

Q: 08

Given:
1. class Super {
2. private int a;
3. protected Super(int a) { this.a = a; }
4. }
...
11. class Sub extends Super {
12. public Sub(int a) { super(a); }
13. public Sub() { this.a = 5; }
14. }
Which two, independently, will allow Sub to compile? (Choose two.)
  1. Change line 2 to :public int a;
  2. Change line 2 to :protected int a;
  3. Change line 13 to :public Sub() { this(5); }
  4. Change line 13 to :public Sub() { super(5); }
  5. Change line 13 to :public Sub() { super(a); }
Answer: C, D

Q: 09

Click the Task button. Solution: Car is a Vehicle class A and < ==================> implements B , C car is a Collectable { } Car has a < ==================> class A { Steering Wheel B b; } car has Wheels < ==================> class A{ List <B> b; } Mini is a Car < ==================> class A extends B{ } Car is an Object < ==================> class A { } img

Q: 10 Given:

class ClassA { 
 public int numberOfInstances; 
 protected ClassA(int numberOfInstances) { 
 this.numberOfInstances = numberOfInstances; 
 } 
} 

public class ExtendedA extends ClassA { 
 private ExtendedA(int numberOfInstances) { 
 super(numberOfInstances); 
 } 
 public static void main(String[] args) { 
 ExtendedA ext = new ExtendedA(420); 
 System.out.print(ext.numberOfInstances); 
 } 
} 
Which statement is true?
  1. 420 is the output.
  2. An exception is thrown at runtime.
  3. All constructors must be declared public.
  4. Constructors CANNOT use the private modifier.
  5. Constructors CANNOT use the protected modifier.
Answer: A

Q: 11 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?
  1. Compilation fails because of an error in line 3.
  2. Compilation fails because of an error in line 7.
  3. Compilation fails because of an error in line 9.
  4. If you define D e = new E(), then e.bMethod() invokes the version of bMethod() defined in Line 5.
  5. If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 5.
  6. If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 9.
Answer: F

Q: 12 Given:

public class Base { 
public static final String FOO = "foo"; 
public static void main(String[] args) { 
Base b = new Base(); 
Sub s = new Sub(); 
System.out.print(Base.FOO); 
System.out.print(Sub.FOO); 
System.out.print(b.FOO); 
System.out.print(s.FOO); 
System.out.print(((Base)s).FOO); 
} } 
class Sub extends Base {public static final String FOO="bar";}
What is the result?
  1. foofoofoofoofoo
  2. foobarfoobarbar
  3. foobarfoofoofoo
  4. foobarfoobarfoo
  5. barbarbarbarbar
  6. foofoofoobarbar
  7. foofoofoobarfoo
Answer: D

Q: 13 Which two statements are true? (Choose two.)

  1. An encapsulated, public class promotes re-use.
  2. Classes that share the same interface are always tightly encapsulated.
  3. An encapsulated class allows subclasses to overload methods, but does NOT allow overriding methods.
  4. An encapsulated class allows a programmer to change an implementation without affecting outside code.
Answer: A, D

Q: 14 Given :

11. public static void test(String str) { 
12. if (str == null | str.length() == 0) { 
13. System.out.println("String is empty"); 
14. } else { 
15. System.out.println("String is not empty"); 
16. } 
17. } 
And the invocation: 
31. test(null); 
What is the result?
  1. An exception is thrown at runtime.
  2. "String is empty" is printed to output.
  3. Compilation fails because of an error in line 12.
  4. "String is not empty" is printed to output.
Answer: A

Q: 15 Given :

public class Wow { 
 public static void go(short n) {System.out.println("short");} 
 public static void go(Short n) {System.out.println("SHORT");} 
 public static void go(Long n) {System.out.println(" LONG");} 
 public static void main(String [] args) { 
 Short y = 6; 
 int z = 7; 
 go(y); 
 go(z); 
 } 
} 
What is the result?
  1. short LONG
  2. SHORT LONG
  3. Compilation fails.
  4. An exception is thrown at runtime.
Answer: C

Q : 16 Click the Exhibit button.

Which three code fragments, added individually at line 29, produce the output 100 ? (Choose three.)

10. class Inner{
11.  private int x;
12.  public void setX(int x){ this.x=x; }
13.  public int getX(){ return x; }
14. }
15.
16. class Outer{
17.  private Inner y;
18.  public void setY(Inner y){ this.y=y; }
19.  public Inner getY(){ return y; }
20. }
21.
22. public class Gamma {
23.  public static void main(String [] args) { 
24.   Outer o=new Outer();
25.   Inner i=new Inner();
26.   int n=10;
27.   i.setX(n);
28.   o.setY(i);
29.   //insert code here
30.   System.out.println(o.getY().getX());
31.  } 
32. }
  1. n = 100;
  2. i.setX( 100 );
  3. o.getY().setX( 100 );
  4. i = new Inner(); i.setX( 100 );
  5. o.setY( i ); i = new Inner(); i.setX( 100 );
  6. i = new Inner(); i.setX( 100 ); o.setY( i );
Answer: B, C, F

Q: 17 Given:

class One {
 public One() { System.out.print(1); }
}

class Two extends One {
 public Two() { System.out.print(2); }
}

class Three extends Two {
 public Three() { System.out.print(3); }
}

public class Numbers{
 public static void main( String[] argv ) { 
	 new Three();
 }
}
What is the result when this code is executed?
  1. 1
  2. 3
  3. 123
  4. 321
  5. The code runs with no output.
Answer: C

Q: 18 Click the Exhibit button.

What is the outcome of the code?

public class Item{
 private String desc;
 public String getDescription() {
	 return desc;
 }
 public void setDescription(String d) {
  this.desc=d;
 }
 public static void modifyDesc(Item item, String desc){
	 item=new Item();
	 item.setDescription(desc);
 }
 public static void main( String[] args ) { 
  Item it=new Item();
  it.setDescription("Gobstopper");
  Item it2=new Item();
  it2.setDescription("Fizzylifting");
  modifyDesc(it,"Scrundiddlyumptious");
  System.out.println(it.getDescription());
  System.out.println(it2.getDescription());
 }
  1. Compilation fails.
  2. Gobstopper
    Fizzylifting
  3. Gobstopper
    Scrumdiddlyumptious
  4. Scrumdiddlyumptious
    Fizzylifting
  5. Scrumdiddlyumptious
Answer: B

Q: 19 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.)

  1. public void foo() {}
  2. public int foo() { return 3; }
  3. public Two foo() { return this; }
  4. public One foo() { return this; }
  5. public Object foo() { return this; }
Answer: C, D

Q: 20 Click the Exhibit button.

What is the output of the program shown in the exhibit?

class Foo {
 private int x;
 public Foo(int x) { this.x=x ;}
 public void setX(int x) { this.x=x; }
 public int getX() { return x; }
}

public class Gamma{
	
 static Foo fooBar(Foo foo) {
  foo=new Foo(100);
  return foo;
 }
 public static void main( String[] args ) {
	 
  Foo foo=new Foo(300);
  System.out.print(foo.getX()+"-");
  
  Foo fooFoo=fooBar(foo);
  System.out.print(foo.getX()+"-");
  System.out.print(fooFoo.getX()+"-");
  
  foo=fooBar(fooFoo);
  System.out.print(foo.getX()+"-");
  System.out.print(fooFoo.getX());
 }

}
  1. 300-100-100-100-100
  2. 300-300-100-100-100
  3. 300-300-300-100-100
  4. 300-300-300-300-100
Answer: B

Q: 21 Click the Task button.

Add methods to the Beta class to make it compile correctly.

class Alpha {
 public void bar(int... x){ }
 public void bar(int x){ }
}

public class Beta extends Alpha{
 //Place here
 //Place here
 //Place here
}
Methods
private void bar(int x){}

public void bar(int x){}

public int bar(String x){ return 1;}

public Alpha bar(int x){ }

public void bar(int x,int y){ }

public int bar(int x){ return x; }
Solution:
public void bar(int x){ }
public int bar(String x){ return 1; }
public void bar(int x,int y) { }

Q: 22 Given:

10. interface A { public int getValue(); }
11. class B implements A {
12. public int getValue() { return 1; }
13. }
14. class C extends B {
15. // insert code here
16. }

Which three code fragments, inserted individually at line 15, make use of polymorphism ? (Choose three.)

  1. public void add(C c) { c.getValue(); }
  2. public void add(B b) { b.getValue(); }
  3. public void add(A a) { a.getValue(); }
  4. public void add(A a, B b) { a.getValue(); }
  5. public void add(C c1, C c2) { c1.getValue(); }
Answer: B, C, D

Q: 23 Click the Exhibit button.

What is the result ?

  1. snootchy 420 third second first
  2. snootchy 420 first second third
  3. first second third snootchy 420
  4. third second first snootchy 420
  5. third first second snootchy 420
  6. first second first third snootchy 420
Answer: D

Q: 24 Given:

10. abstract class A {
11. abstract void a1();
12. void a2() { }
13. }
14. class B extends A {
15. void a1() { }
16. void a2() { }
17. }
18. class C extends B { void c1() { } }
and:
A x = new B(); 
C y = new C(); 
A z = new C();

What are four valid examples of polymorphic method calls ? (Choose four.)

  1. x.a2();
  2. z.a2();
  3. z.c1();
  4. z.a1();
  5. y.c1();
  6. x.a1();
Answer: A, B, D, F

Q: 25 Click the Exhibit button.

What is the result?

  1. Value is: 8
  2. Compilation fails.
  3. Value is: 12
  4. Value is: -12
  5. The code runs with no output.
  6. An exception is thrown at runtime.
Answer: A

Q: 26 Given:

20. public class CreditCard {
21.
22. private String cardID;
23. private Integer limit;
24. public String ownerName;
25.
26. public void setCardInformation(String cardID,
27. String ownerName,
28. Integer limit) {
29. this.cardID = cardID;
30. this.ownerName = ownerName;
31. this.limit = limit;
32. }
33. }
Which statement is true?
  1. The class is fully encapsulated.
  2. The code demonstrates polymorphism.
  3. The ownerName variable breaks encapsulation.
  4. The cardID and limit variables break polymorphism.
  5. The setCardInformation method breaks encapsulation.
Answer: C

Q: 27 Given:

11. class Animal { public String noise() { return "peep"; } }
12. class Dog extends Animal {
13. public String noise() { return "bark"; }
14. }
15. class Cat extends Animal {
16. public String noise() { return "meow"; }
17. }
...
30. Animal animal = new Dog();
31. Cat cat = (Cat)animal;
32. System.out.println(cat.noise());
What is the result?
  1. peep
  2. bark
  3. meow
  4. Compilation fails.
  5. An exception is thrown at runtime.
Answer: E

Q: 28 Click the Task button.

Solution: Dog is-a Animal Forest has-a Tree Rectangle has-a Side JavaBook is-a ProgrammingBook img

Q: 29 Which three statements are true? (Choose three.)

  1. A final method in class X can be abstract if and only if X is abstract.
  2. A protected method in class X can be overridden by any subclass of X.
  3. A private static method can be called only within other static methods in class X.
  4. A non-static public final method in class X can be overridden in any subclass of X.
  5. A public static method in class X can be called by a subclass of X without explicitly referencing the class X.
  6. A method with the same signature as a private final method in class X can be implemented in a subclass of X.
  7. A protected method in class X can be overridden by a subclass of A only if the subclass is in the same package as X.
Answer: B, E, F

Q: 30 Which four statements are true? (Choose four.)

  1. Has-a relationships should never be encapsulated.
  2. Has-a relationships should be implemented using inheritance.
  3. Has-a relationships can be implemented using instance variables.
  4. Is-a relationships can be implemented using the extends keyword.
  5. Is-a relationships can be implemented using the implements keyword.
  6. The relationship between Movie and Actress is an example of an is-a relationship.
  7. An array or a collection can be used to implement a one-to-many has-a relationship.
Answer: C, D, E, G

Q: 31 Given :

10. interface Jumper { public void jump(); } ... 
20. class Animal {} ... 
30. class Dog extends Animal { 
31. Tail tail; 32. } ... 
40. class Beagle extends Dog implements Jumper{ 
41. public void jump() {} 
42. } ... 
50. class Cat implements Jumper{ 
51. public void jump() {} 
52. } 

Which three are true? (Choose three.)

  1. Cat is-a Animal
  2. Cat is-a Jumper
  3. Dog is-a Animal
  4. Dog is-a Jumper
  5. Cat has-a Animal
  6. Beagle has-a Tail
  7. Beagle has-a Jumper
Answer: B, C, F

Q: 32 Click the Exhibit button.

What two must the programmer do to correct the compilation errors? (Choose two.)

  1. insert a call to this() in the Car constructor
  2. insert a call to this() in the MeGo constructor
  3. insert a call to super() in the MeGo constructor
  4. insert a call to super(vin) in the MeGo constructor
  5. change the wheelCount variable in Car to protected
  6. change line 3 in the MeGo class to super.wheelCount = 3;
Answer: D, E

Q: 33 Given:

10. public class SuperCalc {
11. protected static int multiply(int a, int b) { return a * b;}
12. }
and:
20. public class SubCalc extends SuperCalc{
21. public static int multiply(int a, int b) {
22. int c = super.multiply(a, b);
23. return c; 
24. } 
25. } 
and: 
30. SubCalc sc = new SubCalc (); 
31. System.out.println(sc.multiply(3,4)); 
32. System.out.println(SubCalc.multiply(2,2)); 

What is the result ?

  1. 12 4
  2. The code runs with no output.
  3. An exception is thrown at runtime.
  4. Compilation fails because of an error in line 21.
  5. Compilation fails because of an error in line 22.
  6. Compilation fails because of an error in line 31.
Answer: E

Q: 34 Given:

1.public class Blip { 
2. protected int blipvert(int x) { return 0; } 
3. } 
4. class Vert extends Blip { 
5. // insert code here 
6. } 

Which five methods, inserted independently at line 5, will compile? (Choose five.)

  1. public int blipvert(int x) { return 0; }
  2. private int blipvert(int x) { return 0; }
  3. private int blipvert(long x) { return 0; }
  4. protected long blipvert(int x) { return 0; }
  5. protected int blipvert(long x) { return 0; }
  6. protected long blipvert(long x) { return 0; }
  7. protected long blipvert(int x, int y) { return 0; }
Answer: A, C, E, F, G

Q: 35 Given:

 
1. class Pizza { 
2. java.util.ArrayList toppings; 
3. public final void addTopping(String topping) { 
4. toppings.add(topping); 
5. } 
6. } 
7. public class PepperoniPizza extends Pizza { 
8. public void addTopping(String topping) { 
9. System.out.println("Cannot add Toppings"); 
10. } 
11. public static void main(String[] args) { 
12. Pizza pizza = new PepperoniPizza(); 
13. pizza.addTopping("Mushrooms"); 
14. } 
15. }
What is the result?
  1. Compilation fails.
  2. Cannot add Toppings
  3. The code runs with no output.
  4. A NullPointerException is thrown in Line 4.
Answer: A

Q: 36 Given:

1. interface DoStuff2 {
2. float getRange(int low, int high); }
3.
4. interface DoMore {
5. float getAvg(int a, int b, int c); }
6.
7. abstract class DoAbstract implements DoStuff2, DoMore { }
8.
9. class DoStuff implements DoStuff2 {
10. public float getRange(int x, int y) { return 3.14f; } }
11.
12. interface DoAll extends DoMore {
13. float getAvg(int a, int b, int c, int d); }
What is the result?
  1. The file will compile without error.
  2. Compilation fails. Only line 7 contains an error.
  3. Compilation fails. Only line 12 contains an error.
  4. Compilation fails. Only line 13 contains an error.
  5. Compilation fails. Only lines 7 and 12 contain errors.
  6. Compilation fails. Only lines 7 and 13 contain errors.
  7. Compilation fails. Lines 7, 12, and 13 contain errors.
Answer: A

Q: 37 Click the Exhibit button.

Given:

25. A a = new A();
26. System.out.println(a.doit(4, 5));
What is the result?
  1. Line 26 prints "a" to System.out.
  2. Line 26 prints "b" to System.out.
  3. An exception is thrown at line 26 at runtime.
  4. Compilation of class A will fail due to an error in line 6.
Answer: A

Q: 38 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?
  1. TestA
  2. TestB
  3. Compilation fails.
  4. An exception is thrown at runtime.
Answer: B

Q: 39 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?
  1. test
  2. null
  3. An exception is thrown at runtime.
  4. Compilation fails because of an error in line 1.
  5. Compilation fails because of an error in line 4.
  6. Compilation fails because of an error in line 5.
Answer: A

Q: 40 Given:

11. public class ItemTest { 
12. private final int id; 
13. public ItemTest(int id) { this.id = id; } 
14. public void updateId(int newId) { id = newId; } 
15. 
16. public static void main(String[] args) { 
17. ItemTest fa = new ItemTest(42); 18. fa.updateId(69); 
19. System.out.println(fa.id); 
20. } 
21. } 
What is the result?
  1. Compilation fails.
  2. An exception is thrown at runtime.
  3. The attribute id in the Item object remains unchanged.
  4. The attribute id in the Item object is modified to the new value.
  5. A new Item object is created with the preferred value in the id attribute.
Answer: A

Q: 41 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.)

  1. int foo() { /* more code here */ }
  2. void foo() { /* more code here */ }
  3. public void foo() { /* more code here */ }
  4. private void foo() { /* more code here */ }
  5. protected void foo() { /* more code here */ }
Answer: B, C, E

Q: 42 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 ?

  1. A. public class Employee extends Info implements Data { public void load() { /*do something*/ } }
  2. public class Employee implements Info extends Data { public void load() { /*do something*/ } }
  3. public class Employee extends Info implements Data public void load(){ /*do something*/ } public void Info.load(){ /*do something*/ } }
  4. public class Employee implements Info extends Data { public void Data.load(){ /*do something*/ } public void load(){ /*do something*/ } }
  5. public class Employee implements Info extends Data { public void load(){ /*do something*/ } public void Info.load(){ /*do something*/ } }
  6. public class Employee extends Info implements Data{ public void Data.load() { /*do something*/ } public void Info.load() { /*do something*/ } }
Answer: A

Q: 43 Given:

11. public abstract class Shape { 
12. int x; 
13. 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. } 

and a class Circle that extends and fully implements the Shape class. Which is correct ?

  1. Shape s = new Shape(); s.setAnchor(10,10); s.draw();
  2. Circle c = new Shape(); c.setAnchor(10,10); c.draw();
  3. Shape s = new Circle(); s.setAnchor(10,10); s.draw();
  4. Shape s = new Circle(); s->setAnchor(10,10); s->draw();
  5. Circle c = new Circle(); c.Shape.setAnchor(10,10); c.Shape.draw();
Answer: C

Q: 44 Click the Exhibit button.

What is the result?

  1. 4321
  2. 0000
  3. An exception is thrown at runtime.
  4. Compilation fails because of an error in line 18.
Answer: D

Q: 45 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 ?

  1. Alpha a = x;
  2. Foo f = (Delta)x;
  3. Foo f = (Alpha)x;
  4. Beta b = (Beta)(Alpha)x;
Answer: B

Q: 46 Given:

1. class SuperClass {
2. public A getA() {
3. return new A();
4. }
5. }
6. class SubClass extends SuperClass { 
7. public B getA(){ 
8. return new B(); 
9. } 
10. } 

Which statement is true ?

  1. Compilation will succeed if A extends B.
  2. Compilation will succeed if B extends A.
  3. Compilation will always fail because of an error in line 7.
  4. Compilation will always fail because of an error in line 8.
Answer: B

Q: 47 Given:

 
11. static class A { 
12. void process() throws Exception { throw new Exception(); } 
13. } 
14. static class B extends A { 
15. void process() { System.out.println("B"); } 
16. } 
17. public static void main(String[] args) { 
18. new B().process(); 
19. } 

What is the result ?

  1. B
  2. The code runs with no output.
  3. Compilation fails because of an error in line 12.
  4. Compilation fails because of an error in line 15.
  5. Compilation fails because of an error in line 18.
Answer: A

Q: 48 Given:

 
11. class ClassA {} 
12. class ClassB extends ClassA {} 
13. class ClassC extends ClassA {} and: 
21. ClassA p0 = new ClassA(); 
22. ClassB p1 = new ClassB(); 
23. ClassC p2 = new ClassC(); 
24. ClassA p3 = new ClassB(); 
25. ClassA p4 = new ClassC(); 

Which three are valid? (Choose three.)

  1. p0 = p1;
  2. p1 = p2;
  3. p2 = p4;
  4. p2 = (ClassC)p1;
  5. p1 = (ClassB)p3;
  6. p2 = (ClassC)p4;
Answer: A, E, F

Q: 49 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() 
25. + ", " + vehicle.speed()); 

What is the result ?

  1. 0, 0, 0
  2. 150, 60, 0
  3. Compilation fails.
  4. 150, 150, 150
  5. An exception is thrown at runtime.
Answer: D

Q: 50 Given code in separate source files:

10. public class Foo { 
11. public int a; 
12. public Foo() { a = 3; } 
13. public void addFive() { a += 5;} 
14. } 
and: 
20. public class Bar extends Foo { 
21. public int a; 
22. public Bar() { a = 8; } 
23. public void addFive() { this.a += 5; } 
24. } 
invoked with: 
30. Foo foo = new Bar(); 
31. foo.addFive(); 
32. System.out.println("Value: " + foo.a); 

What is the result ?

  1. Value: 3
  2. Value: 8
  3. Value: 13
  4. Compilation fails.
  5. The code runs with no output.
  6. An exception is thrown at runtime.
Answer: A

Q: 51 Which Man class properly represents the relationship "Man has a best friend who is a Dog" ?

  1. class Man extends Dog { }
  2. class Man implements Dog { }
  3. class Man { private BestFriend dog; }
  4. class Man { private Dog bestFriend; }
  5. class Man { private Dog<bestFriend>; }
  6. class Man { private BestFriend<dog>; }
Answer: D

Q: 52 Given:

10. interface Foo { int bar(); }
11. public class Sprite {
12. public int fubar( Foo foo ) { return foo.bar(); }
13. public void testFoo() {
14. fubar(
15. // insert code here
16. );
17. }
18. }
Which code, inserted at line 15, allows the class Sprite to compile?
  1. Foo { public int bar() { return 1; }
  2. new Foo { public int bar() { return 1; }
  3. new Foo() { public int bar() { return 1; }
  4. new class Foo { public int bar() { return 1; }
Answer: C

Q: 53 Click the Task button.

Solution: 1. int 2. h 3. amount.

Q: 54 Given classes defined in two different files:

1. package packageA; 
2. public class Message { 
3. String getText() { return "text"; } 
4. } 
and: 
1. package packageB; 
2. public class XMLMessage extends packageA.Message { 
3. String getText() { return "<msg>text</msg>";} 
4. public static void main(String[] args) { 
5. System.out.println(new XMLMessage().getText()); 
6. } 
7. } 

What is the result of executing XMLMessage.main ?

  1. text
  2. An exception is thrown at runtime.
  3. Compilation fails because of an error in line 2 of XMLMessage.
  4. Compilation fails because of an error in line 3 of XMLMessage.
Answer: D

Q: 55 Given:

1. public class A { 
2. public void doit() { 
3. } 
4. public String doit() { 
5. return "a"; 
6. } 
7. public double doit(int x) { 
8. return 1.0; 
9. } 
10. } 
What is the result?
  1. An exception is thrown at runtime.
  2. Compilation fails because of an error in line 7.
  3. Compilation fails because of an error in line 4.
  4. Compilation succeeds and no runtime errors with class A occur.
Answer: C

Q: 56 Click the Exhibit button.

 
1. public class GoTest { 
2. public static void main(String[] args) { 
3. Sente a = new Sente(); a.go(); 
4. Goban b = new Goban(); b.go(); 
5. Stone c = new Stone(); c.go(); 
6. } 
7. }
8. 
9. class Sente implements Go { 
10. public void go() { System.out.println("go in Sente."); } 
11. } 
12. 
13. class Goban extends Sente { 
14. public void go() { System.out.println("go in Goban"); } 
15. } 
16. 
17. class Stone extends Goban implements Go { } 
18. 
19. interface Go { public void go(); } 

What is the result ?

  1. go in Goban go in Sente go in Sente
  2. go in Sente go in Sente go in Goban
  3. go in Sente go in Goban go in Goban
  4. go in Goban go in Goban go in Sente
  5. Compilation fails because of an error in line 17.
Answer: C

Q : 57 Click the Exhibit button.

 
1. public class Employee { 
2. String name; 
3. double baseSalary; 
4. Employee(String name, double baseSalary) { 
5. this.name = name; 
6. this.baseSalary = baseSalary; 
7. } 
8. } 
And: 
1. public class Salesperson extends Employee { 
2. double commission; 
3. public Salesperson(String name, double baseSalary, 
4. double commission) { 
5. // insert code here 
6. } 
7. } 

Which code, inserted at line 7, completes the Salesperson constructor ?

  1. this.commission = commission;
  2. superb(); commission = commission;
  3. this.commission = commission; superb();
  4. super(name, baseSalary); this.commission = commission;
  5. super(); this.commission = commission;
  6. this.commission = commission; super(name, baseSalary);
Answer: D

Q : 58 Given:

1. public class Team extends java.util.LinkedList { 
2. public void addPlayer(Player p) { 
3. add(p); 
4. } 
5. public void compete(Team opponent) { /* more code here */ } 
6. } 
7. class Player { /* more code here */ } 
Which two are true? (Choose two.)
  1. This code will compile.
  2. This code demonstrates proper design of an is-a relationship.
  3. This code demonstrates proper design of a has-a relationship.
  4. A Java programmer using the Team class could remove Player objects from a Team object.
Answer: A,D

Q : 59 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?
  1. Compilation fails.
  2. atom granite
  3. granite granite
  4. atom granite granite
  5. An exception is thrown at runtime.
  6. atom granite atom granite
Answer: F

Q : 60 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?
  1. If line 10 is removed, the compilation succeeds.
  2. If line 11 is removed, the compilation succeeds.
  3. If line 12 is removed, the compilation succeeds.
  4. If line 13 is removed, the compilation succeeds.
  5. More than one line must be removed for compilation to succeed.
Answer: C

Q : 61

A company has a business application that provides its users with many different reports receivables reports, payables reports, revenue projects, and so on. The company has just purchased some new, state-of-the-art, wireless printers, and a programmer has been assigned the task of enhancing all of the reports to use not only the company's old printers, but the new wireless printers as well. When the programmer starts looking into the application, the programmer discovers that because of the design of the application, it is necessary to make changes to each report to support the new printers.

Which two design concepts most likely explain this situation? (Choose two.)

  1. Inheritance
  2. Low cohesion
  3. Tight coupling
  4. High cohesion
  5. Loose coupling
  6. Object immutability
Answer: B,C

Q : 62 Given:

31. class Foo { 
32. public int a = 3; 
33. public void addFive() { a += 5; System.out.print("f "); } 
34. } 
35. class Bar extends Foo {
36. public int a = 8; 
37. public void addFive() { this.a += 5; System.out.print("b " ); } 
38. } 
Invoked with: 
Foo f = new Bar(); 
f.addFive(); 
System.out.println(f.a);

What is the result ?

  1. b 3
  2. b 8
  3. b 13
  4. f 3
  5. f 8
  6. f 13
  7. Compilation fails.
  8. An exception is thrown at runtime.
Answer: A

Q : 63

A company that makes Computer Assisted Design (CAD) software has, within its application, some utility classes that are used to perform 3D rendering tasks. The company's chief scientist has just improved the performance of one of the utility classes' key rendering algorithms, and has assigned a programmer to replace the old algorithm with the new algorithm. When the programmer begins researching the utility classes, she is happy to discover that the algorithm to be replaced exists in only one class. The programmer reviews that class's API, and replaces the old algorithm with the new algorithm, being careful that her changes adhere strictly to the class's API. Once testing has begun, the programmer discovers that other classes that use the class she changed are no longer working properly.

What design flaw is most likely the cause of these new bugs?

  1. Inheritance
  2. Tight coupling
  3. Low cohesion
  4. High cohesion
  5. Loose coupling
  6. Object immutability
Answer: B

Q : 64 Given:

5. class Thingy { Meter m = new Meter(); } 
6. class Component { void go() { System.out.print("c"); } } 
7. class Meter extends Component { void go() { System.out.print("m"); } } 
8. 
9. class DeluxeThingy extends Thingy { 
10. public static void main(String[] args) { 
11. DeluxeThingy dt = new DeluxeThingy(); 
12. dt.m.go(); 
13. Thingy t = new DeluxeThingy();
14. t.m.go(); 
15. } 
16. } 
Which two are true? (Choose two.)
  1. The output is mm.
  2. The output is mc.
  3. Component is-a Meter.
  4. Component has-a Meter.
  5. DeluxeThingy is-a Component.
  6. DeluxeThingy has-a Component.
Answer: A,F

Q : 65 Given:

 
1. class X { 
2. X() { System.out.print(1); } 
3. X(int x) { 
4. this(); System.out.print(2); 
5. } 
6. } 
7. public class Y extends X { 
8. Y() { super(6); System.out.print(3); } 
9. Y(int y) { 
10. this(); System.out.println(4); 
11. } 
12. public static void main(String[] a) { new Y(5); } 
13. } 
What is the result?
  1. 13
  2. 134
  3. 1234
  4. 2134
  5. 2143
  6. 4321
Answer: C

Q : 66 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.)

  1. public void m1() { }
  2. protected void m1() { }
  3. private void m1() { }
  4. void m2() { }
  5. public void m2() { }
  6. protected void m2() { }
  7. private void m2() { }
Answer: A,B,E,F

Q : 67 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?
  1. Compilation succeeds.
  2. Exactly one class does NOT compile.
  3. Exactly two classes do NOT compile.
  4. Exactly four classes do NOT compile.
  5. Exactly three classes do NOT compile.
Answer: C

Q : 68 Given:

 
3. interface Fish { } 
4. class Perch implements Fish { } 
5. class Walleye extends Perch { } 
6. class Bluegill { } 
7. public class Fisherman { 
8. public static void main(String[] args) { 
9. Fish f = new Walleye(); 
10. Walleye w = new Walleye(); 
11. Bluegill b = new Bluegill(); 
12. if(f instanceof Perch) System.out.print("f-p "); 
13. if(w instanceof Fish) System.out.print("w-f "); 
14. if(b instanceof Fish) System.out.print("b-f "); 
15. } 
16. } 
What is the result?
  1. w-f
  2. f-p w-f
  3. w-f b-f
  4. f-p w-f b-f
  5. Compilation fails.
  6. An exception is thrown at runtime.
Answer: B

Q : 69

11. public interface A111 { 
12. String s = "yo"; 
13. public void method1(); 
14. } 
17. interface B { } 
20. interface C extends A111, B { 
21. public void method1(); 
22. public void method1(int x); 
23. }

What is the result ?

  1. Compilation succeeds.
  2. Compilation fails due to multiple errors.
  3. Compilation fails due to an error only on line 20.
  4. Compilation fails due to an error only on line 21.
  5. Compilation fails due to an error only on line 22.
  6. Compilation fails due to an error only on line 12.
Answer: A

Q : 70

 
10. import java.io.*; 
11. class Animal { 
12. Animal() { System.out.print("a"); } 
13. } 
14. class Dog extends Animal implements Serializable { 
15. Dog() { System.out.print("d"); } 
16. } 
17. public class Beagle extends Dog { } 

If an instance of class Beagle is created, then Serialized, then deSerialized, what is the result ?

  1. ad
  2. ada
  3. add
  4. adad
  5. Compilation fails.
  6. An exception is thrown at runtime.
Answer: B

Q : 71

A team of programmers is involved in reviewing a proposed design for a new utility class. After some discussion, they realize that the current design allows other classes to access methods in the utility class that should be accessible only to methods within the utility class itself. What design issue has the team discovered?
  1. Tight coupling
  2. Low cohesion
  3. High cohesion
  4. Loose coupling
  5. Weak encapsulation
  6. Strong encapsulation
Answer: E

Q : 72 Given that:

Gadget has-a Sprocket and Gadget has-a Spring and Gadget is-a Widget and Widget has-a Sprocket
Which two code fragments represent these relationships? (Choose two.)
  1. class Widget { Sprocket s; } class Gadget extends Widget { Spring s; }
  2. class Widget { } class Gadget extends Widget { Spring s1; Sprocket s2; }
  3. class Widget { Sprocket s1; Spring s2; } class Gadget extends Widget { }
  4. class Gadget { Spring s; } class Widget extends Gadget{ Sprocket s; }
  5. class Gadget { } class Widget extends Gadget{ Sprocket s1; Spring s2; }
  6. class Gadget { Spring s1; Sprocket s2; } class Widget extends Gadget{ }
Answer: A,C

Q : 73

11. class Alpha { 
12. public void foo() { System.out.print("Afoo "); } 
13. } 
14. public class Beta extends Alpha { 
15. public void foo() { System.out.print("Bfoo "); } 
16. public static void main(String[] args) { 
17. Alpha a = new Beta(); 
18. Beta b = (Beta)a; 
19. a.foo(); 
20. b.foo(); 
21. } 
22. } 
What is the result?
  1. Afoo Afoo
  2. Afoo Bfoo
  3. Bfoo Afoo
  4. Bfoo Bfoo
  5. Compilation fails.
  6. An exception is thrown at runtime.
Answer: D

Q : 74 Click the Exhibit button.

 
11. class Payload { 
12. private int weight; 
13. public Payload(int wt) { weight = wt; } 
13. public void setWeight(mt w) { weight = w; } 
15. public String toString { return Integer.toString(weight); } 
16. } 
17. 
18. public class TestPayload { 
19. static void changePayload(Payload p) { 
20. /* insert code here */ 
21. } 
22. 
23. public static void main(String[] args) { 
24. Payload p = new Payload();
25. p.setWeight(1024); 
26. changePayload(p); 
27. System.out.println("The value of p is "+ p); 
28. } 
29. }
Which statement, placed at line 20, causes the code to print "The value of p is 420."?
  1. p.setWeight(420);
  2. p.changePayload(420);
  3. p = new Payload(420);
  4. Payload.setWeight(420);
  5. p = Payload.setWeight(420);
  6. p = new Payload(); p.setWeight(420);
Answer: A

Q : 75, Which statement(s) are true? (Choose all that apply.)

  1. Has-a relationships always rely on inheritance.
  2. Has-a relationships always rely on instance variables.
  3. Has-a relationships always require at least two class types.
  4. Has-a relationships always rely on polymorphism.
  5. Has-a relationships are always tightly coupled.
Answer: ->? B is correct. -> A and D describe other OO topics. C is incorrect because a class can have an instance of itself. E is incorrect because while has-a relationships can lead to tight coupling, it is by no means always the case.

76. Given:

 
class Clidders { 
 public final void flipper() { 
 System.out.println("Clidder"); 
 }
} 

public class Clidlets extends Clidders { 
 public void flipper() { 
  System.out.println("Flip a Clidlet"); 
  super.flipper(); 
 } 
public static void main(String [] args) { 
   new Clidlets().flipper(); 
} 
} 
What is the result?
  1. Flip a Clidlet
  2. Flip a Clidder
  3. Flip a Clidder Flip a Clidlet
  4. Flip a Clidlet Flip a Clidder
  5. Compilation fails.
Answer:
  • E is correct. final methods cannot be overridden.
  • A, B, C, and D are incorrect based on the above.

77. Given:

 
public abstract interface Frobnicate { 
 public void twiddle(String s); 
} 
Which is a correct class? (Choose all that apply.)
  1. public abstract class Frob implements Frobnicate { public abstract void twiddle(String s) { } }
  2. public abstract class Frob implements Frobnicate { }
  3. public class Frob extends Frobnicate { public void twiddle(Integer i) { } }
  4. public class Frob implements Frobnicate { public void twiddle(Integer i) { } }
  5. public class Frob implements Frobnicate { public void twiddle(String i) { } public void twiddle(Integer s) { } }
Answer:
  • B is correct, an abstract class need not implement any or all of an interface's methods.
    E is correct, the class implements the interface method and additionally overloads the twiddle() method.
  • A is incorrect because abstract methods have no body.
    C is incorrect because classes implement interfaces they don't extend them.
    D is incorrect because overloading a method is not implementing it.

78. Given:

class Top {
 public Top(String s) {
 System.out.print("B");
 } } 
public class Bottom2 extends Top {
 public Bottom2(String s) {
 System.out.print("D");
 }
public static void main(String [] args) {
 new Bottom2("C");
 System.out.println(" ");
} 
} 
What is the result ?
  1. BD
  2. DB
  3. BDC
  4. DBC
  5. Compilation fails.
Answer:
  • E is correct. The implied super() call in Bottom2's constructor cannot be satisfied because there isn't a no-arg constructor in Top.
    A default, no-arg constructor is generated by the compiler only if the class has no constructor defined explicitly.
  • A, B, C, and D are incorrect based on the above.

79. Select the two statements that best indicate a situation with low coupling. (Choose two.)

  1. The attributes of the class are all private.
  2. The class refers to a small number of other objects.
  3. The object contains only a small number of variables.
  4. The object is referred to using an anonymous variable, not directly.
  5. The reference variable is declared for an interface type, not a class. The interface provides a small number of methods.
  6. It is unlikely that changes made to one class will require any changes in another.
Answer:
  • E and F are correct. Only having access to a small number of methods implies limited coupling.
    If the access is via a reference of interface type,
    it may be argued that there is even less opportunity for coupling as the class type itself is not visible.
    Stating that changes in one part of a program are unlikely to cause consequences in another part is really the essence of low coupling.
    There is no such thing as an anonymous variable. Referring to only a small number of other objects might imply low coupling, but if each object has many methods, and all are used, then coupling is high.
    Variables (attributes) in a class should usually be private, but this describes encapsulation, rather than low coupling.
    Of course, good encapsulation tends to reduce coupling as a consequence.
  • A, B, C and D are incorrect based on the preceding treatise.

80. Given:

 
class Clidder {
 private final void flipper() {
 System.out.println("Clidder");
 } 
} 
public class Clidlet extends Clidder {
 public final void flipper() {
 System.out.println("Clidlet");
 } 
public static void main(String [] args) {
 new Clidlet().flipper(); 
} 
} 

What is the result ?

  1. Clidlet
  2. Clidder
  3. Clidder Clidlet
  4. Clidlet Clidder
  5. Compilation fails.
Answer:
  • A is correct.
    Although a final method cannot be overridden, in this case, the method is private, and therefore hidden.
    The effect is that a new, accessible, method flipper is created. Therefore, no polymorphism occurs in this example,
    the method invoked is simply that of the child class, and no error occurs.
  • B, C, D, and E are incorrect based on the preceding.

81. Given:

 
1. class Plant { 
2. String getName() { return "plant"; } 
3. Plant getType() { return this; } 
4. } 
5. class Flower extends Plant { 
6. // insert code here 
7. } 
8. class Tulip extends Flower { } 
Which statement(s), inserted at line 6, will compile? (Choose all that apply.)
  1. Flower getType() { return this; }
  2. String getType() { return "this"; }
  3. Plant getType() { return this; }
  4. Tulip getType() { return new Tulip(); }
Answer:
  • A, C, and D are correct. A and D are examples of co-variant returns,
    i.e., Flower and Tulip are both subtypes of Plant.
  • B is incorrect, String is not a subtype of Plant.

82. Given:

1. class Zing { 
2. protected Hmpf h; 
3. } 
4. class Woop extends Zing { } 
5. class Hmpf { } 

Which is true? (Choose all that apply.)

  1. Woop is-a Hmpf and has-a Zing.
  2. Zing is-a Woop and has-a Hmpf.
  3. Hmpf has-a Woop and Woop is-a Zing.
  4. Woop has-a Hmpf and Woop is-a Zing.
  5. Zing has-a Hmpf and Zing is-a Woop.
Answer:
  • D is correct, Woop inherits a Hmpf from Zing.
  • A, B, C, and E are incorrect based on the preceding.

83. Given:

1. class Programmer { 
2. Programmer debug() { return this; } 
3. } 
4. class SCJP extends Programmer { 
5. // insert code here 
6. } 

Which, inserted at line 5, will compile? (Choose all that apply.)

  1. Programmer debug() { return this; }
  2. SCJP debug() { return this; }
  3. Object debug() { return this; }
  4. int debug() { return 1; }
  5. int debug(int x) { return 1; }
  6. Object debug(int x) { return this; }
Answer:
  • A, B, E, and F are correct. A and B are examples of overriding, specifically,
    B is an example of overriding using a covariant return. E and F are examples of overloading.
  • C and D are incorrect. They are illegal overrides because their return types are incompatible.
    They are illegal overloads because their arguments did not change.

84. Given:

class Uber { 
 static int y = 2; 
 Uber(int x) { 
 this(); 
 y = y * 2; 
 } 
Uber() { 
 y++; 
 } 
} 

class Minor extends Uber { 
 Minor() { super(y); y = y + 3; } 
 public static void main(String [] args) { 
  new Minor(); 
  System.out.println(y); 
 } 
} 

What is the result ?

  1. 6
  2. 7
  3. 8
  4. 9
  5. Compilation fails.
  6. An exception is thrown.
Answer:
  • D is correct. Minor's constructor makes an explicit call to Uber's 1-arg constructor,
    which makes an explicit (this) call to Uber's no-arg constructor,
    which increments y, then returns to the 1-arg constructor, which multiples y * 2, and then returns to Minor's constructor,
    which adds 3 to y.
  • A, B, C, E, and F are incorrect based on the preceding.

85. Which statement(s) are true? (Choose all that apply.)

  1. Cohesion is the OO principle most closely associated with hiding implementation details.
  2. Cohesion is the OO principle most closely associated with making sure that classes know about other classes only through their APIs.
  3. Cohesion is the OO principle most closely associated with making sure that a class is designed with a single, well-focused purpose.
  4. Cohesion is the OO principle most closely associated with allowing a single object to be seen as having many types.
Answer:
  • Answer C is correct.
  • A refers to encapsulation, B refers to coupling, and D refers to polymorphism.

86. Given:

 
1. class Dog { } 
2. class Beagle extends Dog { } 
3. 
4. class Kennel { 
5. public static void main(String [] arfs) { 
6. Beagle b1 = new Beagle(); 
7. Dog dog1 = new Dog(); 
8. Dog dog2 = b1; 
9. // insert code here 
10. } 
11. }

Which, inserted at line 9, will compile? (Choose all that apply.)

  1. Beagle b2 = (Beagle) dog1;
  2. Beagle b3 = (Beagle) dog2;
  3. Beagle b4 = dog2;
  4. None of the above statements will compile
Answer:
  • A and B are correct. However, at runtime, A will throw a ClassCastException because dog1 refers to a Dog object,
    which can't necessarily do Beagle stuff.
  • C and D are incorrect based on the preceding.

87. Given the following,

1. class X { void do1() { } } 
2. class Y extends X { void do2() { } } 
3. 
4. class Chrome { 
5. public static void main(String [] args) { 
6. X x1 = new X(); 
7. X x2 = new Y(); 
8. Y y1 = new Y(); 
9. // insert code here 10. } 
11. } 

Which, inserted at line 9, will compile? (Choose all that apply.)

  1. x2.do2();
  2. (Y)x2.do2();
  3. ((Y)x2).do2();
  4. None of the above statements will compile.
Answer:
  • C is correct. Before you can invoke Y's do2 method you have to cast x2 to be of type Y.
    Statement B looks like a proper cast but without the second set of parentheses, the compiler thinks it's an incomplete statement.
  • A, B and D are incorrect based on the preceding.

88. Given:

class Bird { { System.out.print("b1 "); } 
public Bird() { System.out.print("b2 "); } 
} 

class Raptor extends Bird { 
static { System.out.print("r1 "); } 
public Raptor() { System.out.print("r2 "); } { System.out.print("r3 "); } 
static { System.out.print("r4 "); } } 
class Hawk extends Raptor { 
public static void main(String[] args) { System.out.print("pre "); 
new Hawk(); System.out.println("hawk "); } } 

What is the result ?

  1. pre b1 b2 r3 r2 hawk
  2. pre b2 b1 r2 r3 hawk
  3. pre b2 b1 r2 r3 hawk r1 r4
  4. r1 r4 pre b1 b2 r3 r2 hawk
  5. r1 r4 pre b2 b1 r2 r3 hawk
  6. pre r1 r4 b1 b2 r3 r2 hawk
  7. pre r1 r4 b2 b1 r2 r3 hawk
  8. The order of output cannot be predicted.
  9. Compilation fails.
Answer:
  • D is correct. Static init blocks are executed at class loading time, instance init blocks run right after the call to super() in a constructor.
    When multiple init blocks of a single type occur in a class, they run in order, from the top down.
  • A, B, C, E, F, G, H, and I are incorrect based on the above.


BACK