Aquí teneis las preguntas del Tema 2, según la preparación de nuestro curso
Question 1
Which statement(s) are true? (Choose all that apply.)
a. Has-a relationships always rely on inheritance.
b. Has-a relationships always rely on instance variables.
c. Has-a relationships always require at least two class types.
d. Has-a relationships always rely on polymorphism.
e. Has-a relationships are always tightly coupled.
Answer: B
Question 2
Given the following,
1. interface Base {
2. boolean m1 ();
3. byte m2(short s);
4. }
Which code fragments will compile? (Choose all that apply.)
a.
interface Base2 implements Base { }
b.
abstract class Class2 extends Base {
public boolean m1() { return true; } }
c.
abstract class Class2 implements Base { }
d.
abstract class Class2 implements Base {
public boolean m1() { return (true); } }
e.
class Class2 implements Base {
boolean m1() { return false; }
byte m2(short s) { return 42; } }
Answer: C and D
Question 3
Which declare a compilable abstract class? (Choose all that apply.)
a. public abstract class Canine { public Bark speak(); }
b. public abstract class Canine { public Bark speak() { return null; } }
c. public class Canine { public abstract Bark speak(); }
d. public class Canine abstract { public abstract Bark speak(); }
Answer: B
Question 4
Which is true? (Choose all that apply.)
a. "X extends Y" is correct if and only if X is a class and Y is an interface.
b. "X extends Y" is correct if and only if X is an interface and Y is a class.
c. "X extends Y" is correct if X and Y are either both classes or both interfaces.
d. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces.
Answer: C
Question 5
Which method names follow the JavaBeans standard? (Choose all that apply.)
a. addSize
b. getCust
c. deleteRep
d. isColorado
e. putDimensions
Answer: B and D
Question 6
Given:
1. enum Animals {
2. DOG("woof"), CAT("meow"), FISH("burble");
3. String sound;
4. Animals(String s) { sound = s; }
5. }
6. class TestEnum {
7. static Animals a;
8. public static void main(String [] args) {
9. System.out.println(a.DOG.sound + " " + a.FISH.sound);
10. }
11. }
What is the result?
a. woof burble
b. Multiple compilation errors
c. Compilation fails due to an error on line 2
d. Compilation fails due to an error on line 3
e. Compilation fails due to an error on line 4
f. Compilation fails due to an error on line 9
Answer: A
Question 7
Given:
1. enum A { A }
2. class E2 {
3. enum B { B }
4. void C() {
5. enum D { D }
6. }
7. }
Which statements are true? (Choose all that apply.)
a. The code compiles.
b. If only line 1 is removed the code compiles.
c. If only line 3 is removed the code compiles.
d. If only line 5 is removed the code compiles.
e. If lines 1 and 3 are removed the code compiles.
f. If lines 1, 3 and 5 are removed the code compiles.
Answer: D and F
Question 8
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.)
a. Programmer debug() { return this; }
b. SCJP debug() { return this; }
c. Object debug() { return this; }
d. int debug() { return 1; }
e. int debug(int x) { return 1; }
f. Object debug(int x) { return this; }
Answer: A, B, E and F
Question 9
Given:
1. class Uber {
2. static int y = 2;
3. Uber(int x) { this(); y = y * 2; }
4. Uber() { y++; }
5. }
6. class Minor extends Uber {
7. Minor() { super( y ); y = y + 3; }
8. public static void main(String [] args) {
9. new Minor();
10. System.out.println( y );
11. } }
What is the result?
Seleccione una respuesta.
a. 6
b. 7
c. 8
d. 9
e. Compilation fails.
f. An exception is thrown.
Answer: D
Question 10
Which statement(s) are true? (Choose all that apply.)
a. Cohesion is the OO principle most closely associated with hiding implementation details.
b. Cohesion is the OO principle most closely associated with making sure that classes know about other classes only through their APIs.
c. Cohesion is the OO principle most closely associated with making sure that a class is designed with a single, well-focused purpose.
d. Cohesion is the OO principle most closely associated with allowing a single object to be seen as having many types.
Answer: C
Question 11
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.)
a. Beagle b2 = (Beagle) dog1;
b. Beagle b3 = (Beagle) dog2;
c. Beagle b4 = dog2;
d. None of the above statements will compile
Answer: A and B
Question 12
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. } }
Which, inserted at line 9, will compile? (Choose all that apply.)
a. x2.do2();
b. (Y)x2.do2();
c. ((Y)x2).do2();
d. None of the above statements will compile.
Answer: C
Question 13
Given:
1. class Clidders {
2. public final void flipper() { System.out.println("Clidder"); }
3. }
4. public class Clidlets extends Clidders {
5. public void flipper() {
6. System.out.println("Flip a Clidlet");
7. super.flipper();
8. }
9. public static void main(String [] args) {
10. new Clidlets().flipper();
11. }
12.}
What is the result?
a. Flip a Clidlet
b. Flip a Clidder
c. Flip a Clidder
d. Flip a Clidlet
e. Flip a Clidlet
f. Flip a Clidder
g. Compilation fails.
Answer: G
Question 14
Given:
public abstract interface Frobnicate { public void twiddle(String s); }
Which is a correct class? (Choose all that apply.)
a.
public abstract class Frob implements Frobnicate {
public abstract void twiddle(String s) { }
}
b.
public abstract class Frob implements Frobnicate { }
c.
public class Frob extends Frobnicate {
public void twiddle(Integer i) { }
}
d.
public class Frob implements Frobnicate {
public void twiddle(Integer i) { }
}
e.
public class Frob implements Frobnicate {
public void twiddle(String i) { }
public void twiddle(Integer s) { }
}
Answer: B and E
Question 15
Given:
1. class Top {
2. public Top(String s) { System.out.print("B"); }
3. }
4. public class Bottom2 extends Top {
5. public Bottom2(String s) { System.out.print("D"); }
6. public static void main(String [] args) {
7. new Bottom2("C");
8. System.out.println(" ");
9. } }
What is the result?
Seleccione una respuesta.
a. BD
b. DB
c. BDC
d. DBC
e. Compilation fails.
Answer: E
Question 16
Select the two statements that best indicate a situation with low coupling. (Choose two.)
a. The attributes of the class are all private.
b. The class refers to a small number of other objects.
c. The object contains only a small number of variables.
d. The object is referred to using an anonymous variable, not directly.
e. The reference variable is declared for an interface type, not a class. The interface provides a small number of methods.
f. It is unlikely that changes made to one class will require any changes in another.
Answer: E and F
Question 17
Given:
1. class Clidder {
2. private final void flipper() { System.out.println("Clidder"); }
3. }
4. public class Clidlet extends Clidder {
5. public final void flipper() { System.out.println("Clidlet"); }
6. public static void main(String [] args) {
7. new Clidlet().flipper();
8. } }
What is the result?
Seleccione una respuesta.
a. Clidlet
b. Clidder
c. Clidder
Clidlet
d. Clidlet
Clidder
e. Compilation fails.
Answer: A
Question 18
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.)
a. Flower getType() { return this; }
b. String getType() { return "this"; }
c. Plant getType() { return this; }
d. Tulip getType() { return new Tulip(); }
Answer: A, C and D
Question 19
Given:
public class MyOuter {
public static class MyInner { public static void foo() { } }
}
Which, if placed in a class other than MyOuter or MyInner, instantiates an instance of the
nested class?
a.
MyOuter.MyInner m = new MyOuter.MyInner();
b.
MyOuter.MyInner mi = new MyInner();
c.
MyOuter m = new MyOuter();
MyOuter.MyInner mi = m.new MyOuter.MyInner();
d.
MyInner mi = new MyOuter.MyInner();
Answer: A
Question 20
Given:
1. public class HorseTest {
2. public static void main(String[] args) {
3. class Horse {
4. public String name;
5. public Horse(String s) {
6. name = s;
7. }
8. }
9. Object obj = new Horse("Zippo");
10. Horse h = (Horse) obj;
11. System.out.println(h.name);
12. }
13. }
What is the result?
Seleccione una respuesta.
a. An exception occurs at runtime at line 10.
b. Zippo
c. Compilation fails because of an error on line 3.
d. Compilation fails because of an error on line 9.
e. Compilation fails because of an error on line 10.
f. Compilation fails because of an error on line 11.
Answer: B
Question 21
Given:
1. public class HorseTest {
2. public static void main(String[] args) {
3. class Horse {
4. public String name;
5. public Horse(String s) {
6. name = s;
7. }
8. }
9. Object obj = new Horse("Zippo");
10. System.out.println(obj.name);
11. }
12. }
What is the result?
Seleccione una respuesta.
a. An exception occurs at runtime at line 10.
b. Zippo
c. Compilation fails because of an error on line 3.
d. Compilation fails because of an error on line 9.
e. Compilation fails because of an error on line 10.
Answer: E
Question 22
Given:
1. public abstract class AbstractTest {
2. public int getNum() {
3. return 45;
4. }
5. public abstract class Bar {
6. public int getNum() {
7. return 38;
8. }
9. }
10. public static void main(String[] args) {
11. AbstractTest t = new AbstractTest() {
12. public int getNum() {
13. return 22;
14. }
15. };
16. AbstractTest.Bar f = t.new Bar() {
17. public int getNum() {
18. return 57;
19. }
20. };
21. System.out.println(f.getNum() + " " + t.getNum());
22. } }
What is the result?
Seleccione una respuesta.
a. 57 22
b. 45 38
c. 45 57
d. An exception occurs at runtime.
e. Compilation fails.
Answer: A
Question 23
Which are true about a static nested class? (Choose all that apply.)
Seleccione al menos una respuesta.
a. You must have a reference to an instance of the enclosing class in order to instantiate it.
b. It does not have access to non-static members of the enclosing class.
c. Its variables and methods must be static.
d. If the outer class is named MyOuter, and the nested class is named MyInner, it can be instantiated using new MyOuter.MyInner();.
e. It must extend the enclosing class.
Answer: B and D
Question 24
Given:
public interface Runnable { void run(); }
Which construct an anonymous inner class instance? (Choose all that apply.)
a.
Runnable r = new Runnable() { };
b.
Runnable r = new Runnable(public void run() { });
c.
Runnable r = new Runnable { public void run(){ } };
d.
Runnable r = new Runnable() {public void run{ } };
e.
System.out.println(new Runnable() {public void run() { } });
f.
System.out.println(new Runnable(public void run() { } ));
Answer: E
Question 25
Given:
1. class Boo {
2. Boo(String s) { }
3. Boo() { }
4. }
5. class Bar extends Boo {
6. Bar() { }
7. Bar(String s) {super(s);}
8. void zoo() {
9. // insert code here
10. }
11. }
Which create an anonymous inner class from within class Bar? (Choose all that apply.)
Seleccione al menos una respuesta.
a. Boo f = new Boo(24) { };
b. Boo f = new Bar() { };
c. Boo f = new Boo() {String s; };
d. Bar f = new Boo(String s) { };
e. Boo f = new Boo.Bar(String s) { };
Answer: B and C
Question 26
Given:
1. class Foo {
2. class Bar{ }
3. }
4. class Test {
5. public static void main(String[] args) {
6. Foo f = new Foo();
7. // Insert code here
8. }
9. }
Which, inserted at line 7, creates an instance of Bar? (Choose all that apply.)
a. Foo.Bar b = new Foo.Bar();
b. Foo.Bar b = f.new Bar();
c. Bar b = new f.Bar();
d. Bar b = f.new Bar();
e. Foo.Bar b = new f.Bar();
Answer: B
Question 27
Which are true about a method-local inner class? (Choose all that apply.)
a. It must be marked final.
b. It can be marked abstract.
c. It can be marked public.
d. It can be marked static.
e. It can access private members of the enclosing class.
Answer: B and E
Question 28
Which are true about an anonymous inner class? (Choose all that apply.)
a. It can extend exactly one class and implement exactly one interface.
b. It can extend exactly one class and can implement multiple interfaces.
c. It can extend exactly one class or implement exactly one interface.
d. It can implement multiple interfaces regardless of whether it also extends a class.
e. It can implement multiple interfaces if it does not extend a class.
Answer: C
Question 29
Given:
1. public class Foo {
2. Foo() {System.out.print("foo");}
3. class Bar{
4. Bar() {System.out.print("bar");}
5. public void go() {System.out.print("hi");}
6. }
7. public static void main(String[] args) {
8. Foo f = new Foo();
9. f.makeBar();
10. }
11. void makeBar() {
12. (new Bar() {}).go();
13. }
14. }
What is the result?
Seleccione una respuesta.
a. Compilation fails.
b. An error occurs at runtime.
c. foobarhi
d. barhi
e. hi
f. foohi
Answer: C
Question 30
Given:
1. public class TestObj {
2. public static void main(String[] args) {
3. Object o = new Object() {
4. public boolean equals(Object obj) {
5. return true;
6. }
7. }
8. System.out.println(o.equals("Fred"));
9. }
10. }
What is the result?
a. An exception occurs at runtime.
b. true
c. fred
d. Compilation fails because of an error on line 3.
e. Compilation fails because of an error on line 4.
f. Compilation fails because of an error on line 8.
g. Compilation fails because of an error on a line other than 3, 4, or 8.
Answer: G
Question 31
Given:
1. class Zing {
2. protected Hmpf h;
3. }
4. class Woop extends Zing { }
5. class Hmpf { }
Which is true? (Choose all that apply.)
a. Woop is-a Hmpf and has-a Zing.
b. Zing is-a Woop and has-a Hmpf.
c. Hmpf has-a Woop and Woop is-a Zing.
d. Woop has-a Hmpf and Woop is-a Zing.
e. Zing has-a Hmpf and Zing is-a Woop.
Answer: D