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

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

Moderator: admin

Post Reply
ETS User

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

Post by ETS User »

interface XMen {
void shoot(String a);
}

public class WarZone {
public static void main(String[] args){
XMen x = null;
if(args.length() > 0){
x = new XMen(){
...

Confused why I got this wrong because you cannot instantiate an instance of an interface, am I wrong? I even tried this myself and got a compile error like I expected.

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

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

Post by admin »

Did you try the exact same code as given in the question?

You are right that an interface cannot be instantiated. But we are not instantiating an interface here. We are instantiating an anonymous class that implements the interface.

If you are interested to learn more, try this: after compiling the program, check how many .class files are created. One of the files will have a $ in its name. Run javap on that class. That will illustrate what is happening in this question.

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

el_bar
Posts: 1
Joined: Sat Jun 15, 2013 4:29 pm
Contact:

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

Post by el_bar »

ok .. what is result if its args.length

DesRenthuware
Posts: 32
Joined: Wed Aug 28, 2013 6:12 am
Contact:

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

Post by DesRenthuware »

... just my ten cents. Also got this wrong on the test, but then I had not prepared by studying inner classes (not in exam objectives - so prep only done using pertinent sections of K&B6 and the Mala Gupta book). My real question then is - is Oracle likely to do something like this on the real test?

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

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

Post by admin »

No, We recently updated the problem statement to include a note on the top:
"Note: This question may be considered too advanced for this exam."

Please make sure you are using the latest version of the question bank.

There are a few questions that touch upon these topics and we have included these just to make sure the reader has complete grasp of the subject. But if you are short on time, you may ignore these. (All of them have the same note at the top so that it is easy to identify.)

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

DesRenthuware
Posts: 32
Joined: Wed Aug 28, 2013 6:12 am
Contact:

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

Post by DesRenthuware »

Thanks for the feedback.

TheSarjo
Posts: 15
Joined: Fri Jul 12, 2013 12:34 pm
Contact:

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

Post by TheSarjo »

sorry, i don't know if i have a problem with my version of java (i'm on mac, and it's still java 6 running on it).

i tried to run javap on the class file named WarZone$1, but... nothing lights my mind. And there is no difference if i run it without $1...

here is what it gives me:

Code: Select all

javap WarZone$1
Compiled from "WarZone.java"
public class WarZone extends java.lang.Object{
    public WarZone();
    public static void main(java.lang.String[]);
}



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

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

Post by admin »

When you compile the given code, what all .class files do you see generated?
-Paul.
If you like our products and services, please help us by posting your review here.

TheSarjo
Posts: 15
Joined: Fri Jul 12, 2013 12:34 pm
Contact:

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

Post by TheSarjo »

When i compile WarZone.java i get 3 classes:
WarZone.class
WarZone$1.class
XMen.class

Here is compilation commands
iMac-di-Alex:ciao Sarjo$ javac WarZone.java
iMac-di-Alex:ciao Sarjo$ javap WarZone$1
Compiled from "WarZone.java"
public class WarZone extends java.lang.Object{
public WarZone();
public static void main(java.lang.String[]);
}

iMac-di-Alex:ciao Sarjo$ javap WarZone
Compiled from "WarZone.java"
public class WarZone extends java.lang.Object{
public WarZone();
public static void main(java.lang.String[]);
}

iMac-di-Alex:ciao Sarjo$ javap XMen
Compiled from "WarZone.java"
interface XMen{
public abstract void shoot(java.lang.String);
}

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

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

Post by admin »

Not sure why your javap is not showing the details of the anonymous class. When I do javap on WarZone$1, I see this:
final class WarZone$1 extends java.lang.Object implements XMen{
WarZone$1();
public void shoot(java.lang.String);
}
I don't have access to mac.

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

TheSarjo
Posts: 15
Joined: Fri Jul 12, 2013 12:34 pm
Contact:

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

Post by TheSarjo »

thank you, now it's more clear

fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

Post by fasty23 »

if we use args.length() compiler assumed 'self" is an string and if we use args.length compiler assumed "self" is an array?
sorry for this lame question, somebody explain this question in this way but i'm not sure it is correct.
Last edited by fasty23 on Wed Mar 12, 2014 9:30 pm, edited 1 time in total.

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

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

Post by admin »

Compiler doesn't assume anything. i.e whether args is a String or array. Compiler knows what args is. Based on what args is, it will either allow args.length() or allow args.length.
Let me ask it to you in another way. How do you call a method on an object and how do you access a variable of an object?
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 46 guests