Esbjörn Rundberg wrote:The compiler will give one error when I try to compile, and it could easily be solved by just removing final from line 2, but the answer is two errors; both line 2 and 5 are faulty.
This topic discusses exactly the same question. So it's definitely worth reading
Esbjörn Rundberg wrote:When doing the exam, which is correct way to count the number of compilation errors in cases such as this?
I have never encountered a question like this one where you literally have to count the compiler errors. Most of the times you have a "Compilation fails" option. Some questions might have a few options like "Compilation fails due to an error on line X" and/or "Compilation fails due to an error on multiple lines". But in the code snippet you posted, there are (obviously) 2 compiler errors: static (class) variable
bench is final and should be appropriately initialized (either directly or using a static initializer block) and you can't reassign a final static (class) variable in a method.
And you could indeed remove
final from line 2 and the code will compile successfully, so you might think there's just 1 compiler error.
But you should assume the code snippets are like strings, they are immutable
So removing
final from line 2 is simply not possible. The only case when you have to consider changes to the given code snippet is if it's mentioned in one of the answer options. For example a possible answer option for this code snippet could be:
if final is removed from line 2, the code compiles successfully. So the answer options clearly tell you what possible changes you should consider, but you should not try to invent your own changes. Otherwise it would be sometimes very easy: e.g. a code snippet has an invalid method overload and you don't know why, you could simple change the method's name and problem is solved
Hope it helps!
Kind regards,
Roel