1Z0-803 · Question #214
Given: How many objects have been created when the line / / do complex stuff is reached?
The correct answer is C. Four. The question asks to count the total number of distinct objects instantiated in a given Java code snippet before a specific line.
Question
Given:
How many objects have been created when the line / / do complex stuff is reached?
Exhibit
Options
- ATwo
- BThree
- CFour
- DSix
How the community answered
(40 responses)- A15% (6)
- B5% (2)
- C73% (29)
- D8% (3)
Why each option
The question asks to count the total number of distinct objects instantiated in a given Java code snippet before a specific line.
This would only count the two `Item` objects, ignoring the `String` literal objects created implicitly during instantiation.
This count would imply only one `String` object was created or an incorrect count of `Item` objects.
Four objects are created: two instances of the `Item` class via the `new` operator, and two distinct `String` literal objects ('Book' and 'Pen') that are passed as arguments to the `Item` constructors. The line `Item item3 = item1;` only creates a new reference, not a new object.
This count would require two additional objects to be instantiated beyond the four objects identified in the correct analysis.
Concept tested: Java object instantiation and String literals
Source: https://docs.oracle.com/javase/tutorial/java/javaOO/objects.html
Topics
Community Discussion
No community discussion yet for this question.
