Given: 1. public class Threads3 implements Runnable { 2. public void run() { 3. System.out.print("running"); 4. } 5. public static void main(String[] args) { 6. Thread t = new Thread(new Threads3()); 7. t.run(); 8. t.run(); 9. t.start(); 10. } 11. } What is the result?
Compilation fails.
An exception is thrown at runtime.
The code executes and prints "running".
The code executes and prints "runningrunning".
The code executes and prints "runningrunningrunning".
The segment is the smallest unit of information that DL/I can handle.
True
False
DBD is physical description of database.
Given: public class NamedCounter { private final String name; private int count; public NamedCounter(String name) { this.name = name; } public String getName() { return name; } public void increment() { count++; } public int getCount() { return count; } public void reset() { count = 0; } } Which three changes should be made to adapt this class to be used safely by multiple threads? (Choose three.)
declare reset() using the synchronized keyword
declare getName() using the synchronized keyword
declare getCount() using the synchronized keyword
declare the constructor using the synchronized keyword
declare increment() using the synchronized keyword
Given: 7. void waitForSignal() { 8. Object obj = new Object(); 9. synchronized (Thread.currentThread()) { 10. obj.wait(); 11. obj.notify(); 12. } 13. } Which statement is true?
This code may throw an InterruptedException
This code may throw an IllegalStateException
This code may throw a TimeoutException after ten minutes
This code will not compile unless "obj.wait()" is replaced with "((Thread) obj).wait()".
Reversing the order of obj.wait() and obj.notify() may cause this method to complete normally
A call to notify() or notifyAll() from another thread may cause this method to complete normally
Given: 1. public class TestOne implements Runnable { 2. public static void main (String[] args) throws Exception { 3. Thread t = new Thread(new TestOne()); 4. t.start(); 5. System.out.print("Started"); 6. t.join(); 7. System.out.print("Complete"); 8. } 9. public void run() { 10. for (int i = 0; i < 4; i++) { 11. System.out.print(i); 12. } 13. } 14. } What can be a result?
The code executes and prints "StartedComplete"
The code executes and prints "StartedComplete0123".
The code executes and prints "Started0123Complete".
Given: 11. public class Test { 12. public enum Dogs {collie, harrier, shepherd}; 13. public static void main(String [] args) { 14. Dogs myDog = Dogs.shepherd; 15. switch (myDog) { 16. case collie: 17. System.out.print("collie "); 18. case default: 19. System.out.print("retriever "); 20. case harrier: 21. System.out.print("harrier "); 22. } 23. } 24. } What is the result?
harrier
shepherd
retriever
Compilation fails
retriever harrier
An exception is thrown at runtime
Given: 8. public class test { 9. public static void main(String [] a) { 10. assert a.length == 1; 11. } 12. } Which two will produce an AssertionError? (Choose two.)
java test
java -ea test
java test file1
java -ea test file1
java -ea test file1 file2
java -ea:test test file1
Given: 10. interface Foo {} 11. class Alpha implements Foo {} 12. class Beta extends Alpha {} 13. class Delta extends Beta { 14. public static void main( String[] args ) { 15. Beta x = new Beta(); 16. // insert code here 17. } 18. } Which code, inserted at line 16, will cause a java.lang.ClassCastException?
Alpha a = x;
Foo f = (Delta)x;
Foo f = (Alpha)x;
Beta b = (Beta)(Alpha)x;
Given: 11. public static Collection get() { 12. Collection sorted = new LinkedList(); 13. sorted.add("B"); sorted.add("C"); sorted.add("A"); 14. return sorted; 15. } 16. public static void main(String[] args) { 17. for (Object obj: get()) { 18. System.out.print(obj + ", "); 19. } 20. } What is the result?
A, B, C,
B, C, A,
The code runs with no output