Wednesday, October 5, 2011

Challenge 020’s Solution

The answer to Challenge 20 is (B) A syntax error will occur. When all preconditions have been set, (being turning n into the int value 0 at the code’s start), BlueJ™ “cannot find symbol - variable x”, as the challenge never specified to mention x otherwise in the code.
 
However, if one does define variable x, the answer is (D) statement2, but not statement1, will be executed. This is because statement 1 can only be executed if both the statements “n ≠ 0” and “x ÷ n > 100” are true; however, n is 0, so statement 1 cannot be true. statement 2 is activated “else”, meaning that if no other “if” conditions can be met, this is performed, making statement 2 the only one to be executed.
 
−Phoenix

1 comment:

  1. Hi Phoenix, it's a little trippy, this notion of a precondition.

    The precondition is just a way of representing abstraction. It's telling us that all sorts of things could have happened leading up to this particular segment of the program. That's not important, other than to just know that x = 0. When testing this in the compiler, as I believe you've discovered, you have to set the value of x to run the code.

    Your analysis here is spot on, and 15 XPs are awarded.

    ReplyDelete