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
arjun gaur
Posts: 20
Joined: Sun Feb 16, 2014 3:28 am
Contact:

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

Post by arjun gaur »

How come option 3 throws ClassCastException
Though String class and Object class are related

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

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

Post by admin »

I am not sure what is your current understanding of when CCE is thrown. Can you post it here so that I can address your doubt?

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

arjun gaur
Posts: 20
Joined: Sun Feb 16, 2014 3:28 am
Contact:

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

Post by arjun gaur »

Well to me,a CCE occurs when an Object fails an IS-A test with the class type into which it is being Casted(Book-Mala Gupta).
And every class extends Object class.

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

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

Post by admin »

So is that happening or not happening here?
If you like our products and services, please help us by posting your review here.

bogieman987
Posts: 1
Joined: Wed Nov 05, 2014 8:58 am
Contact:

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

Post by bogieman987 »

I need a bit of clarification on why casting and object to String throws ClassCastException.

But I'll state what I think first, then ask my questions.

Here's example code in the Mala Gupta book.

Code: Select all

import java.util.ArrayList;
public class ListAccess {
     public static void main(String[] args) {
          ArrayList<Ink> inks = new ArrayList<Ink>();
          inks.add(new ColorInk());
          inks.add(new BlackInk());
          Ink ink = (BlackInk) inks.get(0); // Throws ClassCastException
     }
}
class Ink{}
class ColorInk extends Ink{}
class BlackInk extends Ink{}
So line that throws the exceptions throws it because it is trying to cast a ColorInk object as a BlackInk object but BlackInk isn't a ColorInk. I assume the code would work if I were to try and cast to Ink? Since ColorInk is an Ink.

So in the case of the mock exam question, why does casting an Object to String fail, isn't String an object and thus implicitly extends Object?

What am I missing?

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

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

Post by admin »

You said casting ColorInk to Ink should work because ColorInk extends Ink.
If you apply the same logic in this question, why do you think casting Object to String should work? Dpes Object extend String?
If you like our products and services, please help us by posting your review here.

Sergey
Posts: 39
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

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

Post by Sergey »

The compiler says: java.lang.Object cannot be cast to java.lang.String
But why? I thought String extends Obects.

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

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

Post by admin »

Think of it this way - Dog extends Animal, What does that mean? Every Animal is a Dog? or Every Dog is an Animal?
If you like our products and services, please help us by posting your review here.

zoharch
Posts: 4
Joined: Fri Sep 29, 2017 3:34 pm
Contact:

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

Post by zoharch »

String extends Object therefore Object pointer can point to a String object.
String s ="test"
Object o = s;
Now you can do all methods of Object class for that string.
Every String is an Object.
Also methods with Object in their argument accepts String as a parameter.
The other direction is not like that. Not every object is a String.
Therefore cast exception raid for:
Object o = new Object();
String s = o // compile error.
String s = o.toString(); // ok!

flex567
Posts: 201
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

I don't understand the issue in the first code snippet?
To me the code looks fine, and it has the boundary condition .

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

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

Post by admin »

The first one uses recursion. It will cause the stack to overflow if you run it for a large integer. For example, try calling it - factorial(Integer.MAX_VALUE)
If you like our products and services, please help us by posting your review here.

roberto1
Posts: 1
Joined: Mon Jul 09, 2018 4:43 am
Contact:

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

Post by roberto1 »

The questions asks about exceptions, but the right answer has an error in it? Those are different branches of the Throwable tree. Or do I see this the wrong as a non-native English speaker?

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

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

Post by admin »

The word "exception" (with a lower case e) implies all sorts of exceptions i.e. all instances of Throwable. The word "Exception" (with an upper case E) implies only Exception and its subclasses (including RuntimeExceptions). This is common terminology and is used by the exam as well.

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

qazwsx922
Posts: 7
Joined: Sat Sep 04, 2021 8:17 pm
Contact:

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

Post by qazwsx922 »

I don't understand the third one. I thought it was "No exception will be thrown".
Why it caused ClassCastException ?

Also where can I find the "ExceptionClassSummary document in the "Study References" section."?

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

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

Post by admin »

The call to m1() returns an object of class java.lang.Object. Since Object does not satisfy the is-a relation with String (because an Object is not a String, although a String is an Object), if you try to cast it to String, the JVM will throw a ClassCastException.

It is like trying to force a Vehicle to be a Car. All Vehicles are not Cars even though all Cars are Vehicles.

You can read Exception class summary here.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: samba2 and 4 guests