nerdexam
Oracle

1Z0-850 · Question #32

Given: 1. class X { 2. private Y y; 3. public X(Y y) { this.y = y; } 4. } 5. class Y { 6. private X x; 7. public Y() { } 8. public Y(X x) { this.x = x; } 9. } The instance variable y is intended to…

The correct answer is A. X x1 = new X(new Y()); X x2 = new X(new Y()). See the full explanation below for the reasoning.

Question

Given:
  1. class X {
  2. private Y y;
  3. public X(Y y) { this.y = y; }
  4. }
  5. class Y {
  6. private X x;
  7. public Y() { }
  8. public Y(X x) { this.x = x; }
  9. } The instance variable y is intended to represent the composition relationship "X is composed of Y". Which code correctly maintains this meaning?

Options

  • AX x1 = new X(new Y()); X x2 = new X(new Y());
  • BX xx = new X(null); Y y1 = new Y(xx); Y y2 = new Y(xx);
  • CX x1 = new Y(); X x1 = new Y(yy); X x2 = new Y(xy);
  • DY y1 = new Y(new X(null)); Y y2 = new Y(new X(null));

How the community answered

(54 responses)
  • A
    85% (46)
  • B
    2% (1)
  • C
    6% (3)
  • D
    7% (4)

Community Discussion

No community discussion yet for this question.

Full 1Z0-850 Practice