Compilation error

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

Moderator: admin

Post Reply
Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

Compilation error

Post by Meghana »

What will be printed when the following code is compiled and run?

class A
public int getCode(){ return 2;}
}

class AA extends A {
public long getCode(){ return 3;}
}

public class TestClass {

public static void main(String[] args) throws Exception {
A a = new A();
A aa = new AA();
System.out.println(a.getCode()+" "+aa.getCode());
}

public int getCode() {
return 1;
}
}
The code doesn't compile because the methods, "getCodes()" are not defined in a static context and we are trying to access them from a static method is it?

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

Re: Compilation error

Post by Meghana »

Also, when is the ClassNotFoundException thrown?
(When I tried to run the above code, this exception was thrown)

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

Re: Compilation error

Post by admin »

In your first post you are saying the code doesn't compile but in your second message you are saying you got CNFE when you ran the code? So I am not sure what exactly are you doing. Did the code compile or not? What is the error message displayed by the compiler if it did not compile?

Please post the exact code that you are trying to compile.
If you like our products and services, please help us by posting your review here.

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

Re: Compilation error

Post by Meghana »

admin wrote:In your first post you are saying the code doesn't compile but in your second message you are saying you got CNFE when you ran the code? So I am not sure what exactly are you doing. Did the code compile or not? What is the error message displayed by the compiler if it did not compile?

Please post the exact code that you are trying to compile.
1. The correct answer to the question was: It does not compile. I was not sure as to why it did not compile.
2. Also, later when I tried to run the code as it is, I got the ClassNotFoundException. I wanted to know what I was doing wrong. (I guess its a basic question but I haven't practically worked on java much)

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

Re: Compilation error

Post by Meghana »

This is the question with the code:

Quote:
What will be printed when the following code is compiled and run?

class A
public int getCode(){ return 2;}
}

class AA extends A {
public long getCode(){ return 3;}
}

public class TestClass {

public static void main(String[] args) throws Exception {
A a = new A();
A aa = new AA();
System.out.println(a.getCode()+" "+aa.getCode());
}

public int getCode() {
return 1;
}
}

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

Re: Compilation error

Post by Meghana »

To be precise,
1. I don't know as to why the right answer is: It does not compile.
2. When is a CNFE thrown?

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

Re: Compilation error

Post by admin »

It seems like you are using an IDE to run the code because it is not possible to run a java file directly from command line. You have to compile it first. IDEs sometimes hide the compilation step and that is why it is not recommended to use Ides for exam preparation.

So try to compile the given code from command like and read the error message carefully.

CNFE is thrown by the jvm when you try to load a class but the jvm is not able to find it.
If you like our products and services, please help us by posting your review here.

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

Re: Compilation error

Post by Meghana »

I'll try doing that from now. Using Eclipse for now.
Thank you so much! :)

Post Reply

Who is online

Users browsing this forum: No registered users and 30 guests