nerdexam
Oracle

1Z0-850 · Question #28

Given: 1. public class Foo { 2. int size; 3. public static void main(String[] args) { 4. Foo f = new Foo(); 5. f.setSize(5); 6. Foo g = f.go(f); 7. System.out.print(f.size + " : " + g.size); 8. } 9…

The correct answer is D. 2 : 2. See the full explanation below for the reasoning.

Question

Given:
  1. public class Foo {
  2. int size;
  3. public static void main(String[] args) {
  4. Foo f = new Foo();
  5. f.setSize(5);
  6. Foo g = f.go(f);
  7. System.out.print(f.size + " : " + g.size);
  8. }
  9. void setSize(int s) {
  10. size = s;
  11. }
  12. public Foo go(Foo g) {
  13. g.setSize(2);
  14. return g;
  15. }
  16. } What is the result?

Options

  • ACompilation fails.
  • B5 : 5
  • C5 : 2
  • D2 : 2

How the community answered

(24 responses)
  • A
    17% (4)
  • B
    4% (1)
  • C
    8% (2)
  • D
    71% (17)

Community Discussion

No community discussion yet for this question.

Full 1Z0-850 Practice