• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Exam 1Z0-829 Study Guide Chapter 3 Flow Scoping Review Questions 8 and 28

 
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chapter 3 has 2 review questions regarding Pattern Matching and Flow Scoping that have conflicting answers. Please explain what is different between them.

Question 8 has the following code:
What is the output of calling printType(11)?
31: void printType(Object o) {
32:    if(o instanceof Integer bat) {
33:       System.out.print("int");
34:    } else if(o instanceof Integer bat && bat ˂ 10) {
35:       System.out.print("small int");
36:    } else if(o instanceof Long bat || bat ˂= 20) {
37:       System.out.print("long");
38:    } default {
39:       System.out.print("unknown");
40:    }
41: }

In the answer explanation, it states that "The first two pattern-matching statements compile without issue. The variable bat is allowed to be used again, provided it is no longer in scope."

However, in Question 28, the code is similar:
What is the output of calling getFish("goldie")?
40: void getFish(Object fish) {
41:    if (!(fish instanceof String guppy))
42:       System.out.print("Eat!");
43:    else if (!(fish instanceof String guppy)) {
44:       throw new RuntimeException();
45:    }
46:    System.out.print("Swim!");
47: }

This time the answer explanation states "Based on flow scoping, guppy is in scope after lines 41-42 if the type is not a String. In this case, line 43 declares a variable guppy that is a duplicate of the previously defined local variable defined on line 41. For this reason, the code does not compile..."

Why is the duplicate variable declaration allowed in question 8 but not in question 28?

Thanks in advance
 
Marshal
Posts: 80485
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you copied the first block correctly? My JShell doesn't like the default in line 38.
 
Crystal Kirscht
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it was part of the question.  The correct answer was " The code contains two lines that do not compile." Here is the whole explanation:

The first two pattern-matching statements compile without issue. The variable bat is allowed to be used again, provided it is no longer in scope. Line 36 does not compile, though. Due to flow scoping, if s is not a Long, then bat is not in scope in the expression bat ˂= 20. Line 38 also does not compile as default cannot be used as part of an if/else statement. For these two reasons, option G is correct.

I don't know where 's' came from, I'm assuming they mean 'o'
 
Campbell Ritchie
Marshal
Posts: 80485
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Crystal Kirscht wrote:.  . . " The code contains two lines that do not compile."

I only had one line with compile time errors in the first code block.

jshell> void printType(Object o) {
  ...>    if(o instanceof Integer bat) {
  ...>       System.out.print("int");
  ...>    } else if(o instanceof Integer bat && bat < 10) {
  ...>       System.out.print("small int");
  ...>    } else if(o instanceof Long bat || bat = 20) {
  ...>       System.out.print("long");
  ...>    } default {
  ...>       System.out.print("unknown");
  ...>    }
  ...> }
|  Error:
|  orphaned default
|     } default {
|       ^
|  Error:
|  : or -> expected
|     } default {
|              ^

Both errors are about the default.

. . . I don't know where 's' came from, I'm assuming they mean 'o'

I think you are probably correct; that should go down as an erratum.
 
author & internet detective
Posts: 42109
934
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

Crystal Kirscht wrote:Why is the duplicate variable declaration allowed in question 8 but not in question 28?


This is the essence of what makes flow scoping tricky!

First, look at this example. C is not in scope at the println because of the return statement. If o is a Cat, the method returns and never gets to the println. Question 8 uses an else rather than a return, but the idea is the same. If you get to the else you know the type didn't match.



Now what about this one?  The only change is that I've negated the if statement (and added parens so the negate compiles.)  This time, the println does compile. The if statement checked if o is not a Cat and returned immediately. This pattern is useful for validations. (often throwing an exception.) So by the time we get to the print, we've guaranteed o is a Cat and are able to use c just fine. This is equivalent to the example in question 28.



If my teammates wrote code like question 28, I'd ask them to change it. But the goal here is testing understanding of flow scope. And it worked. You found out that you were missing a part of the concept when reading this chapter and not on the exam. Far better time to learn this!


The s vs o in the explanation is an errata and I've logged it crediting you.
 
Crystal Kirscht
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I understand it now but let me confirm:

For question 8 calling printType(11):

31: void printType(Object o) {
32:    if(o instanceof Integer bat) {
33:       System.out.print("int");
// We exit the if statement here since 11 is an Integer

34:    } else if(o instanceof Integer bat && bat ˂ 10) { Since o was an integer bat is no longer in scope and can be redefined?
35:       System.out.print("small int");
36:    } else if(o instanceof Long bat || bat ˂= 20) {
37:       System.out.print("long");
38:    } default {
39:       System.out.print("unknown");
40:    }
41: }

For questions 28 calling getFish("goldie")?

40: void getFish(Object fish) {
41:    if (!(fish instanceof String guppy))
42:       System.out.print("Eat!");
fish is an instance of String so we don't exit the if statement and guppy is still in scope

43:    else if (!(fish instanceof String guppy)) {  Since guppy is still in scope we can't redefine it
44:       throw new RuntimeException();
45:    }
46:    System.out.print("Swim!");
47: }


 
Jeanne Boyarsky
author & internet detective
Posts: 42109
934
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
You got it!
 
reply
    Bookmark Topic Watch Topic
  • New Topic