About Question enthuware.ocajp.i.v8.2.1230

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 » Thu Oct 06, 2016 10:36 pm

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 2500 times

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

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

Post by admin » Thu Oct 06, 2016 11:41 pm

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: 35
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

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

Post by Sergey » Fri Nov 17, 2017 6:05 pm

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;   
  } 
}

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

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

Post by admin » Fri Nov 17, 2017 9:57 pm

What error message do you get?
If you like our products and services, please help us by posting your review here.

Sergey
Posts: 35
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

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

Post by Sergey » Sat Nov 18, 2017 10:52 am

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.

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

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

Post by admin » Sun Nov 19, 2017 12:14 am

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 » Tue Sep 11, 2018 7:33 am

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.

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

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

Post by admin » Tue Sep 11, 2018 11:02 am

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.

Post Reply

Who is online

Users browsing this forum: admin and 8 guests