About Question enthuware.ocajp.i.v8.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
corptrainer1
Posts: 5
Joined: Mon Oct 03, 2016 3:41 am
Contact:

About Question enthuware.ocajp.i.v8.2.1230

Post by corptrainer1 »

A course mentor has submitted the following:

The code does not compile.
Attachments
enthuware.ocajp.i.v8.2.1230_Screencast.png
enthuware.ocajp.i.v8.2.1230_Screencast.png (145.53 KiB) Viewed 4550 times

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

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

Post by admin »

It does compile fine. Please try the code exactly as given in the question and post the compilation error message that you get.

HTH,
Paul.
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.v8.2.1230

Post by Sergey »

What is the principle difference?
1)
Compiles fine and has no errors:

Code: Select all

//In file B.java 
import java.io.*;
class A{   
public static void main() throws IOException{ }
 }

You can have a main method that doesn't take String[] as an argument. It will not make the class executable from the command line though.
2)Compiles fine but throws an error:

Code: Select all

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

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

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

Post by admin »

What error message do you get?
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.v8.2.1230

Post by Sergey »

Compiles fine.
Error at runtime: Error: Could not find or load main class B
//In file B.java
import java.io.*;
class A{
public static void main() throws IOException{ }
}
Compiles fine.
Error message: 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)

public class TestClass {
public static long main(String[] args)
{
System.out.println("Hello");
return 10L; }
}
Hm... if i understand it right, the general idea is when we try to use slightly different main method (or if there is no mian method at all), the code could compile but often trows an error at runtme. Right? Or, maybe it is not comparable examples.

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

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

Post by admin »

If you want to run a class from command line then your class must have main method as:
public static void main(String[] args)

Otherwise, it will be like any other method in the class. The JVM will not be able to recognize that method as "the" main method.
If you like our products and services, please help us by posting your review here.

Kevin30
Posts: 28
Joined: Sun Oct 25, 2015 10:14 am
Contact:

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

Post by Kevin30 »

I spotted right away that this main-method had "long" and not "void" in its method signature, and therefore this method was not THE main method that can be run from the command line.

However, the question looks a bit ambiguous to me, and I answered "Hello".

Let me explain:
If you read the question as follows:
What will the following codeSNIPPET print when run?

Code: Select all

public class TestClass {
  public static long main(String[] args){
     System.out.println("Hello");
     return 10L;
  }
}
...then this code will print "Hello" because the full code would look like:

Code: Select all

//in file /root/com/Alpha.java
public class Alpha {
    public static void main(String[] args) {
        TestClass tc = new TestClass();
        String[] strAr = new String[2];
        tc.main(strAr);
    }
}

//in file /root/com/TestClass.java
class TestClass{
    public static long main(String[] args){
        System.out.println("Hello");		// Prints out: Hello
        return 10L;
    }
}
So, how you interpret the question depends on if you read the word 'code' or 'codesnippet'. I think on the java-exam they can also use the word 'code' even if they mean 'codesnippet', but I could be mistaken.

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

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

Post by admin »

Oracle does ask you to assume several things such as appropriate imports as well as any supporting code. Specifically, one of the assumptions mentioned on their page says, "A code fragment is a small section of source code that is presented without its context. Assume that all necessary supporting code exists and that the supporting environment fully supports the correct compilation and execution of the code shown and its omitted environment."

However, the code given in the question is not a code snippet. It shows complete code. So there is no need to assume anything, specially when the problem clearly seems to be about "compilation and execution". This assumption is really about code fragments without any method or class declarations.

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

Denyo1986
Posts: 38
Joined: Thu Jan 07, 2021 2:47 am
Contact:

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

Post by Denyo1986 »

I just wonder why that is not something the compiler complains about as it is easily foreseeable....

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

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

Post by admin »

What is forseeable?
If you like our products and services, please help us by posting your review here.

Denyo1986
Posts: 38
Joined: Thu Jan 07, 2021 2:47 am
Contact:

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

Post by Denyo1986 »

That there is a method that is very similar to the standard main method but doesnt fulfill all of its requirements. Would be nice to get a compiler warning highlighting the "long" in " public static long main(String[] args)"

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

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

Post by admin »

How can the compiler know that you wanted "the" main method and not a method named main which returns a long? How will the compiler know that this class was even supposed to be executed from the command line?
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 4 guests