#Reviewing Questions : For Loops and Strings

1 messages · Page 1 of 1 (latest)

pine yarrowBOT
#

<@&987246399047479336> please have a look, thanks.

south oxide
#

The question is whether it contains, not whether it ends with.

#

And i only exists within the bounds of that loop.

#

@sour tulip

south oxide
#

It says it needs to contain the suffix, not that it needs to end with that word.

#

I would love someJava with this.

gloomy mortar
#

re: question 40

The question is poorly worded. It could mean checking whether s contains "Java", whether s ends with "Java".

However since all of the answers are invalid Java, the question is pretty broken...

a. s.lastindexof("Java") >= 0 should be s.lastIndexOf("Java") >= 0 to check for contains.
b. s.substring(s.length() = 5).equals("Java") should be s.substring(s.length() - 5).equals("Java") to be valid... but wrong for either interpretation of the question.
c. s.substring(s.length() = 4).equals("Java") should be s.substring(s.length() - 4).equals("Java") to be valid... And this would test for ending with... except if s is shorter than 4, in which case it's a runtime error.
d. This is just messed but has the same problem as c for errors once corrected
e. s.endswith("Java") should be s.endsWith("Java") which would be correct for an ending-with interpretation. The method name is case-sensitive though so endswith is not the same as endsWith.

#

re: question 31.

The wording of the question is important here. If you interpreted it (incorrectly) as "What is the value of i after the following for loop" then 'undefined' would technically be incorrect. In the formality of programming languages, an "undefined" value refers to indeterminate state (eg may or may not have been initialised). The Java compiler never allows such states. Regardless, the value is not undefined because there is not value called i.

The question is not about the value of i, but of i itself. And the syntax-error would be that the compiler cannot find the symbol i as i is undefined at that point in the code.