java

java

Wednesday 14 December 2011

SCJP Questions 131-14

Question 131
Click the Exhibit button.
1. class Computation extends Thread {
2.
3. private int num;
4. private boolean isComplete;
5. private int result;
6.
7. public Computation(int num) { this.num = num; }
8.
9. public synchronized void run() {
10. result = num * 2;
11. isComplete = true;
12. notify();
13. }
14.
15. public synchronized int getResult() {
16. while (!isComplete) {
17. try {
18. wait();
19. } catch (InterruptedException e) { }
20. }
21. return result;
22. }
23.
24. public static void main(String[] args) {
25. Computation[] computations = new Computation [4];
26. for (int i = 0; i < computations.length; i++) {
27. computations[i] = new Computation(i);
28. computations[i] .start();
29. }
30. for (Computation c : computations)
31. System.out.print(c.getResult() +“ “);
32. }
33. }
What is the result?
A. The code will deadlock.
B. The code may run with no output.
C. An exception is thrown at runtime.
D. The code may run with output “0 6”.
E. The code may run with output “2 0 6 4’.
F. The code may ruin with output “0 2 4 6”.
Answer: F

Question 132
Given:
7. void waitForSignal() {
8. Object obj = new Object();
9. synchronized (Thread.currentThread()) {
10. obj.wait();
11. obj.notify();;
12. }
13. }
Which is true?
A. This code may throw an InterruptedException.
B. This code may throw an IllegalStateException.
C. This code may throw a TimeoutException after ten minutes.
D. This code will not compile unless “obj.wait()” is replaced with
“((Thread) obj).wait()”.
E. Reversing the order of obj.wait() and obj.notify() may cause this
method to complete normally.
F. A call to notify() or notifyAll() from another thread may cause this
method to complete normally.
Answer: B

Question 133
Given:
foo and bar are public references available to many other threads. foo
refers to a Thread and bar is an Object. The thread foo is currently
executing bar.wait(). From another thread, which statement is the
most reliable way to ensue that foo will stop executing wait()?
A. foo.notify();
B. bar.notify();
C. foo.notifyAll();
D. Thread.notify();
E. bar.notiFYAll();
F. Object.notify();
Answer: E

Question 134
Which two are true? (Choose two.)
A. An encapsulated, public class promotes re-use.
B. Classes that share the same interface are always tightly
encapsulated.
C. An encapsulated class allows subclasses to overload methods, but
does NOT allow overriding methods.
D. An encapsulated class allows a programmer to change an
implementation without affecting outside code.
Answer: AD

Question 135
Given:
1. package test;
2.
3. class Target {
4. public String name = “hello”;
5. }
What can directly access and change the value of the variable name?
A. any class
B. only the Target class
C. any class in the test package
D. any class that extends Target
Answer: C

Question 136
Given:
1. public class Target {
2. private int i = 0;
3. public int addOne() {
4. return ++i;
5. }
6. }
And:
1. public class Client {
2. public static void main(String[] args) {
3. System.out.println(new Target().addOne());
4. }
5. }
Which change can you make to Target without affecting Client?
A. Line 4 of class Target can be changed to return i++;
B. Line 2 of class Target can be changed to private int i = 1;
C. Line 3 of class Target can be changed to private int addOne() {
D. Line 2 of class Target can be changed to private Integer i = 0;
Answer: D

Question 137
Given:
1. package geometry;
2. public class Hypotenuse {
3. public InnerTriangle it = new InnerTriangle();
4. class InnerTriangle {
5. public int base;
6. public int height;
7. }
8. }
Which is true about the class of an object that can reference the
variable base?
A. It can be any class.
B. No class has access to base.
C. The class must belong to the geometry package.
D. The class must be a subclass of the class Hypotenuse.
Answer: C

Question 138
Given:
11. class ClassA {}
12. class ClassB extends ClassA {}
13. class ClassC extends ClassA {}
and:
21. ClassA p0 = new ClassA();
22. ClassB p1 = new ClassB();
23. ClassC p2 = new ClassC();
24. ClassA p3 = new ClassB();
25. ClassA p4 = new ClassC();
Which three are valid? (Choose three.)
A. p0 = p1;
B. p1 =p2;
C. p2 = p4;
D. p2 = (ClassC)p1;
E. p1 = (ClassB)p3;
F. p2 = (ClassC)p4;
Answer: AEF

Question 139
Given:
11. class Animal { public String noise() { return “peep”; } }
12. class Dog extends Animal {
13. public String noise() { return “bark”; }
14. }
15. class Cat extends Animal {
16. public String noise() { return “meow”; }
17. }
.....
30. Animal animal = new Dog();
31. Cat cat = (Cat)animal;
32. System.out.printIn(cat.noise());
What is the result?
A. peep
B. bark
C. meow
D. Compilation fails.
E. An exception is thrown at runtime.
Answer: E

Question 140
Given:
11. abstract class Vehicle { public int speed() { return 0; } }
12. class Car extends Vehicle { public int speed() { return 60; } }
13. class RaceCar extends Car { public int speed() { return 150; }}
......
21. RaceCar racer = new RaceCar();
22. Car car = new RaceCar();
23. Vehicle vehicle = new RaceCar();
24. System.out.println(racer.speed() + “, ‘ + car.speed()
25. + “, “+ vehicle.speed());
What is the result?
A. 0, 0,0
B. 150, 60, 0
C. Compilation fails.
D. 150, 150, 150
E. An exception is thrown at runtime.
Answer: D

No comments:

Post a Comment