Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrong answer chapter#5, question #4 (Java OCA 8 Programmer I Study Guide)

 
Evgeny Kapinos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!
Question code sample is Correct answers in guide are:
  • C. It fails to compile because the return types are not covariant.
  • E. It fails to compile because of a static modifier mismatch between the two methods.

  • BUT
    Compiler can't realize what developer try to do. Maybe developer tries to Override static method or maybe write another method (developer choose same name by mistake). Compiler can realize only one: in result class can't have two methods with same signature, also when one of them is static and another is not.

    So only answer E is correct. Answer C will be correct only after some code modifications. See last attachment in this topic. Both answers can't be applied at the same time.
    Eclipse IDE proof
    Screen Shot 2015-05-17 at 1.05.38 PM.png
    [Thumbnail for Screen Shot 2015-05-17 at 1.05.38 PM.png]
    Compilation without changes
    Screen Shot 2015-05-17 at 1.09.10 PM.png
    [Thumbnail for Screen Shot 2015-05-17 at 1.09.10 PM.png]
    Beaver chew() method became static
     
    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
    Hi Evgeny Kapinos,

    First of all, a warm welcome to CodeRanch!

    Evgeny Kapinos wrote:Compiler can't realize what developer try to do. Maybe developer tries to Override static method or maybe write another method (developer choose same name by mistake). Compiler can realize only one: in result class can't have two methods with same signature, also when one of them is static and another is not.

    There is no such thing as overriding a static method. You can only override instance methods! That's a very important rule you must remember!

    I think it's safe to say, option E is correct because you can't have a static method and an instance method with exactly the same name in the same class. Because Rodent.chew() is protected, it's also acessible through class Beaver, so you have a static method and an instance method with exactly the same name => compiler error (it would compile successfully if Rodent.chew() would have been a private) method).

    Now what would happen if we fix this compiler error (either by turningRodent.chew() into an instance method or turning Beaver.chew() into a class method)? Will the code compile successfully or not? Your code snippets clearly show the code won't compile, because the return types are not covariant. That's why option C is also correct!

    So in order to successfully compile code snippets you require 2 code changes: one to fix the static modifier mismatch and another one to fix the return types. That's why both C and E are correct and the study guide is spot-on!

    Hope it helps!
    Kind regards,
    Roel
     
    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
    Evgeny,
    Welcome to CodeRanch!

    Even though this turned out not to be an error in the book, I think it's great that you posted here. It helps others who have that same question as reading.

    One note: remember that the real exam tells you how many answer choices are correct. Which as a practical matter means you don't frequently get asked about multiple compile errors in the same question.
     
    Evgeny Kapinos
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    First of all, thanks for warm welcome!

    Roel De Nijs wrote:Now what would happen if we fix this compiler error (either by turningRodent.chew() into an instance method or turning Beaver.chew() into a class method)? Will the code compile successfully or not? Your code snippets clearly show the code won't compile, because the return types are not covariant. That's why option C is also correct!

    Your answer is pretty clear. In this case I only want to warn about difference between "2 errors" in answer, but only "1 error" after real compilation. We also able to avoid "covariant" error at all. Third available fix is: rename one of the methods. I do not want to spend your time with long holy war. Maybe additional requirement in task (like "do not rename methods") will make this task more unambiguous. I think the reason of my confuse is found.

    Jeanne Boyarsky wrote:One note: remember that the real exam tells you how many answer choices are correct. Which as a practical matter means you don't frequently get asked about multiple compile errors in the same question.

    Oh yes, certainly it will help.

    Guys, thanks a lot for quick reply and regard. Have a nice day.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Evgeny Kapinos wrote:We also able to avoid "covariant" error at all. Third available fix is: rename one of the methods. I do not want to spend your time with long holy war. Maybe additional requirement in task (like "do not rename methods") will make this task more unambiguous. I think the reason of my confuse is found.

    If you could rename a variable, method, class, interface,... you'll be able to fix plenty of compiler errors very easily Adding "do not rename methods, variable names, classes,..." to every question will become very tedious and cumbersome very quickly. Maybe it's much simpler and easier to remember that you should never think of renaming methods, variable names, classes,... unless it's explicitly stated in the question or one of the possible answer options. I have already taken a few certification exams and if I remember correctly, renaming was never mentioned as a possible answer option.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic