• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

instance initializer block question (building blocks)

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello everyone, I have a question in the ocp book, the book says that option 'B' is incorrect, how is this possible if line 2 is not going to compile, and in line 3 we are trying to use a variable that didn't compile in line 2? do we just ignore that it didn't compile and use it in the initializer block? can someone explain this to me please, java 17.
Screenshot-2024-09-07-151938.png
[Thumbnail for Screenshot-2024-09-07-151938.png]
 
Bartender
Posts: 10968
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Marshal
Posts: 80133
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I shall move you to our certification forum.
 
author & internet detective
Posts: 42056
926
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's nothing wrong with line 3. Line 2 doesn't compile.

The compiler doesn't complain about line 3 because it doesn't have any errors. And if line 2 were fixed, line 3 would be fine.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am also similarly confused about the question - not the code.

My compiler shows 2 errors (line 2 and 4).

The answer states there should be 3 errors (line 2, 4 and 7).

If I were the compiler i'd complain about lines 2,3,4,5,6,7 and 8.  



I guess it's a philosophical question of what is a compile error?

More importantly, should I be concerned about it for the exam?
 
Rancher
Posts: 5099
82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error on line 3, 5, and 6 go away if line two is fixed, replace the comma with a semicolon, and a new line.

The errors on line 4 and 8 requires a separate fix to line 4, remove the "= 3" part.

The error on line 7 requires a separate fix - though it would probably make sense to fix it by removing braces on line 3.  But line 3 wasn't an error on its own - it was line 7 that had a problem with it.  Line 7 could also be fixed by replacing "fins" with anything else that's actually accessible on line 7.

There are 3 independent errors here, on lines 2, 4, and 7.  Or maybe 2, 4, and 3.  But between the 7 and the 3, the place the compiler is complaining about is 7.  So that's the best answer.

In real programming, not certification, it's best to just fix the first error and recompile to see what remains.  It gets very messy trying to follow what the compiler is thinking after a syntax error.

For certification, not real programming, I agree it's still a bit ambiguous, but 2, 4, 7 seems best here.
 
Johnathan White
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:For certification, not real programming, I agree it's still a bit ambiguous, but 2, 4, 7 seems best here.



This is an exam where there is no room for careless oversight. Where they try and trick you at every turn and I'm supposed to say line 3 doesn't generate a compile error? Never! 😂

I hope there's less ambiguity in the actual exam!
 
Campbell Ritchie
Marshal
Posts: 80133
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Johnathan White wrote:. . . I guess it's a philosophical question of what is a compile error? . . .

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:-

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

For once, JShell and javac gave the same results.
I am sure the real questions will be unambiguous. But not easier
 
Johnathan White
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Johnathan White wrote:. . . I guess it's a philosophical question of what is a compile error? . . .

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:-



It was meant in the context of this question.

My thinking is line 2 is gobbledygook and so line 3 is referencing an undeclared variable and should generate a compile error.

However, it could be argued the first part of line 2 is valid, the compiler interprets the correct declaration of the variable and hence no compile error. That is of course, the compiler interprets left to right!
 
MyExamCloud Software Support
Posts: 755
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The given code in the  ClownFish.java file will result in two compiler errors when compiled as it is without any modifications:

1. In line 2, an error will occur because the syntax for variable declaration is incorrect.


2. In line 4, an error will occur because the method print is incorrectly defined.

The correct syntax for methods in Java is access modifier return type methodName (parameter list), which includes the data type and name of each parameter.  The print method can be rewritten as void print(int length), with no value assigned to the parameter.

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.
 
Johnathan White
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



I don't understand how an undeclared variable doesn't count as a compile error.

If the question is based on what the terminal output is we need to be a human JVM as well as a human compiler.
 
Jhonson Fernando
MyExamCloud Software Support
Posts: 755
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler will never reach Line 7. without fixing line 4.
So you will get:


Only two error...


 
Johnathan White
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then the question is when does a compiler continue compiling and when does it not? Or more specifically: how does it carry on looking for errors when it has already found one?

If I were the compiler I'd have given up at line 2!

The fact that pretty much everyone has a different answer in this thread suggests questions on multiple errors can be ambiguous.
 
Jhonson Fernando
MyExamCloud Software Support
Posts: 755
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct Johnathan.

Either revising the question to "What will happen if the following code is compiled and run?" or modifying Choice D to "Line 7 cannot use undeclared variable" would eliminate any confusion.
 
Mike Simmons
Rancher
Posts: 5099
82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When more than one error is present in the code, it's quite difficult to know for sure what will be reported as an error, and what will not.  Of course you can find out by running the compiler yourself.  However, in many cases, the exact behavior is not specified in the Java Language Specification, and it's possible that different compilers will provide different lists of errors.  That is, the JLS is very specific about what constitutes a compile-time error and what does not.  However, nothing is specified about what exactly gets reported when the compiler errors out.  It generally tries to identify where the error was and what the problem was.  But after the first error, such messages can get very confused and misleading, and may also be incomplete.  Once you've committed the first compile error, the compiler may have a rather different understanding than you do about what the remaining code means.  Also compilers are often multi-pass compilers, meaning they go through the code multiple times, looking for different things and performing different compilation tasks.  If an error is detected on the first pass, later steps may not be performed, and so the errors that those later steps would have detected, may not be reported.

Now, I don't know exactly what modern certification test questions are like.  Historically though, they are created pretty carefully, with a lot of people providing feedback to prevent incorrect or ambiguous questions.  They also perform ongoing review of the tests as people take them - if they find that there's a question that is frequently answers "incorrectly" even by people who otherwise ace the exam, they do extra review of that question, to see if there's a problem with the question itself.  That doesn't mean that incorrect or ambiguous questions never happen, but they're pretty rare, probably a lot rarer than incorrect or ambiguous questions on mock exams, even from highly respected authors.  So, don't panic too much about ambiguity in questions you find outside the actual exam.  Just focus on giving what seems like the best answer, under the circumstances, and understanding the rationale given when the "official" answer is different than you expect.

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.  Using that approach here leads to this code, with 3 fixes noted in comments:

Those fixes are on lines 2, 4, and 7.  That's what they are looking for in this question.  Yes, you could have also fixed line 7 by going back to line 3 and removing the braces - but remember that line 3 wasn't originally a compile-time error.  (It's weird and useless, but not an error.)  It only becomes a possible solution when we got to line 7 and wondered what "fins" was.  So, rule of thumb for you: don't look backward when looking for problems this way.  Look for fixes on the line you're on, then move on.

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.
 
Campbell Ritchie
Marshal
Posts: 80133
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 . . .

It is very difficult to get a compiler to prodce accurate error messags; Eclipse usually manages much better than javac.
 
Johnathan White
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.  



I like this reasoning, although I don't necessarily find it intuitive.

It also has edge cases, for example, is the easiest fix on line 2 to replace a comma with a semi colon or to delete the double declaration? Although not an issue on this example, it could lead to subsequent problems later in the code. We would need an order of precedence of fixes to resolve this.

Forgive me if I'm being pedantic!

 
Johnathan White
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



I shall take solace in this!
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic