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:
- class X {
- private Y y;
- public X(Y y) { this.y = y; }
- }
- class Y {
- private X x;
- public Y() { }
- public Y(X x) { this.x = x; }
- } 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)- A85% (46)
- B2% (1)
- C6% (3)
- D7% (4)
Community Discussion
No community discussion yet for this question.