class Bar { } class Test { Bar doBar() { Bar b = new Bar(); /* Line 6 */ return b; /* Line 7 */ } public static void main (String args[]) { Test t = new Test(); /* Line 11 */ Bar newBar = t.doBar(); /* Line 12 */ System.out.println("newBar"); newBar = new Bar(); /* Line 14 */ System.out.println("finishing"); /* Line 15 */ } }
Option A is wrong. This actually protects the object from garbage collection.
Option C is wrong. Because the reference in the doBar() method is returned on line 7 and is stored in newBar on line 12. This preserver the object created on line 6.
Option D is wrong. Not applicable because the object is eligible for garbage collection after line 14.
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.
public class MyOuter { public static class MyInner { public static void foo() { } } }
MyOuter.MyInner mi = m.new 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.
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.
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.
class Boo { Boo(String s) { } Boo() { } } class Bar extends Boo { Bar() { } Bar(String s) {super(s);} void zoo() { // insert code here } }
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.
void start() { A a = new A(); B b = new B(); a.s(b); b = null; /* Line 5 */ a = null; /* Line 6 */ System.out.println("start completed"); /* Line 7 */ }
Option A is wrong. I found 4 delete() methods in all of the Java class structure. They are:
None of these destroy the object to which they belong.
Option B is wrong. I found 19 finalize() methods. The most interesting, from this questions point of view, was the finalize() method in class java.lang.Object which is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. This method does not destroy the object to which it belongs.
Option C is wrong. But it is interesting. The Runtime class has many methods, two of which are:
class Test { private Demo d; void start() { d = new Demo(); this.takeDemo(d); /* Line 7 */ } /* Line 8 */ void takeDemo(Demo demo) { demo = null; demo = new Demo(); } }
Option A is wrong. The variable d is a member of the Test class and is never directly set to null.
Option B is wrong. A copy of the variable d is set to null and not the actual variable d.
Option C is wrong. The variable d exists outside the start() method (it is a class member). So, when the start() method finishes the variable d still holds a reference.
public class X { public static void main(String [] args) { X x = new X(); X x2 = m1(x); /* Line 6 */ X x4 = new X(); x2 = x4; /* Line 8 */ doComplexStuff(); } static X m1(X mx) { mx = new X(); return mx; } }
class X2 { public X2 x; public static void main(String [] args) { X2 x2 = new X2(); /* Line 6 */ X2 x3 = new X2(); /* Line 7 */ x2.x = x3; x3.x = x2; x2 = new X2(); x3 = x2; /* Line 11 */ doComplexStuff(); } }
Comments
There are no comments.Copyright ©CuriousTab. All rights reserved.