What is the result?

Given:

1. public class Batman {
2. int squares = 81;
3. public static void main(String[] args) {
4. new Batman().go();
5. }
6. void go() {
7. incr(++squares);
8. System.out.println(squares);
9. }
10. void incr(int squares) { squares += 10; }
11. }

What is the result?

Given:

1. public class Batman {
2. int squares = 81;
3. public static void main(String[] args) {
4. new Batman().go();
5. }
6. void go() {
7. incr(++squares);
8. System.out.println(squares);
9. }
10. void incr(int squares) { squares += 10; }
11. }

What is the result?

A.
81

B.
82

C.
91

D.
92

E.
Compilation fails.

F.
An exception is thrown at runtime.

Explanation:
squares starts with 81
squares value is incremented before entering in incr method (82).
inside incr method another squares is incremented by 10 (shadowing)
when incr method exits class variable squares continues being 82.



Leave a Reply 0

Your email address will not be published. Required fields are marked *