Practice mode Java

Given class T { int n; void set(int n) { n = n; } }, what does T t = new T(); t.set(9); System.out.println(t.n); print?

Correct answer C. 0

Explanation

Inside set, the parameter n shadows the field n, so n = n merely assigns the parameter to itself and the field stays untouched. Instance fields of type int default to 0, so 0 is printed. Writing this.n = n would reach the field and store 9. The code compiles without error, although many tools warn about it.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

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

Links and website addresses are not allowed in comments.