(5) is correct because sleep() will always pause the currently running thread for at least the duration specified in the sleep argument (unless an interrupted exception is thrown).
(6) is correct because, assuming that the thread you're calling join() on is alive, the thread calling join() will immediately block until the thread you're calling join() on is no longer alive.
(1) is wrong, but tempting. The yield() method is not guaranteed to cause a thread to leave the running state, although if there are runnable threads of the same priority as the currently running thread, then the current thread will probably leave the running state.
(3) and (4) are incorrect because they don't cause the thread invoking them to leave the running state.
(7) is wrong because there's no such method.
class X implements Runnable { public static void main(String args[]) { /* Missing code? */ } public void run() {} }
try { Float f1 = new Float("3.0"); int x = f1.intValue(); byte b = f1.byteValue(); double d = f1.doubleValue(); System.out.println(x + b + d); } catch (NumberFormatException e) /* Line 9 */ { System.out.println("bad number"); /* Line 11 */ }
String a = "newspaper"; a = a.substring(5,7); char b = a.charAt(1); a = a + b; System.out.println(a);
Option B is incorrect because to call wait(), the thread must own the lock on the object that wait() is being invoked on, not the other way around.
Option C is wrong because notify() is defined in java.lang.Object.
Option D is wrong because notify() will not cause a thread to release its locks. The thread can only release its locks by exiting the synchronized code.
class Test1 { public int value; public int hashCode() { return 42; } } class Test2 { public int value; public int hashcode() { return (int)(value^5); } }
Option A and D are incorrect because these classes are legal.
Option B is incorrect based on the logic described above.
x = 0; if (x1.hashCode() != x2.hashCode() ) x = x + 1; if (x3.equals(x4) ) x = x + 10; if (!x5.equals(x6) ) x = x + 100; if (x7.hashCode() == x8.hashCode() ) x = x + 1000; System.out.println("x = " + x);
Option A is incorrect because if the hashCode() values are not equal, the two objects must not be equal.
Option C is incorrect because if equals() is not true there is no guarantee of any result from hashCode().
Option D is incorrect because hashCode() will often return == even if the two objects do not evaluate to equals() being true.
class Test { public static void main(String [] args) { printAll(args); } public static void printAll(String[] lines) { for(int i = 0; i < lines.length; i++) { System.out.println(lines[i]); Thread.currentThread().sleep(1000); } } }
A is incorrect, but it would be correct if the InterruptedException was dealt with.
B is incorrect, but it would still be incorrect if the InterruptedException was dealt with because all Java code, including the main() method, runs in threads.
C is incorrect. The sleep() method is static, so even if it is called on an instance, it still always affects the currently executing thread.
public class MyProgram { public static void throwit() { throw new RuntimeException(); } public static void main(String args[]) { try { System.out.println("Hello world "); throwit(); System.out.println("Done with try block "); } finally { System.out.println("Finally executing "); } } }
interface Base { boolean m1 (); byte m2(short s); }
(1) is incorrect because interfaces don't implement anything. (2) is incorrect because classes don't extend interfaces. (5) is incorrect because interface methods are implicitly public, so the methods being implemented must be public.
public class MyOuter { public static class MyInner { public static void foo() { } } }
MyOuter.MyInner mi = m.new MyOuter.MyInner();
Option B is incorrect because it doesn't use the enclosing name in the new.
Option C is incorrect because it uses incorrect syntax. When you instantiate a nested class by invoking new on an instance of the enclosing class, you do not use the enclosing name. The difference between Option A and C is that Option C is calling new on an instance of the enclosing class rather than just new by itself.
Option D is incorrect because it doesn't use the enclosing class name in the variable declaration.
Comments
There are no comments.Copyright ©CuriousTab. All rights reserved.