aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes small mistake in ExamLab Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "small mistake in ExamLab" Watch "small mistake in ExamLab" New topic
Author

small mistake in ExamLab

Suranga Nath Kasthurirathne
Greenhorn

Joined: Aug 11, 2009
Posts: 13
and im just as stunned as you are... wonder whats happening... but im pretty sure that my exam certificate says that the cut off mark is 66% or so....
Tommi Vd
Ranch Hand

Joined: Apr 23, 2009
Posts: 49
how long is taht ago?

i'm making this examn from the examlab and i do not understand a question:

It's a question about serialization,

A class Forest has a Tree but it is declared static...
The Tree class does not implements Serializable.
Now they try to serialize a forest object, so i think this must succeed without exception that class Tree needs to implement Serializable?

Why? Because static vars aren't serialized right...

any help?
Suranga Nath Kasthurirathne
Greenhorn

Joined: Aug 11, 2009
Posts: 13
that was on the first of this month
Suranga Nath Kasthurirathne
Greenhorn

Joined: Aug 11, 2009
Posts: 13
static objects wont get serialized.. but that wont give an error.. only when you try to access the static object do you get a null pointer excaption..

but if it was not static and yet did not impliment serilizable then an exception is thrown because it sees that tree is not serializable...

hope that this answer is correct ...
Tommi Vd
Ranch Hand

Joined: Apr 23, 2009
Posts: 49
Well you say what i mean, but in the question of the exam it gives an exception because the class Tree is not serializable...
But the instnace of tree is declared static in the class from witch an instance gets serialized...
So actually there shouldn't be an error...?


you understand what i mean?





Here the answer is exception thrown because class Tree isn't serializable. But the instance of Three in Forest is static...
Tommi Vd
Ranch Hand

Joined: Apr 23, 2009
Posts: 49
Anyone?
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

You pass that static reference to writeObject method so JVM is going to serialize it - but the exception is thrown.

But Serialization won't be one exam. Bert wrote that.


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Thomas Van Driessche wrote:Anyone?


Please read this. You should be patient about answer to your questions ...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Tommi Vd
Ranch Hand

Joined: Apr 23, 2009
Posts: 49
oops sorry

not a reference of tree is passed to writeobject, a reference of f.
And since the instance var tree from f is static, there shouldn't even be a try to serialise that...
So there can not be thrown a runtime exception though?
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Well....passing the tree reference is ok. But letz see the JVM looks at the class and sees the class Forest is serializable and what's this this class has a relationship with the Tree class i.e Tree is in the Object graph of Forest. So following the all or nothing rule the JVM fails at runtime throwing the NOSerializtionError : Tree.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Thomas Van Driessche wrote:oops sorry


You broke another rule No Need To Say Sorry. But don't say sorry for breaking the rule or you'll broke that rule again

not a reference of tree is passed to writeobject, a reference of f.
And since the instance var tree from f is static, there shouldn't even be a try to serialise that...
So there can not be thrown a runtime exception though?


Your Forest class itself is not serializable. Its not about the Tree class. Your Forest class needs to be serializable for you to be able to serialize its objects...
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Actually he copied the question wrongly from examlab. Well here yes the FOrest is not serializable. Even if it implemented Serializable then also no instance of forest will be serializable. This question was discussed here Coderanch
Tommi Vd
Ranch Hand

Joined: Apr 23, 2009
Posts: 49
Static variables are implicitly transient - that's true, but it will not apply here, because we are serializing the object of the tree variable directly.

i do not really get this...
is it because the reference to the object is static? but the object itself isn't?
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

AGAIN: Serialization will NOT be on exam! And read my post above. Thanks!
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537



This is 44th question of Examlab. Well I think the explanation is a little bit off. See when mt2.start() is executed it will not run. Main will continue to run and will append sb1 and then it sleeps. So synchronization or not this code is fine. In the explanation it was said that the lock sb1 was held by a thread but due to non synchronized methods it can change it.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

The whole question is good, but tricky.


The only thing that will be printed is: BuilderPassed,

ExamLab's thread questions are the hardest ones
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

I got the answer right i understand the problem. I read the explanation and i thought the explanation was little off coz the m2.start() doesn't run untill the thread sleeps again. So what i was telling is that m2 won't run so it won't acquire the lock for sb1 that's all.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

How do you know that mt2 won't run? You can not predict it. Thread Scheduler can put MainThread into Runnable and let mt1 to go. The fact is Thread.sleep(...) helps with thread switching but without it the switching can perform.
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Well the other way round is also can be debated on....but here those things don't matter hehe.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: small mistake in ExamLab