1Z0-803 · Question #226
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. Only one MarkList object is explicitly instantiated using the new MarkList() keyword in the main method. All subsequent assignments and method calls manipulate references to this single object or its fields, without creating any additional objects.
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 obj1 = null; obj2.num = 60; graceMarks(obj2); } } How many objects are created in the memory runtime?
Options
- A1
- B2
- C3
- D4
How the community answered
(21 responses)- A81% (17)
- B10% (2)
- C5% (1)
- D5% (1)
Why each option
Only one `MarkList` object is explicitly instantiated using the `new MarkList()` keyword in the `main` method. All subsequent assignments and method calls manipulate references to this single object or its fields, without creating any additional objects.
Only one `MarkList` object is created in memory because the `new` keyword, which allocates memory for a new object, is called only once at the line `MarkList obj1 = new MarkList();`.
This is incorrect because the `new` keyword, which is responsible for object instantiation, appears only once in the provided code snippet.
This is incorrect because the `new` keyword, which is responsible for object instantiation, appears only once in the provided code snippet.
This is incorrect because the `new` keyword, which is responsible for object instantiation, appears only once in the provided code snippet.
Concept tested: Java object creation and reference management
Source: https://docs.oracle.com/javase/tutorial/java/javaOO/objects.html
Topics
Community Discussion
No community discussion yet for this question.