| 7.1 | What will be the result of attempting to compile and run the following code? public class MyClass { public static void main(String[] args) { Outer objRef = new Outer(); System.out.println(objRef.createInner().getSecret()); } } class Outer { private int secret; Outer() { secret = 123; } class Inner { int getSecret() { return secret; } } Inner createInner() { return new Inner(); } } Select the one correct answer. -
The code will fail to compile because the class Inner cannot be declared within the class Outer . -
The code will fail to compile because the method createInner() cannot be allowed to pass objects of the class Inner to methods outside of the class Outer . -
The code will fail to compile because the secret field is not accessible from the method getSecret() . -
The code will fail to compile because the method getSecret() is not visible from the main() method in the class MyClass . -
The code will compile without error and will print 123 when run. |
| 7.3 | What will be the result of attempting to compile and run the following code? public class MyClass { public static void main(String[] args) { State st = new State(); System.out.println(st.getValue()); State.Memento mem = st.memento(); st.alterValue(); System.out.println(st.getValue()); mem.restore(); System.out.println(st.getValue()); } public static class State { protected int val = 11; int getValue() { return val; } void alterValue() { val = (val + 7) % 31; } Memento memento() { return new Memento(); } class Memento { int val; Memento() { this.val = State.this.val; } void restore() { ((State) this).val = this.val; } } } } Select the one correct answer. -
The code will fail to compile since the static main() method tries to create a new instance of the static member class State . -
The code will fail to compile since the declaration of class State.Memento is not accessible from the main() method. -
The code will fail to compile since the non-static member class Memento declares a field with the same name as a field in the outer class State . -
The code will fail to compile since the State.this.val expression in the Memento constructor is invalid. -
The code will fail to compile since the ((State) this).val expression in the method restore() of the class Memento is invalid. -
The program compiles without error and prints 11 , 18 , and 11 when run. |
| 7.4 | What will be the result of attempting to compile and run the following program? public class Nesting { public static void main(String[] args) { B.C obj = new B().new C(); } } class A { int val; A(int v) { val = v; } } class B extends A { int val = 1; B() { super(2); } class C extends A { int val = 3; C() { super(4); System.out.println(B.this.val); System.out.println(C.this.val); System.out.println(super.val); } } } Select the one correct answer. -
The program will fail to compile. -
The program will compile without error and print 2 , 3 , and 4, in that order, when run. -
The program will compile without error and print 1 , 4 , and 2 , in that order, when run. -
The program will compile without error and print 1 , 3 , and 4 , in that order, when run. -
The program will compile without error and print 3 , 2 , and 1 , in that order, when run. |