About Question enthuware.ocajp.i.v7.2.1092 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

TheSarjo
Posts: 15
Joined: Fri Jul 12, 2013 12:34 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1092 :

Post by TheSarjo »

Problem arise when it's invoked the setIt method.
a.link points to the same object b points.
b.link points to the object returned by method setIt.
setIt takes two objects, (referenced by a and b) BUT sets a.link (x.link) to b.link(y.link). But b.link points to nothing!
So, a.link now also points to nothing. So nullpointerexception.
nice question!

thupten
Posts: 4
Joined: Thu Jan 09, 2014 3:21 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1092 :

Post by thupten »

@baxhurli The method setIt(a,b) does get executed.

Code: Select all

public static Holder setIt(final Holder x, final Holder y){
   x.link = y.link;  //makes a.link to null  so now a is an object with value=5, link = null
   return x;   //returns a. This is important because it is being assigned to b. So b.link.value gives a.value, i.e. 5 because of this return statement.
}

__Bill
Posts: 25
Joined: Thu Mar 27, 2014 11:35 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1092 :

Post by __Bill »

The java content of this question is simply "What happens if you use a null pointer." Null "reference" I mean.

It's sooo frustrating to miss such a simple question because it's buried under a half-mile of knots. The worst is working through nested loops with nonsense breaks and continues with pencil and paper to figure out what it will print.

I suppose they're trying to test something more than java knowledge, and I know this example is typical of the real tests, so it speaks well of the example. I feel almost like it's a twisted fraternity initiation.... Oh well, it's all in fun I guess. - Bill

vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1092 :

Post by vchhang »

Code: Select all

class Holder{
   int value = 1;
   Holder link;
   public Holder(int val){ this.value = val; }
   public static void main(String[] args){
	final Holder a = new Holder(5);
	Holder b = new Holder(10);
	a.link = b;
	b.link = setIt(a, b); //(1)
	System.out.println(a.link.value+" "+b.link.value);
   }
   
   public static Holder setIt(final Holder x, final Holder y){
       x.link = y.link; //(2)
       return x;  //(3)
   }
}
Would you explain why b.link is pointing to null?
1. At //(1) setIt() is called with 'a' and 'b'.
2. At //(2) x.link which is pointing the same location as a.link. a.link points to the Object reference by 'b'. The object reference by 'x' which is the same object referenced by 'a'.

Therefore,
3. At //(1) b.link is point to the object reference by 'a'.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1092 :

Post by admin »

When you create b = new Holder(); b.link is null. It is not being assigned a value anywhere. So it is null.

I have no idea what you are saying at 2. At //2, when you execute x.link = y.link; you assign y.link to x.link. y.link is null (because b.link is null as explained above), so x.link becomes null. x is nothing but a reference to the same object referred to by a. so a.link becomes null. Hence, NPE is thrown when you do a.link.value.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1092 :

Post by vchhang »

at //3 we are returning x which is the object referenced by a. When the method setIt returns, b.link is pointing a. Where am I going wrong?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1092 :

Post by admin »

b.link is null before the invocation of setIt. b.link is null even when you are inside the setIt method. That is why null is assigned to a.link.

b.link becomes non-null (pointing to a) AFTER setIt returns i.e. AFTER //3 (not at //3).

You might want to run it in a debugger and see the values step by step.
If you like our products and services, please help us by posting your review here.

Sergiy Romankov
Posts: 31
Joined: Thu Feb 19, 2015 8:25 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1092 :

Post by Sergiy Romankov »

Tell me please if I understand it right

When creates b = new Holder(), field b.link = null
than fetching to method setIt() reference b and y are both reference to
same object, which field link is null
By assigning y.link to x.link implicitly a.link became null
and when the program tries to get a field a.link.value of a.link we get NullPointerException

Thanks,

Sergey

NickWoodward
Posts: 29
Joined: Mon Mar 30, 2015 6:00 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1092 :

Post by NickWoodward »

I got the question right, so think I understand the main points, but....

I can't see why trying to access a.link.value would through an NPE. Yes it's null, but
String s = null; System.out.println(s); doesn't throw that exception.
I thought it might be applying the + operator, but it also occurs with System.out.println(a.link.value);

does that make sense?

thanks,

Nick

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1092 :

Post by admin »

NickWoodward wrote:I got the question right, so think I understand the main points, but....

I can't see why trying to access a.link.value would through an NPE. Yes it's null, but
String s = null; System.out.println(s); doesn't throw that exception.
I thought it might be applying the + operator, but it also occurs with System.out.println(a.link.value);

does that make sense?

thanks,

Nick
You need to understand that accessing a null reference always causes a NullPointerException to be generated. Always. No exceptions.

Basically, "access" means, the use of the dot operator. someReferenceDOTsomeMember. If you try a.link.value and if a is null or a.link is null, it will generate a NPE because you are trying to access a null.

Now, regarding why System.out.println(s); does not throw NPE, you have to ask yourself, are you accessing null here? The fact that it does not throw a NPE itself tells you that it is not being accessed. Then how does it print "null"? The only way that can happen is if the code inside the print method checks whether the reference is null before trying to access it. Something like this: if(ref == null) print("null") else print(ref.toString());

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

NickWoodward
Posts: 29
Joined: Mon Mar 30, 2015 6:00 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1092 :

Post by NickWoodward »

ah of course, i'm confusing access with passing the variable as an argument.

thanks, great answer!

roosdebie
Posts: 3
Joined: Wed Nov 16, 2016 10:16 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1092 :

Post by roosdebie »

Holder link;

This 'link' is declared, but ininitialized. So (if I am correct): If an Object reference has been declared but not instantiated, its value is null.
So this is why b.link; is 'null' (and a.link if it was not assigned to the Object reference 'b').

Took me a while...

bniky1
Posts: 4
Joined: Sun Jan 08, 2017 12:27 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1092 :

Post by bniky1 »

I have read the previous comments and still don't understand a.link = b. Does this mean a.link = b.link?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1092 :

Post by admin »

bniky1 wrote:I have read the previous comments and still don't understand a.link = b. Does this mean a.link = b.link?
No, a.link = b does not mean a.link = b.link. It means a.link points to the same object to which b is pointing.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1092 :

Post by crazymind »

"x.link = y.link, x.link becomes null because y.link is null so a.link.value throws NullPointerException."

Does it mean you do: null = null; will throw a NPE ?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1092 :

Post by admin »

No, null = null is an invalid statement. It will not even compile.
NullPointerException is thrown whenever you try to access an instance field or call an instance method using a reference that is pointing to null.
If you like our products and services, please help us by posting your review here.

Angelica
Posts: 2
Joined: Fri Sep 11, 2020 4:49 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1092 :

Post by Angelica »

admin wrote:
Wed Aug 01, 2012 11:02 pm
The Holder class is actually a very basic (and well known) building block of a linked list. It is usually named "Node" and not Holder. Please google linked list to learn more.

Though not important for this exam, understanding how linked lists work is something that a programmer must know. You mentioned that you've been programming for years and I am surprised to know that you didn't recognize it.

HTH,
Paul.
I think having a mental picture of a linked list actually helps to understand and follow the code in this question even for someone who has not had to work with linked lists. Thank you for mentioning it!

Leonid
Posts: 9
Joined: Mon Jun 28, 2021 4:50 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1092 :

Post by Leonid »

a.link = b does not mean a.link = b.link. It means a.link points to the same object to which b is pointing.
I imagined a and b - these are some country, link and value there are some lakes. a has own lakes and b has own lakes. How lakes of a can be assigned to another country but not to another lake? How is a.link=b works in reality? When I have saw it my brain was broken. How it happened in programming area? Please give me to understand this.

Post Reply

Who is online

Users browsing this forum: No registered users and 79 guests