Multiple choice technology programming languages

What will be the value of Point p after methods in a and b if the value before method call is (700,800). static void changePoint ( Point p) { p.x = 100; p.y=200; } static void changePoint(Point p) { p=new Point(100,200); }

  1. a(100, 200), b(100, 200)

  2. a(100, 200), b(700, 800)

  3. a(700, 800), b(100, 200)

  4. a(700, 800), b(700, 800)

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

In Java, objects are passed by reference. Method a modifies the actual object's fields, changing x to 100 and y to 200. Method b reassigns the local reference variable to a new Point object, but this doesn't affect the original object. So after method a: (100, 200), after method b: (700, 800) unchanged.

AI explanation

In Java, objects are passed by value of the reference. In the first method, p.x and p.y are set directly on the object the caller's reference points to, so the caller sees the change: (100, 200). In the second method, p = new Point(...) only reassigns the local copy of the reference to a brand-new object; the caller's original reference still points to the original object, which is untouched, so it remains (700, 800).