nerdexam
Oracle

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.

Java Basics

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)
  • A
    81% (26)
  • B
    3% (1)
  • C
    6% (2)
  • D
    9% (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.

A1Correct

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.

B2

This is incorrect because `obj2 = obj1` makes `obj2` refer to the same object as `obj1`, it does not create a new object instance.

C3

This is incorrect because `obj3 = null` assigns a null reference to `obj3` and does not create an object.

D4

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

#object creation#reference variables#pass by reference#memory management

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice