← Quiz: Nested and Anonymous Classes: all 10 questions
Quiz: Nested and Anonymous Classes · Question 3 of 10
What does this print? (code)
What does this print?
class Outer {
int x = 10;
class Inner {
int x = 20;
int sum() { return x + Outer.this.x; }
}
}
public class Main {
public static void main(String[] args) {
Outer o = new Outer();
Outer.Inner i = o.new Inner();
o.x = 5;
System.out.println(i.sum());
}
}
- A.30
- B.40
- C.10
- D.25
Answer & explanation
Correct answer: 25
x is the inner field (20); Outer.this.x reads the outer object's current value, 5.
Can you get the rest right without peeking?
Take the timed Quiz: Nested and Anonymous Classes quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →