1. 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
  2. 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
  3. 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
  4. Question: What will be the output of the program? public class Test { public static void main(String[] args) { int x = 0; assert (x > 0) ? "assertion failed" : "assertion passed" ; System.out.println("finished"); } }

    A
    finished

    B
    Compiliation fails.

    C
    An AssertionError is thrown and finished is output.

    D
    An AssertionError is thrown with the message "assertion failed."

    Note: Compilation Fails. You can't use the Assert statement in a similar way to the ternary operator. Don't confuse.
    1. Report
  5. Question: public class Test { public void foo() { assert false; /* Line 5 */ assert false; /* Line 6 */ } public void bar() { while(true) { assert false; /* Line 12 */ } assert false; /* Line 14 */ } } What causes compilation to fail?

    A
    Line 5

    B
    Line 6

    C
    Line 12

    D
    Line 14

    Note: Option D is correct. Compilation fails because of an unreachable statement at line 14. It is a compile-time error if a statement cannot be executed because it is unreachable. The question is now, why is line 20 unreachable? If it is because of the assert then surely line 6 would also be unreachable. The answer must be something other than assert. Examine the following: A while statement can complete normally if and only if at least one of the following is true: - The while statement is reachable and the condition expression is not a constant expression with value true. -There is a reachable break statement that exits the while statement. The while statement at line 11 is infinite and there is no break statement therefore line 14 is unreachable. You can test this with the following code: public class Test80 { public void foo() { assert false; assert false; } public void bar() { while(true) { assert false; break; } assert false; } }
    1. Report
  6. Question: What will be the output of the program? public class Test { public static int y; public static void foo(int x) { System.out.print("foo "); y = x; } public static int bar(int z) { System.out.print("bar "); return y = z; } public static void main(String [] args ) { int t = 0; assert t > 0 : bar(7); assert t > 1 : foo(8); /* Line 18 */ System.out.println("done "); } }

    A
    bar

    B
    bar done

    C
    foo done

    D
    Compilation fails

    Note: The foo() method returns void. It is a perfectly acceptable method, but because it returns void it cannot be used in an assert statement, so line 18 will not compile.
    1. Report
  7. Question: What will be the output of the program (when you run with the -ea option) ? public class Test { public static void main(String[] args) { int x = 0; assert (x > 0) : "assertion failed"; /* Line 6 */ System.out.println("finished"); } }

    A
    finished

    B
    Compilation fails.

    C
    An AssertionError is thrown.

    D
    An AssertionError is thrown and finished is output.

    Note: An assertion Error is thrown as normal giving the output "assertion failed". The word "finished" is not printed (ensure you run with the -ea option) Assertion failures are generally labeled in the stack trace with the file and line number from which they were thrown, and also in this case with the error's detail message "assertion failed". The detail message is supplied by the assert statement in line 6.
    1. Report
  8. Question: public class Test2 { public static int x; public static int foo(int y) { return y * 2; } public static void main(String [] args) { int z = 5; assert z > 0; /* Line 11 */ assert z > 2: foo(z); /* Line 12 */ if ( z < 7 ) assert z > 4; /* Line 14 */ switch (z) { case 4: System.out.println("4 "); case 5: System.out.println("5 "); default: assert z < 10; } if ( z < 10 ) assert z > 4: z++; /* Line 22 */ System.out.println(z); } } which line is an example of an inappropriate use of assertions?

    A
    Line 11

    B
    Line 12

    C
    Line 14

    D
    Line 22

    Note: Assert statements should not cause side effects. Line 22 changes the value of z if the assert statement is false. Option A is fine; a second expression in an assert statement is not required. Option B is fine because it is perfectly acceptable to call a method with the second expression of an assert statement. Option C is fine because it is proper to call an assert statement conditionally
    1. Report
  9. Question: You want sub classes in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?

    A
    public

    B
    private

    C
    protected

    D
    transient

    Note: Access modifiers dictate which classes, not which instances, may access features. Methods and variables are collectively known as members. Method and variable members are given access control in exactly the same way. private makes a member accessible only from within its own class protected makes a member accessible only to classes in the same package or subclass of the class default access is very similar to protected (make sure you spot the difference) default access makes a member accessible only to classes in the same package. public means that all other classes regardless of the package that they belong to, can access the member (assuming the class itself is visible) final makes it impossible to extend a class, when applied to a method it prevents a method from being overridden in a subclass, when applied to a variable it makes it impossible to reinitialise a variable once it has been initialised abstract declares a method that has not been implemented. transient indicates that a variable is not part of the persistent state of an object. volatile indicates that a thread must reconcile its working copy of the field with the master copy every time it accesses the variable. After examining the above it should be obvious that the access modifier that provides the most restrictions for methods to be accessed from the subclasses of the class from another package is C - protected. A is also a contender but C is more restrictive, B would be the answer if the constraint was the "same package" instead of "any package" in other words the subclasses clause in the question eliminates default.
    1. Report
  10. Question: public class Outer { public void someOuterMethod() { //Line 5 } public class Inner { } public static void main(String[] argv) { Outer ot = new Outer(); //Line 10 } } Which of the following code fragments inserted, will allow to compile?

    A
    new Inner(); //At line 5

    B
    new Inner(); //At line 10

    C
    new ot.Inner(); //At line 10

    D
    new Outer.Inner(); //At line 10

    Note: Option A compiles without problem. Option B gives error - non-static variable cannot be referenced from a static context. Option C package ot does not exist. Option D gives error - non-static variable cannot be referenced from a static context.
    1. Report
Copyright © 2025. Powered by Intellect Software Ltd