java

java

Wednesday 14 December 2011

SCJP Questions 111-120

Question 111
Given:
11. String test = “Test A. Test B. Test C.”;
12. // insert code here
13. String[] result = test.split(regex);
Which regular expression inserted at line 12 will correctly split test into
“Test A,” “Test B,” and “Test C”?
A. String regex = “”;
B. String regex = “ “;
C. String regex = “.*“.
D. String regex = “\\s”
E. String regex = “\\.\\s*”;
F. String regex = “\\w[ \.] +“;
Answer: E

Question 112
Given:
12. System.out.format(”Pi is approximately %d.”, Math.PI);
What is the result?
A. Compilation fails.
B. Pi is approximately 3.
C. Pi is approximately 3.141593.
D. An exception is thrown at runtime.
Answer: D

Question 113
Given:
12. String csv = “Sue,5,true,3”;
13. Scanner scanner = new Scanner( csv);
14. scanner.useDelimiter(”,”);
15. int age = scanner.nextInt();
What is the result?
A. Compilation fails.
B. After line 15, the value of age is 5.
C. After line 15, the value of age is 3.
D. An exception is thrown at runtime.
Answer: D

Question 114
Which two code fragments will execute the method doStuff() in a
separate thread? (Choose two.)
A. new Thread() {
public void run() { doStuff(); }
}
B. new Thread() {
public void start() { doStuff(); }
}
C. new Thread() {
public void start() { doStuff(); }
} .run();
D. new Thread() {
public void run() { doStuff(); }
} .start();
E. new Thread(new Runnable() {
public void run() { doStuff(); }
} ).run();
F. new Thread(new Runnable() {
public void run() { doStuff(); }
}).start();
Answer: DF

Question 115
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?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes and prints “running”.
D. The code executes and prints “runningrunning”.
E. The code executes and prints “runningrunningrunning”.
Answer: E

Question 116
Given:
1. public class Threads4 {
2. public static void main (String[] args) {
3. new Threads4().go();
4. }
5. public void go() {
6. Runnable r = new Runnable() {
7. public void run() {
8. System.out.print(”foo”);
9. }
10. };
11. Thread t = new Thread(r);
12. t.start();
13. t.start();
14. }
15. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints ‘foo”.
D. The code executes normally, but nothing is printed.
Answer: B

Question 117
Given:
1. public class Threads5 {
2. public static void main (String[] args) {
3. new Thread(new Runnable() {
4. public void run() {
5. System.out.print(”bar”);
6. }}).start();
7. }
8. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints “bar”.
D. The code executes normally, but nothing prints.
Answer: C

Question 118
Given:
11. Runnable r = new Runnable() {
12. public void run() {
13. System.out.print(”Cat”);
14. }
15. };
16. Threadt=new Thread(r) {
17. public void run() {
18. System.out.print(”Dog”);
19. }
20. };
21. t.start();
What is the result?
A. Cat
B. Dog
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.
Answer: B

Question 119
Click the Exhibit button.
Given:
10. public class Starter extends Thread {
11. private int x= 2;
12. public static void main(String[] args) throws Exception {
13. new Starter().makeItSo();
14. }
15. public Starter() {
16. x=5;
17. start();
18. }
19. public void makeItSo() throws Exception {
20. join();
21. x=x- 1;
22. System.out.println(x);
23. }
24. public void run() { x *= 2; }
25. }
What is the output if the main() method is rum?
A. 4
B. 5
C. 8
D. 9
E. Compilation fails.
F. An exception is thrown at runtime.
G. It is impossible to determine for certain.
Answer: D

Question 120
Given:
1. public class Threads2 implements Runnable {
2.
3. public void nun() {
4. System.out.println(”run.”);
5. throw new RuntimeException(”Problem”);
6. }
7. public static void main(String[] args) {
8. Thread t = new Thread(new Threads2());
9. t.start();
10. System.out.println(”End of method.”);
11. }
12. }
Which two can be results? (Choose two.)
A. java.lang.RuntimeException: Problem
B. run.
java.lang.RuntimeException: Problem
C. End of method.
java.lang.RuntimeException: Problem
D. End of method.
run.
java.lang.RuntimeException: Problem
E. run.
java.lang.RuntimeException: Problem
End of method.
Answer: DE

No comments:

Post a Comment