Ask your own question, for FREE!
Computer Science 15 Online
OpenStudy (fanduekisses):

JAVA Logical Operators, conditions, Booleans, etc. I got this question wrong

OpenStudy (fanduekisses):

OpenStudy (fanduekisses):

So the index cannot be greater than the length of a, which is an array right?

OpenStudy (woodrow73):

You noted that if n > i, that the boolean expression at the top would always result in true. This is false. You are right in the respect that i will never surpass the limit of the array, thus never causing an out-of-bounds exception error, however, in order for the boolean expression at the top to be true, you need to know that a[i] == 0 && a[i-1] == 0; so that regardless of what i's value is, ( i >= n || i <= n), one side of the || logical operator will always result in true. Though your teacher is also wrong for not accounting for i's value being ( i >=0 && i < n), with my addition being added to the deduction that: (a[i] == 0 && a[i-1] == 0), the boolean expression at the top will always result in true for the values inputted for n amount of arrays. Within java, it's important to note the PEMDAS of logical operators -- know that (not - and - or) is the order aka; (notandor), so the and statements on both sides execute before the or statement in the middle executes. Personally I found the question poorly worded in that it left me 'assuming', which any well-written question didn't. note that I'm assuming that your job is to pick the appropriate multiple choice answer, which will result in the boolean expression at the top always resulting in true.

OpenStudy (rsmith6559):

The index can't be larger than array.length() - 1; because arrays are zero indexed.

OpenStudy (woodrow73):

@rsmith6559 in java at least, there are no parenthesis after length.. I believe because it calls a field, not a method- but I'm not positive about that.

OpenStudy (e.mccormick):

Yes, it is a built in and no () used in Java.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!