About Question enthuware.ocajp.i.v7.2.1372

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

Moderator: admin

Post Reply
apisani
Posts: 2
Joined: Wed Dec 31, 2014 8:01 am
Contact:

About Question enthuware.ocajp.i.v7.2.1372

Post by apisani »

Assume that it is called with a very large integer.

void printMe(Object[] oa){
       for(int i=0; i<=oa.length; i++)
       System.out.println(oa);    
 }


I think the right raised exception is ArrayIndexOutOfBoundException
but the question shows that NullPointerException is the right one. I think this is wrong. ??!!!

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

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

Post by admin »

The comment, "Assume that it is called with a very large integer. " is for the previous code snippet. It is written above the number 2.

For the code that you've pasted, the comment is, "Assume that it is called as such: printMe(null);".
If you like our products and services, please help us by posting your review here.

apisani
Posts: 2
Joined: Wed Dec 31, 2014 8:01 am
Contact:

[RESOLVED] Re: About Question enthuware.ocajp.i.v7.2.1372

Post by apisani »

You are totally right.

Im sorry for the confusion.

mutley
Posts: 6
Joined: Mon Feb 23, 2015 9:37 am
Contact:

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

Post by mutley »

I don't really understand why should this piece of code throw ClassCastException :

Object m1(){       
 return new Object();     
}     
void m2(){        
String s = (String) m1();     
}

I mean , should it be an object obj of the class XXX that wrapps these methods , a call to obj.m2() , would throw a ClassCastEXxception , but literally this piece of code doesn't throw any exception.
IMHO you should mark
StackOverflowError
NullPointerException
No Exception Will Be Thrown

as the correct option

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

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

Post by admin »

Hello,
I am sorry but I'm not sure I understand your question. The given code does throw ClassCastException. You may want to try it out.

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

mutley
Posts: 6
Joined: Mon Feb 23, 2015 9:37 am
Contact:

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

Post by mutley »

admin wrote:Hello,
I am sorry but I'm not sure I understand your question. The given code does throw ClassCastException. You may want to try it out.

HTH,
Paul.
Ok maybe I didn't express myself well enough
Class Test
{
Object m1(){
return new Object();
}
void m2(){
String s = (String) m1();
}
public static void main(String[] args)
{
Test obj = new Test();
obj.m2();// only at this point does the ClassCastException occur
}
}

The way is presented ( without the call obj.m2() ) does not throw any exception ,hence my doubt ...

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

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

Post by admin »

Ok, I see your point. These are just code snippets and not complete executable classes. Since the question explicitly says, "...when the following code snippets are executed", you have to assume that the given code is somehow executed. This format is used in the question in real exam as well.

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

ElizabethCM
Posts: 29
Joined: Sun Apr 05, 2015 11:26 am
Contact:

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

Post by ElizabethCM »

Hi Paul,

In the explanation section it says:
Please read ExceptionClassSummary document in the "Study References" section.
Can you please tell me where can I find the Study References section?
Is it in the book - Appendix B- Study Tips? Or on the EnthuWare website?

Thanks

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

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

Post by admin »

It is in ETS Viewer itself. In the tree view, click on the "Study View" tab. There you will see Study References node. Expand that.

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

EfimGraur
Posts: 4
Joined: Fri Oct 16, 2015 1:25 pm
Contact:

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

Post by EfimGraur »

Hello Paul,

I am a bit confused with point 3:

Object m1(){     
   return new Object();  
   }     
void m2(){  
      String s = (String) m1();  
   }


I know that ClassCastException is thrown when an object fails an IS-A test with the class type to
which it’s being cast. But all Java Classes inherit from Object class. I guest this means that String and Object have a IS-A relationship.

but when I try to do a instanceof check I receive false.

public class Test78
{
Object m1(){
return new Object();
}
void m2(){
String s = (String) m1();
}
public static void main(String[] args)
{
Test78 obj = new Test78();
System.out.println(obj.m1() instanceof String);
}
}

Many Thanks,
Regards,
Efim

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

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

Post by admin »

You have not understood the meaning of IS-A test. Yes, String and Object do have a IS-A relationship, but what you need to ask yourself is which one of the two is-a the other one. Think about it like this. Animal and Dog have an IS-A relationship, but is every Animal a Dog? or is every Dog an Animal? This is what is really important.

Here, Object is not a String, so you cannot cast an Object object to a String. But every String object is an Object, therefore you can cast any String object to Object.

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

EfimGraur
Posts: 4
Joined: Fri Oct 16, 2015 1:25 pm
Contact:

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

Post by EfimGraur »

Hello Paul,

Now I understand,

Many Thanks,
Efim

jamesmccreary
Posts: 22
Joined: Sun Jan 15, 2017 10:51 pm
Contact:

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

Post by jamesmccreary »

Referencing the "Casting Objects" section from the JLS: https://docs.oracle.com/javase/tutorial ... asses.html

We have:

Code: Select all

Object obj = new String();
String myString = obj; // will not compile
String myString = (String) obj; // will compile
So in this question's case, casting from Object to String is not allowed because we are casting the object itself (from the return of m1() ) rather than a reference that actually points to the type being casted to (i.e. String).

Is that correct? You can't cast objects "downward" (but you can "upward") and that casting downward only compiles if
1) you are casting a reference, and
2) the reference is pointing to the object of the same type to which you are casting

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

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

Post by admin »

Your question indicates that you have not understood several fundamental concepts about objects, references, variables, and types. I will try to answer as best as I can in a post but I will suggest you to go through a good book to first get your fundamentals right and then attempt the questions.
jamesmccreary wrote: So in this question's case, casting from Object to String is not allowed because we are casting the object itself (from the return of m1() ) rather than a reference that actually points to the type being casted to (i.e. String).
There are no two things like "casting an object" and "casting a reference". There is only one thing, "casting". You cast a variable of one type to another type. You don't cast objects. Objects don't change their types at all. This is a two step process - first, the compiler checks whether it is possible for a variable of a type to refer to an object of the required target type. (This is a complicated statement, so you might want to read it multiple time until you learn by heart). If it finds out that it is not possible, then the compiler raises a compilation error.

Second, the run time i.e. the JVM checks whether the actual object that is being referred to by the variable is actually of the target type. If it is not, then it will cause a ClassCastException to be thrown.

These checks are nothing but the test of is-a relationship. An object of a type is always also of its super type. For example, an object of type String is always of type Object. Thus, you can always cast a String variable to Object. ie. String str = ""; Object obj = (String) str; is always valid and is also redundant.

The reverse i.e. Object obj =""; String str = (String) obj; has to go through a two step checking process.
1. Is it possible for obj to refer to an object of type String. Yes, it is. So the compiler is happy.
2. Is obj actually referring to an object of type String. Yes, it is. So the JVM is happy.

An unrelated cast such as Integer i = 10; String s = (String) i; will fail at compile time because it is not at all possible for i to point to an object of type String.
Is that correct? You can't cast objects "downward" (but you can "upward") and that casting downward only compiles if
1) you are casting a reference, and
2) the reference is pointing to the object of the same type to which you are casting
The words downward and upward tend to get confusing. The right terminology is "more specific" and "less specific" - you can always assign and cast a variable of one type to a type that is less specific (e.g. String to Object). The reverse i.e. cast a variable of one type to a type that is more specific (e.g. Object to String) is possible only if it passes the two checks mentioned above.

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

jamesmccreary
Posts: 22
Joined: Sun Jan 15, 2017 10:51 pm
Contact:

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

Post by jamesmccreary »

Paul, thank you so much for your patient and thoughtful response. I now fully understand casting; I had never come across that two-step process before in my study preparation. I will add this to my notes and this will be extremely helpful for anyone else as well who is confused about the casting mechanism.

Whenever I contribute on this forum to Casting questions, I can now refer to your response (and give you due credit!).

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 55 guests