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

dtchky
Posts: 19
Joined: Wed Aug 01, 2012 3:11 am
Contact:

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

Post by dtchky »

This question is categorized as easy, though having read many Java books and programmed in a lot of languages for a lot of years, it might be that I'm simply reading the wrong books :-) though this question seems more like a fairly dusty corner of the language rather than easy. Wondering if you could expand a little on a.link = b; I had no idea you could do this and don't quite understand how it works. I can see what the result is by picking a.link apart, but what mechanism makes the assignment work?

Code: Select all

   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;
Thanks.

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

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

Post by admin »

link is a field in class Holder. a is a variable of class Holder. So a.link refers to the member field link of the Holder object pointed to by a.
b is another variable of class Holder. It also points to an object of class Holder.

Therefore, in a.link = b; you are assigning the object pointed to by b to a.link.

I hope that explains it.

Regarding toughness, you are right. It doesn't look like an easy question :)

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

dtchky
Posts: 19
Joined: Wed Aug 01, 2012 3:11 am
Contact:

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

Post by dtchky »

Definitely clears things up now. Conceptually I had been thinking of it as chaining variables of class types together. Quite a difficult one to google. : )

Thanks greatly, appreciate the explanation.

Brendan.

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

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

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

dtchky
Posts: 19
Joined: Wed Aug 01, 2012 3:11 am
Contact:

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

Post by dtchky »

I would humbly disagree with you good sir, purely because I think it much wiser to avoid presumption that any construct translates cleanly from one programming language to another. Clean slate. Starting fresh. I'm familiar with linked lists and how they work in other languages, programming 101, sort algorithms, fodder for bog standard interview questions, however at this point in time, this particular question doesn't jump out at me as being related to linked lists other than the word 'list', which is just a variable. As you say, "not important for this exam", thus quite a critical statement when OCAJP is the end goal. Intricate knowledge of the collections framework is a couple of items away on my check list.

Thanks again, appreciate the reply.

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

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

Post by admin »

You are right, it is not important for this exam and is not really relevant for this question either. There is no presumption of linked lists required for answering it. I was just surprised. That's all. The question will be improved to avoid such confusion.

thank you for your valuable fedback!
If you like our products and services, please help us by posting your review here.

lordnovas
Posts: 12
Joined: Tue Jan 15, 2013 3:31 pm
Contact:

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

Post by lordnovas »

admin wrote:link is a field in class Holder. a is a variable of class Holder. So a.link refers to the member field link of the Holder object pointed to by a.
b is another variable of class Holder. It also points to an object of class Holder.

Therefore, in a.link = b; you are assigning the object pointed to by b to a.link.

I hope that explains it.

Regarding toughness, you are right. It doesn't look like an easy question :)

HTH,
Paul.

Hey Paul, I was a bit confused with this question as well. What I don't get is how 'a' became null in the first place. When I code the question and check to see if 'a' is null with print statements, it appears that 'a' becomes null when it references .link (a.link). Is this because the variable "link" is declared as a reference to the Holder object, but is not assigned to a Holder Object? So like is the value of "link" null, which is why 'a' is now null? or is 'b' null and when "a.link" is assigned the value of 'b' it in turn becomes null?

Sorry if my question is confusing, please help if you can.

cheers,
j-

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

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

Post by admin »

'a' is not null. Where does it say that a becomes null?
a.link is null and that is why a.link.value throws NullPointerException.

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

The_Nick

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

Post by The_Nick »

Amazing question, it's also worth to play a bit with this class to understand how an member object variable works within another object. Also the final keyword very well explained.
However I agree with who above stated that the degree of difficulty does not reflect the reality. I would say at least tough.

baxhuli
Posts: 5
Joined: Wed Mar 27, 2013 11:57 am
Contact:

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

Post by baxhuli »

That's how I understood it.
We assign a value to a.link (a.link=b) whereas b.link is null because we do not assign nothing to it.
When the program tries to do "b.link.value" as b.link has not been initialized we get a error at runtime.
The method "setInt" does not even get executed. I think there is a mistake in the explanation. However this question should at least be tough.
Thank you.

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: 10043
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: 10043
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: 10043
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: 10043
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 ?

Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests