• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock exam 3 explanation clarification request

 
Ray Kolbe
Greenhorn
Posts: 19
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy,

Mock exam 3 in Sybex study guide has a question about a switch statement using badMovie and movieRating as a couple of variable names.

For brevity let's use a more concise but similar code example as the book:



If I'm reading the explanation in the book correctly, it states that if "x" is set to final then the first case statement would cause a compilation error.

From the book:

Note that if badMovie was made a final constant variable with the same value of 0, then option A would be considered correct too.


Option A refers to the first case statement causing a compilation error.

However, I receive the same compilation error whether x is final or not, as I would expect. Specifically:


    error: duplicate case label
      case x: break;
      ^

1 error


Any clarification is appreciated. Thanks!
 
Ray Kolbe
Greenhorn
Posts: 19
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clarification on my post.

I *do* receive different errors when "x" is final vs. when it is not.

When x is not final I receive:


    error: constant expression required
      case x: break;
      ^

1 error


and when x is final:


    error: duplicate case label
      case x: break;
      ^

1 error


I expect both of those compilation errors. However, my request for clarification remains. How is the first case statement an issue whether x is final or not?

Thanks!
 
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
Ray,
Here's what the compiler is "thinking"

Compile #1

  • Case statement has value 0. Good.
  • Case statement has value x. Hey! You didn't make x final! I can't compile a case statement that isn't final. Fix it, would you?


  • Compile #2

  • Case statement has value 0. Good.
  • Case statement has value x. That's a final variable so no problem there.
  • The value of x is 0. Uh oh. I already have a 0 in this switch. Compilation fails! Fix please.
  •  
    Ray Kolbe
    Greenhorn
    Posts: 19
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeanne Boyarsky wrote:
    Compile #2

  • Case statement has value 0. Good.
  • Case statement has value x. That's a final variable so no problem there.
  • The value of x is 0. Uh oh. I already have a 0 in this switch. Compilation fails! Fix please.


  • Correct. I understand this and I think you demonstrated just now that the explanation in the mock exam is either incorrect or needs further clarification, unless of course I'm misinterpreting the explanation.

    In the quote above you state "Case statement has value 0, Good.", which means no compilation error should happen here. However, in the explanation in the mock exam it states that this scenario would cause a compilation error.

    Excerpts from the mock exam:

    A. the code will not compile because of line 6.


    Excerpt from explanation:

    Note that if badMovie was made a final constant variable with the same value of 0, then option A would be considered correct too.


    Hope my explanation was clear.

    Thanks!
     
    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
    Ray,
    This is an artifact of English not being as precise as coding languages . The exam doesn't say the compiler error will occur there. Just that the line contributes to the error. So it isn't an error.

    The most important thing is that you understand it of course and you do.
     
    Ray Kolbe
    Greenhorn
    Posts: 19
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Jeanne,

    Thanks for the response. I'm still not reading the explanation (in the mock exam) that way but that's ok.

    I believe I understand the explanation(s) and the takeaway (correct me if I'm wrong) is that the compilation error in both cases is with the second case label: scenario 1 being that the case label is not compile-time constant and scenario 2 is that there are two case labels that are the same, which is a no-no! However, in scenario 2 both case labels play a role leading to the compilation error.

    Sound about right?

    Thanks for your time! It's much appreciated.
     
    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
    Ray Kolbe wrote:Sound about right?

    Yes. It amounts to "when the compiler finds an error, it sometimes stops looking for more"
     
    Ray Kolbe
    Greenhorn
    Posts: 19
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeanne Boyarsky wrote:
    Ray Kolbe wrote:Sound about right?

    Yes. It amounts to "when the compiler finds an error, it sometimes stops looking for more"


    Right. But in this case an error wasn't found until the second case label (in both scenarios you listed above and what is shown/explained on the mock exam).
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic