[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Mike Simmons wrote:For certification, not real programming, I agree it's still a bit ambiguous, but 2, 4, 7 seems best here.
No, that is strictly defined by the Java® Language Specification. I couldn't copy the screenshot, but if the text copy of the code is accurate and it has the correct line numbers, it throws two compile‑time errors:-Johnathan White wrote:. . . I guess it's a philosophical question of what is a compile error? . . .
For once, JShell and javac gave the same results.jshell> public class ClownFish {
...> int gills = 0, double weight=2; //invalid declaration syntax
...> { int fins = gills; } //unknown variable as 'gills' not correctly declared
...> void print(int length = 3) { //invalid method parameter syntax
...> System.out.println(gills); //undeclared variable
...> System.out.println(weight); //undeclared variable
...> System.out.println(fins); //undeclared variable
...> System.out.println(length); //undeclared variable
...> } }
...>
| Error:
| <identifier> expected
| int gills = 0, double weight=2; //invalid declaration syntax
| ^
| Error:
| ',', ')', or '[' expected
| void print(int length = 3) { //invalid method parameter syntax
| ^
jshell> /exit
| Goodbye
campbell@campbell:~$ pluma Clownfish.java&
[1] 17007
campbell@campbell:~$ javac Clownfish.java
Clownfish.java:2: error: <identifier> expected
int gills = 0, double weight=2; //invalid declaration syntax
^
Clownfish.java:4: error: ',', ')', or '[' expected
void print(int length = 3) { //invalid method parameter syntax
^
2 errors
Campbell Ritchie wrote:Welcome to the Ranch
No, that is strictly defined by the Java® Language Specification. I couldn't copy the screenshot, but if the text copy of the code is accurate and it has the correct line numbers, it throws two compile‑time errors:-Johnathan White wrote:. . . I guess it's a philosophical question of what is a compile error? . . .
Jhonson Fernando wrote:So the correct answers for this question are A and C only.
If we want to include line 7 ( Choice D) in the correct answers, then the question would change something like "Line 7 cannot use undeclared variable" instead of "Line 7 generates a compiler error" for Choice D.
It is very difficult to get a compiler to prodce accurate error messags; Eclipse usually manages much better than javac.Mike Simmons wrote:. . . it's possible that different compilers will provide different lists of errors. . . . after the first error, such messages can get very confused and misleading . . .
Mike Simmons wrote:
That said, I don't find this question ambiguous really. I suggest that you can resolve it best by looking at it this way: how many things need to be changed, in the code, to make it compile correctly? Try to find how to fix the code as you read it, going linearly through the code, and look for the simplest, most straightforward fixes you can imagine on each line as you see it. Don't worry about errors on subsequent lines if they would be fixed by a change on an earlier line.
Mike Simmons wrote:
I think an actual exam question is more likely to either (a) have only one compile error, (b) only ask about the first error, or (c) not ask about where the error occurs, just offer "a compile error occurs" as an option. So, don't stress too much over this one.
Are you okay? You look a little big. Maybe this tiny ad will help:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|