Java Reference
In-Depth Information
which of the statements is true about the following MyProducer program? (Select one.)
1. public class MyProducer {
2. public static void main(String [] args) {
3. StringBuffer sb = new StringBuffer(“”);
4. MyConsumer consumer = new MyConsumer(sb);
5. consumer.start();
6. Thread.yield();
7. sb.append(“abc”);
8. synchronized(sb) {
9. sb.notifyAll();
10. }
11. System.out.println(sb);
12. }
13. }
A. The output is Waiting following by cba.
B. The output is abc.
C. Either A or B always occurs.
D. The code may generate an exception at runtime.
E. The code does not compile.
12. What is the result of the following Reverser program?
1. public class Reverser extends Thread {
2. private StringBuffer sb;
3.
4. public Reverser(StringBuffer sb) {
5. this.sb = sb;
6. }
7.
8. public void run() {
9. sb.reverse();
10. }
11.
12. public static void main(String [] args) {
13. StringBuffer s = new StringBuffer(“xyz”);
14. Reverser r = new Reverser(s);
15. r.start();
16. System.out.print(s);
17. r.start();
18. System.out.print(s);
19. }
20. }
Search WWH ::




Custom Search