• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to count the number of compiler errors? (Java OCA 8 Programmer I Study Guide, Sybex)

 
Esbjörn Rundberg
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!

In most cases I get the number of compilation errors in code snippets correct, at least when I see them, but at the same time I recognize that it depends on what is to be considered the fault.

As an example, this is a truncated version of review question 13 in chapter 4:


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.

When doing the exam, which is correct way to count the number of compilation errors in cases such as this?

Best regards
Esbjörn
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Line 2 isn't faulty per se. This runs fine:






 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good question. When asked about compiler errors, think of it as "which lines themselves will fail to compile." The wording isn't about how you'd fix it, it is about what is wrong.

For example this code has two lines that will fail to compile.


You could of course fix it by changing one line. But that's not what the question asks.

Where this can get tricky is that sometimes the compiler only reports earlier errors. And this varies by compiler. It even varies between an IDE like Eclipse and the command line. So you should answer without this optimization.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Esbjörn Rundberg
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick answers!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic