1. Question: Which is true about an anonymous inner class?

    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.

    Note: Option C is correct because the syntax of an anonymous inner class allows for only one named type after the new, and that type must be either a single interface (in which case the anonymous class implements that one interface) or a single class (in which case the anonymous class extends that one class). Option A, B, D, and E are all incorrect because they don't follow the syntax rules described in the response for answer Option C.
    1. Report
  2. Question: class Boo { Boo(String s) { } Boo() { } } class Bar extends Boo { Bar() { } Bar(String s) {super(s);} void zoo() { // insert code here } } which one create an anonymous inner class from within class Bar?

    A
    Boo f = new Boo(24) { };

    B
    Boo f = new Bar() { };

    C
    Bar f = new Boo(String s) { };

    D
    Boo f = new Boo.Bar(String s) { };

    Note: Option B is correct because anonymous inner classes are no different from any other class when it comes to polymorphism. That means you are always allowed to declare a reference variable of the superclass type and have that reference variable refer to an instance of a subclass type, which in this case is an anonymous subclass of Bar. Since Bar is a subclass of Boo, it all works. Option A is incorrect because it passes an int to the Boo constructor, and there is no matching constructor in the Boo class. Option C is incorrect because it violates the rules of polymorphism—you cannot refer to a superclass type using a reference variable declared as the subclass type. The superclass is not guaranteed to have everything the subclass has. Option D uses incorrect syntax.
    1. Report
  3. Question: Which is true about a method-local inner class?

    A
    It must be marked final.

    B
    It can be marked abstract.

    C
    It can be marked public.

    D
    It can be marked static.

    Note: Option B is correct because a method-local inner class can be abstract, although it means a subclass of the inner class must be created if the abstract class is to be used (so an abstract method-local inner class is probably not useful). Option A is incorrect because a method-local inner class does not have to be declared final (although it is legal to do so). C and D are incorrect because a method-local inner class cannot be made public (remember-you cannot mark any local variables as public), or static.
    1. Report
  4. Question: Which statement is true about a static nested class?

    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 nonstatic members of the enclosing class.

    C
    It's variables and methods must be static.

    D
    It must extend the enclosing class.

    Note: Option B is correct because a static nested class is not tied to an instance of the enclosing class, and thus can't access the nonstatic members of the class (just as a static method can't access nonstatic members of a class). Option A is incorrect because static nested classes do not need (and can't use) a reference to an instance of the enclosing class. Option C is incorrect because static nested classes can declare and define nonstatic members. Option D is wrong because it just is. There's no rule that says an inner or nested class has to extend anything.
    1. Report
  5. Question: Which constructs an anonymous inner class instance?

    A
    Runnable r = new Runnable() { };

    B
    Runnable r = new Runnable(public void run() { });

    C
    Runnable r = new Runnable { public void run(){}};

    D
    System.out.println(new Runnable() {public void run() { }});

    Note: D is correct. It defines an anonymous inner class instance, which also means it creates an instance of that new anonymous class at the same time. The anonymous class is an implementer of the Runnable interface, so it must override the run() method of Runnable. A is incorrect because it doesn't override the run() method, so it violates the rules of interface implementation. B and C use incorrect syntax.
    1. Report
  6. Question: class Foo { class Bar{ } } class Test { public static void main (String [] args) { Foo f = new Foo(); /* Line 10: Missing statement ? */ } } which statement, inserted at line 10, creates an instance of Bar?

    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();

    Note: Option B is correct because the syntax is correct-using both names (the enclosing class and the inner class) in the reference declaration, then using a reference to the enclosing class to invoke new on the inner class. Option A, C and D all use incorrect syntax. A is incorrect because it doesn't use a reference to the enclosing class, and also because it includes both names in the new. C is incorrect because it doesn't use the enclosing class name in the reference variable declaration, and because the new syntax is wrong. D is incorrect because it doesn't use the enclosing class name in the reference variable declaration.
    1. Report
  7. Question: public class MyOuter { public static class MyInner { public static void foo() { } } } which statement, 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();

    Note: MyInner is a static nested class, so it must be instantiated using the fully-scoped name of MyOuter.MyInner. Option B is incorrect because it doesn't use the enclosing name in the new. Option C is incorrect because it uses incorrect syntax. When you instantiate a nested class by invoking new on an instance of the enclosing class, you do not use the enclosing name. The difference between Option A and C is that Option C is calling new on an instance of the enclosing class rather than just new by itself. Option D is incorrect because it doesn't use the enclosing class name in the variable declaration.
    1. Report
  8. Question: What is the value of "d" after this line of code has been executed? double d = Math.round ( 2.5 + Math.random() );

    A
    2

    B
    3

    C
    4

    D
    2.5

    Note: The Math.random() method returns a number greater than or equal to 0 and less than 1 . Since we can then be sure that the sum of that number and 2.5 will be greater than or equal to 2.5 and less than 3.5, we can be sure that Math.round() will round that number to 3. So Option B is the answer.
    1. Report
  9. Question: Which of the following would compile without error?

    A
    int a = Math.abs(-5);

    B
    int b = Math.abs(5.0);

    C
    int c = Math.abs(5.5F);

    D
    int d = Math.abs(5L);

    Note: The return value of the Math.abs() method is always the same as the type of the parameter passed into that method. In the case of A, an integer is passed in and so the result is also an integer which is fine for assignment to "int a". The values used in B, C & D respectively are a double, a float and a long. The compiler will complain about a possible loss of precision if we try to assign the results to an "int".
    1. Report
  10. Question: Which of the following are valid calls to Math.max? Math.max(1,4) Math.max(2.3, 5) Math.max(1, 3, 5, 7) Math.max(-1.5, -2.8f)

    A
    1, 2 and 4

    B
    2, 3 and 4

    C
    1, 2 and 3

    D
    3 and 4

    Note: (1), (2), and (4) are correct. The max() method is overloaded to take two arguments of type int, long, float, or double. (3) is incorrect because the max() method only takes two arguments.
    1. Report
Copyright © 2024. Powered by Intellect Software Ltd