1Z0-803 · Question #133
Given: public class MarkList { int num; public static void graceMarks(MarkList obj4) { obj4.num += 10; } public static void main (String[] args) { MarkList obj1 = new MarkList(); MarkList obj2 = obj1;
The correct answer is A. 1. This question assesses understanding of object creation in Java, specifically how the new keyword instantiates objects versus how assignment operators manage references.
Question
Given:
public class MarkList { int num; public static void graceMarks(MarkList obj4) { obj4.num += 10; } public static void main (String[] args) { MarkList obj1 = new MarkList(); MarkList obj2 = obj1; MarkList obj3 = null; obj2.num = 60; graceMarks(obj2); } } How many objects are created in the memory at runtime?
Options
- A1
- B2
- C3
- D4
How the community answered
(32 responses)- A81% (26)
- B3% (1)
- C6% (2)
- D9% (3)
Why each option
This question assesses understanding of object creation in Java, specifically how the `new` keyword instantiates objects versus how assignment operators manage references.
Only one `new MarkList()` statement is executed in the `main` method, which creates a single object in memory. The subsequent assignments like `obj2 = obj1` and `obj3 = null` only create new reference variables or assign an existing reference, but do not instantiate additional objects.
This is incorrect because `obj2 = obj1` makes `obj2` refer to the same object as `obj1`, it does not create a new object instance.
This is incorrect because `obj3 = null` assigns a null reference to `obj3` and does not create an object.
This is incorrect as only one object is instantiated with the `new` keyword, regardless of how many reference variables point to it or are set to null.
Concept tested: Java object instantiation and reference assignment
Source: https://docs.oracle.com/javase/tutorial/java/javaOO/objects.html
Topics
Community Discussion
No community discussion yet for this question.