About Question enthuware.ocpjp.v7.2.1089 :

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

Moderator: admin

Post Reply
Venceslas
Posts: 15
Joined: Thu Feb 05, 2015 3:50 pm
Contact:

About Question enthuware.ocpjp.v7.2.1089 :

Post by Venceslas »

Hello,

The question is a bit unfair. At first read I thought the code does not compile. However the question asks if the code returns true or false, it means we must assume the code compile.
All in all, I think the option "code does not compile should exist".

Chris

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

Re: About Question enthuware.ocpjp.v7.2.1089 :

Post by admin »

I agree that it might seem a bit unfair but not totally incorrect. It is a fact that the expression doesn't return false (for whatever reason).
-Paul.
If you like our products and services, please help us by posting your review here.

Venceslas
Posts: 15
Joined: Thu Feb 05, 2015 3:50 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1089 :

Post by Venceslas »

Humm, I'm still struggle to understand the logic:
"It is a fact that the expression doesn't return false" => Still the answer is "return false"

Chris

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

Re: About Question enthuware.ocpjp.v7.2.1089 :

Post by admin »

The problem statement says, "Expression (s instanceof java.util.Date) will return false if s was declared as a variable of class String" and asks you whether this statement is true or false.

So does the given expression return false? No. That means the above statement is false.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1089 :

Post by jagoneye »

This was a killer question. Remember you also need to think like the compiler sometimes!

Mark7777
Posts: 32
Joined: Tue Apr 12, 2016 9:19 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1089 :

Post by Mark7777 »

Okay, I don't see how an instance of Object can be an instanceof Date. I can see why it compiles because of being in the same hierarchy, but an Object instance is not in an IS-A relationship with Date. You can't assign supertype Object to a reference of Date, its subtype. On the other hand, Date IS-A Object and I see how Date instanceof Object would work at runtime.

In other words, if class B extends A, a B object is an instanceof A (will compile and run), but an A (though in the same hierarchy) can't be an instanceof B because A is a superclass of B. What am I missing here? To pass the instanceof test is it enough for both to merely be in the same hierarchy? Is the IS-A analysis relevant? hmmm...

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

Re: About Question enthuware.ocpjp.v7.2.1089 :

Post by admin »

All of your argument is correct. But how does it contradict the answer given for this question?
If you like our products and services, please help us by posting your review here.

Mark7777
Posts: 32
Joined: Tue Apr 12, 2016 9:19 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1089 :

Post by Mark7777 »

I got the answer correct and see no contradiction. I just didn't understand fully why. It was obvious that it wouldn't compile and therefore couldn't return true or false. What threw me was your explanation that "Had 's' been declared as a variable of type Object, this code would have compiled because compiler sees that at run time it is possible for s to refer to an object of class Date." I don't see how an instance of Object can be an instance of its subtype Date. Evidently instanceof is simply checking whether they are in the same hierarchy?

Perhaps I misunderstood your explanation. An instance of Object is an instance of any other type because all other types are (IS-a) ultimately of type Object? That makes sense. Am I close?

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

Re: About Question enthuware.ocpjp.v7.2.1089 :

Post by admin »

You are not reading the explanation correctly.
I don't see how an instance of Object can be an instance of its subtype Date.
The explanation doesnt say this at all. It says,"... it is possible for s to refer to an object of class Date"

Something like this:
Object s = new Object();
//somewhere else in code
s = new Date();// this is valid because a Date is an Object.

This is the standard is-a rule in action.

Remember that s is not an object. It is a reference variable of type Object. It is allowed to point to any object that satisfies IS-A test with Object.
If you like our products and services, please help us by posting your review here.

Mark7777
Posts: 32
Joined: Tue Apr 12, 2016 9:19 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1089 :

Post by Mark7777 »

I understand. Was reading too much into your explanation. I was reading apples; you were speaking oranges. I thought you were claiming that the following would return true, which it doesn't, although it compiles:

Object obj = new Object();
boolean bl = obj instanceof java.util.Date;

Were you referring to reassigning an instance of Object to the reference variable (here obj), as per the following? When I run it it returns false. Compiles fine, though. I don't want to waste any more of your time on this.

import java.util.*;
class TestInstance {
public static void main(String[] args) {
boolean bl;
Object obj = new Date();
bl = obj instanceof Date;
System.out.println(bl); // true
obj = new Object();// reassign to variable.
bl = obj instanceof Date; // compiles fine because in same hierarchy?
System.out.println(bl); // false
}
}

I think what threw me off was the phrase "...compiler sees that at run time it is possible for s to refer to an object of class Date." The "it is possible" part confused me. And I think your explanation was limited to compile time. Thanks.

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

Re: About Question enthuware.ocpjp.v7.2.1089 :

Post by admin »

The compiler only checks whether something is possible. It is possible for s to point to an object of class Date. It cannot check whether s really points to an object of any particular class because compiler's job is only to compile. It cannot run the code. The JVM runs the code at run time, and while running the code, the JVM checks whether s really points to the type of the object that the code claims. If it does, the JVM is happy otherwise, the JVM throws a ClassCastException. So, you can see that it is a two step process - one check is done by the compile to see if something is even possible and the second check is done by the JVM to see if whatever is claimed by the developer really holds true.

So, from the above perspective:

Object obj = new Date(); //valid at compile time
bl = obj instanceof Date; //valid at compile time and returns true because obj does point to a Date object

obj = new Object(); //valid at compile time
bl = obj instanceof Date; // valid at compile time because Date is a Object and it is possible for obj to point to a Date object
//compiles fine because in same hierarchy? Yes.
//returns false because obj does not really point to a Date object
Date d = (Date)obj; //valid at compile time but JVM throws ClassCastException because obj does not point to a Date object

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

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

Re: About Question enthuware.ocpjp.v7.2.1089 :

Post by admin »

As an exercise try to determine what will happen at compile time and run time for the following code:

Date d = new Object();
boolean b = d instanceof Date;

d = "string";
b = d instanceof Date;

d = new Date();
b = d instanceof String ;

Object o = "1234";
b = o instanceof Date;
If you like our products and services, please help us by posting your review here.

Mark7777
Posts: 32
Joined: Tue Apr 12, 2016 9:19 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1089 :

Post by Mark7777 »

Date d = new Object(); // won't compile
boolean b = d instanceof Date;

d = "string"; // won't compile. String can't be converted to Date
b = d instanceof Date;

d = new Date(); // compiles
b = d instanceof String ; // won't compile. Date cannot be converted to String

Object o = "1234"; // compiles
b = o instanceof Date; // // compiles. o possibly could refer to ANY object down the road, even an instance of Date.
System.out.println(b); // returns false. Object is in same hierarchy as any object, so can use the instanceof test, but fails the IS-A test

Very helpful. Thank you.

Post Reply

Who is online

Users browsing this forum: No registered users and 37 guests