Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
|
| Review Questions |
| 8.1 | What will be the result of compiling and running the following program?
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.
|
| 8.2 | Which statements about nested classes are true?
Select the two correct answers.
|
| 8.3 | What will be the result of compiling and running the following program?
Code View:
Scroll
/
Show All 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.
|
| 8.4 | What will be the result of compiling and running the following program?
Code View:
Scroll
/
Show All 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.
|
| 8.5 | Which statements about the following program are true?
public class Outer {
public void doIt() {
}
public class Inner {
public void doIt() {
}
}
public static void main(String[] args) {
new Outer().new Inner().doIt();
}
}Select the two correct answers.
|
| 8.6 | What will be the result of compiling and running the following program?
public class Outer {
private int innerCounter;
class Inner {
Inner() {innerCounter++;}
public String toString() {
return String.valueOf(innerCounter);
}
}
private void multiply() {
Inner inner = new Inner();
this.new Inner();
System.out.print(inner);
inner = new Outer().new Inner();
System.out.println(inner);
}
public static void main(String[] args) {
new Outer().multiply();
}
}Select the one correct answer.
|