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

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

Moderator: admin

Post Reply
Matheus

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

Post by Matheus »

This question says that, when run, it'll throw an Throwable, but there is no main method to allow this class to run.

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

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

Post by admin »

Do you mean there is no valid main method? Then yes, that is why it will throw an exception when run. This is also explained in detail in the given explanation.

Or do you mean you don't see any method in the given code? Because there is a main method defined in the given code:

Code: Select all

public class TestClass{   
  public static long main(String[] args){
      System.out.println("Hello");
      return 10L;
  }
}
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Matheus

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

Post by Matheus »

I just don't get why it throw something. I my mind it can't even been executed

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

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

Post by admin »

You are right. It can't be run but when the question says "...when run", it means the question expects you to do: c:/>java TestClass

This is a very common pattern for problem statements even in the exam.

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

Matheus

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

Post by Matheus »

Ok, thanks

Guest

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

Post by Guest »

Good day. I paste this code in IDE IntelliJ IDEA. And result was "Hello".

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

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

Post by admin »

Guest wrote:Good day. I paste this code in IDE IntelliJ IDEA. And result was "Hello".
For the purpose of the exam, you should not worry about any IDE but about what the specification says.

HTH,
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.1230 :

Post by TheSarjo »

About this question, i wanted to ask:

public int main(String [] args)

is a valid declaration of a method named main.

BUT

public static int main(String [] args)

is an invalid declaration of THE main method.
Why?

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

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

Post by admin »

When you say "the" main method, it implies that you are talking about the main method that is used by the JVM to "run" the class from command line.

If you try to "run" a Java class from command line, the JVM expects a public static main method that returns void and takes String[] as the only parameter.

You can have other main methods but the JVM will not accept them for the purpose of running the class from the command line.

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

ewebxml
Posts: 78
Joined: Sun Jun 30, 2013 10:04 pm
Contact:

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

Post by ewebxml »

I selected option e) None of the above
because when I ran the program I received

C:\Java_unique>java TestClass
Error: Main method must return a value of type void in class TestClass, please
define the main method as:
public static void main(String[] args)

-----
On the other hand, Error is subclass of Throwable.
I am assuming that the test question wants you to think along these lines.
Error is a Throwable.

javaman
Posts: 33
Joined: Wed Nov 13, 2013 4:11 pm
Contact:

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

Post by javaman »

Hello,
When I try to run this code from NetBeans, I get an alert saying the main() method is missing (as other reported). So I get no compiletime error or runtime error.
At the exam I'd have to guess between 'c' (does not compile) and 'd' (throws error at runtime). But since the correct answer is 'd', do I understand correctly that a class that is missing the main() method compiles correctly but throws an error at runtime?
Thanks

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

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

Post by admin »

You need to rely on command line and not on IDEs.

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

javaman
Posts: 33
Joined: Wed Nov 13, 2013 4:11 pm
Contact:

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

Post by javaman »

Ok, just for future reference (and for others who might have the same question): if I have a LearnJava.java class, containing 1 public class that contains a method "public static void main1(String args[])" (note: this is not a valid main method) I can sompile it succesfully with "javac LearnJava.java". But running the file gives me
"Error: Main method not found in class LearnJava, please define the main method a
s:
public static void main(String[] args)"
So yes, the compiler allows you to not have a main method and the error will be thrown by the JVM.
Thanks,
Marc

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

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

Post by Venceslas »

Ok understood.

dghant1024
Posts: 1
Joined: Sun Mar 22, 2015 8:18 pm
Contact:

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

Post by dghant1024 »

I would have understood this question better if i were stated more explicitly see below:

"What will the following code print [when ran from command line] ? "

instead of

" What will the following code print when run?"

public class TestClass{   
public static long main(String[] args){
     System.out.println("Hello");      
return 10L;   }
}

Given the signature of the method above, I assumed that this method was NOT going to executed from command line.

Your insight(s) are welcome.

Thanks.

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

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

Post by admin »

Your point is valid but we have followed the convention used in the exam. By "run" or "execute", it implies running from command line.

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

danko82
Posts: 5
Joined: Fri Aug 28, 2015 7:24 am
Contact:

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

Post by danko82 »

I just tried and:

Error: Could not find or load main class TestClass

from cmd windows 7

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

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

Post by admin »

And that is also what the correct option says. It will throw an Error at runtime.
If you like our products and services, please help us by posting your review here.

MarekM
Posts: 1
Joined: Tue Aug 02, 2016 8:55 am
Contact:

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

Post by MarekM »

Hey. Could someone write how the exam is to formulate as regards about calling code from another method? I understand now that the "run" or "execute" means starting from the command line. What if there is a similar question which means calling this code from another method? How do they usually express it? Something like that?:

"What will be the output of the following code snippet?"

"Consider the following method: (...) What will it print?"

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

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

Post by admin »

"What will be the output of the following code snippet?"

"Consider the following method: (...) What will it print?"
The exam uses both the styles. In both the cases, it means that the given piece of code or method is executed somehow by the JVM and you are required to determine the result as per the problem statement. Usually it is about the output of print statements on the command line or state of some variable.

HTH,
Paul.
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 52 guests