• 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

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

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






 
author & internet detective
Posts: 41860
908
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
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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick answers!
 
reply
    Bookmark Topic Watch Topic
  • New Topic