• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ExamLab - Incorrect explanations?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I just did one of Devaka exams and it seems to me that some explanations are not 100% correct. Do you mind to comment:

1.


Answer is that code will not compile because:

Both symbols ... and [] cannot be used together.



While I agree that in order to execute class should exist main(String[] args) method, but it totally fine to have methods with (String... argv[]) . It means that both symbols can be used together.

2.


Answer is that compilation fails because line 19 cause compilation error.
To my opinion even prior line 19(on line 18) we have error, because exception is not handled.

Any comments?

Thank you.
 
Sheriff
Posts: 7136
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Michael,

I feel my explanation is correct with that first question. It makes sense if you try yourself to compile a class which is having a method declared like main(String... argv[]).

For the second one, your observation is good, but it is not appropriate for exam-type questions like this. You see, the given code fragment is NOT a complete program, it is a portion of a program. And the line-numbers are given as 16,17,18,... . It means that there are something 'above' this code fragment. When answering for this type questions, you MUST assume that the invisible code IS properly defined. Therefore you can't assume something like uncaught exceptions. Please keep in mind that for the exam.

Cheers,

Devaka.
 
Ranch Hand
Posts: 686
Netbeans IDE Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is NEWS to me. Can someone confirm that? As far as I have experienced Sun exams are pretty complete and mention all assumptions
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vyas Sanzgiri wrote:That is NEWS to me. Can someone confirm that? As far as I have experienced Sun exams are pretty complete and mention all assumptions


Devaka is right in that if a piece of code in the exam doesn't start by Line Number 1, you can assume that all other parts of the code are correct (for example, necessary import statements are included, and so forth.) In other words, don't assume that some of the code you can't see is wrong or is missing something. Just focus on the code that you are being shown and assume the rest of the code is correct. But if the code starts with Line Number 1, then you have to make sure that all the code is correct (if necessary imports are missing, for example, the code will be wrong.)
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

it totally fine to have methods with (String... argv[]) . It means that both symbols can be used together.



Consider the following code:



This is the what the compiler spit out when trying to compile above:

VarArg.java:3: ')' expected
public static void main(String... argv[]) {
^
VarArg.java:3: illegal start of type
public static void main(String... argv[]) {
^
VarArg.java:3: <identifier> expected
public static void main(String... argv[]) {
^
VarArg.java:3: ';' expected
public static void main(String... argv[]) {
^
VarArg.java:6: class, interface, or enum expected
}→
^
5 errors

Means it is NOT totally fine to have methods with (String... argv[]). so we end up that his both explanations are perfectly fine and legal! EOM.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Morteza Manavi-Parast wrote:

it totally fine to have methods with (String... argv[]) . It means that both symbols can be used together.



Consider the following code:



This is the what the compiler spit out when trying to compile above:

VarArg.java:3: ')' expected
public static void main(String... argv[]) {
: ^
:



Are you sure?? The code in the code block is perfectly legal
 
Morteza Manavi-Parast
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ops... I pasted the correct version in there!! I edited it. Thanks Ankit!
 
Michael Rootman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Regarding point 1 I found where my mistake is. Instead of javac I used Oracle Java compiler (JDeveloper).
And he is totally fine with such declaration . My code get compiled and executed.
Why is that? Shouldn’t java specifications be obligated to all 3rd party compilers?





Regarding point to 2 I think it is seems a little bit exaggerated to assume such things. In the same way I can assume that before and after presented code we have opening and closing comments, so code will compile and executed. In my case both lines (18 and 19) leads to the same answer, so it is not an problem.

In any case thank you all and especially to Devaka for such great product!


 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding the first point, this will compile:


But if you try to run java MainClass or NotPublic you will get an exception because java looks for a main method which takes an array of Strings. You can fix this by overloading the main() method:

This compiles, and you can run both MainClass and NotPublic.

Notice that (String[]... args) is OK, but (String... args[]) is invalid syntax. So you can use both ... and [], but you must use them in the proper way.
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic