Java Magazine, Mar/Apr 2018
ORACLE COM JAVAMAGAZINE MARCH APRIL 2018 101 fix this Given these differences its clear that although write once run anywhere generally works well allowing you to create platform independent programs quite easily there are a few things you can do that would prevent code from working properly As a result option D is correct The final option considers whether Javas multithreading system can guarantee the same output on diferent systems In fact one issue that arises with threaded code is that a deliberate design efort is necessary if a programmer wants to ensure that the same output is reliably presented even with the same machine running the same program several times One fundamental reason is that running two threads concurrently ofers no guarantee that the threads proceed at the same speed each time they run Therefore at the very least messages output by these threads might appear interleaved in difering orders Because the threading system does not intrinsically guarantee exactly consistent output even on the same machine it certainly cannot be guaranteed on diferent machines Of course the threading libraries provide tools that allow a programmer to deliberately create such guarantees when they are needed but this requires deliberate design by the programmer and is not simply a result of the language or the JVM Because of this option E must be incorrect Answer 3 The correct answer is option B The essence of this question is that a lambda expression does not create a new scope for names In particular Java Language Specification section 15272 notes the following Unlike code appearing in anonymous class declarations the meaning of names and the this and super keywords appearing in a lambda body along with the accessibility of referenced declarations are the same as in the surrounding context except that lambda parameters introduce new names In other words the value of this in the body of the lambda in the code shown in question 3 does not refer to the lambda itself but instead refers to the enclosing instance of the TryThis class As a result when the lambda invokes this speak it calls the method defined in the TryThis class not the default method in the Something interface As a result the code prints the output Bonjour Because of this option B is correct and option A is incorrect
You must have JavaScript enabled to view digital editions.