a = a++ + a;//a = 11
5. class Order implements Runnable {
6. public void run() {
7. try { Thread.sleep(2000); } catch (Exception e) { }
8. System.out.print("in ");
9. }
10. public static void main(String [] args) {
11. Thread t = new Thread(new Order());
12. t.start();
13. System.out.print("pre ");
14. try { t.join(); } catch (Exception e) { }
15. System.out.print("post ");
16. } }
sleep() is guaranteed to cause the current Thread to stop executing for at least the specified sleep duration.
JVM decides that the worker thread runs first. In this case, the thread sleeps (may not sleep!, depends on JVM) but lets assume it sleeps. Then in gets printed first.
sleep() is guaranteed to cause the current Thread to stop executing for at least the specified sleep duration.
But that doesn't make sense to me. The Thread t is the thread that prints "in". If it gets put to sleep how can this mean that "in" will be printed first? Doesn't t getting put to sleep suggest the opposite, that Thread main will jump in an print "pre"?"In this case...the Thread [t] sleeps... Then in gets printed first"
This ALWAYS give the same hashCode including when you have equal objects, so this must be correct.return 0
Scenario 2 shows a situation where the a's are different and the objects are deemed equal, so "return a" fails.return a
Under both equality scenarios the sum of a and b are the same, so this choice is correct.return a+b
The sign of the result will be different under equality scenario 2. This option fails. (return Math.abs(a-b) would work)return a-b
XOR is a bitwise "difference detector". It flips bits on when bits in corresponding slots in 2 numbers are different. (e.g. one is a 1 and the other is a 0). Under scenario 1 a^b will clearly return the same value for both objects. Under scenario 2 the order of the arguments is reversed, but since XOR is commutative it doesn't matter; a^b is the same as b^a. This choice is correct.return a^b
This one makes me glad that bitwise shift operators are off the exam. I'm sure that there must be a better way to proceed, but I set out to find a situation where the objects could be the same and different hashcodes could still be produced. Scenario 1 is trivial (tautology). Scenario 2 is interesting. I assumed that a was all 1's and b was a pattern of repeating 1's and 0's. When you left shift you zero fill so this gave mereturn a<<16|b
K&B 1.5 pg 699 says that sleep() is guaranteed to cause the current Thread to stop executing for at least the specified sleep duration.
throw new FileNotFoundException();
A legal non-abstract implementing class has the following properties... It must not declare any checked exceptions that are broader than the exceptions declared in the interface method.
test() in pkg1.Test cannot implement test() in pkg1.Testable; overridden method does not throw java.io.FileNotFoundException
public void test() throws FileNotFoundException{
That sounded fishy to me so I coded up a little sample app to test it."Interfaces can be implemented by any class, from any inheritance tree".
pkg1.Testable is not public in pkg1; cannot be accessed from outside package
import pkg1.Testable;