SCJP - Page -10

91.)
Given:

1. class A {
2. protected int method1(int a, int b) { return 0; }
3. }

Which two are valid in a class that extends class A? (Choose two.)

a. public int method1(int a, int b) { return 0; }
b. private int method1(int a, int b) { return 0; }
c. private int method1(int a, long b) { return 0;}
d. public short method1(int a, int b) { return 0; }
e. static protected int method1(int a, int b) { return 0; }
92.)
Given:

11. try {
12. int x = 0;
13. int y = 5 / x;
14. } catch (Exception e) {
15. System.out.println(“Exception”);
16. } catch (ArithmeticException ae) {
17. System.out.println(“Arithmetic Exception”);
18. }
19. System.out.println(“finished”);

What is the result?

a. finished
b. Exception
c. Compilation fails
d. Arithmetic Exception
93.)
Click the Exhibit button.

What is the result?

1. public class Test {
2. public static void aMethod() throws Exception {
3. try {
4. throw new Exception();
5. } finally {
6. System.out.println(“finally”);
7. }
8. }
9. public static void main(String args[]) {
10. try {
11. aMethod();
12. } catch (Exception e) {
13. System.out.println(“exception”);
14. }
15. System.out.println(“finished”);
16. }
17. }

a. finally
b. exception
c. finally
exception
finished
d. Compilation fails
94.)
Which statement is true?

a. If only thread is blocked in the wait method of an object, and another thread executes the
notify method on that same object, then the first thread immediately resumes execution.
b. If a thread is blocked in the wait method of an object, and another thread executes the
notify method on the same object, it is still possible that the first thread might never
resume execution.
c. If a thread is blocked in the wait method of an object, and another thread executes the notify
method on the same object, then the first thread resumes execution as a direct and sole
consequence of the notify call.
d. If two threads are blocked in the wait method of one object, and another thread executes the
notify method on the same object, then the thread that executed the wait call first resumes
execution as a direct and sole consequence of the notify call.
95.)
Click the Exhibit button.

What is the result?

1. public class SyncTest{
2. public static void main ( String[] args ) {
3. final StringBuffer s1 = new StringBuffer ();
4. final StringBuffer s2 = new StringBuffer ();
5. new Thread() {
6. public void run() {
7. synchronized(s1) {
8. s1.append(“A”);
9. synchronized(s2) {
10. s2.append(“B”);
11. System.out.print(s1);
12. System.out.print(s2);
13. }
14. }
15. }
16. } .start ();
17. new Thread() {
18. public void run () {
19. synchronized (s2) {
20. s2.append(“C”);
21. synchronized (s1) {
22. s1.append(“D”);
23. System.out.print(s2);
24. System.out.print(s1);
25. }
26. }
27. }
28. } .start ();
29. }
30. }

a. ABBCAD
b. CDDACB
c. ADCBADBC
d. Compilation fails
e. An exception is thrown at runtime.
f. The code runs with indeterminate output.
96.)
Given:

11. interface AnInterface { }
12. class AnAdapter0 {
13. AnAdapter0 () { }
14. }
15. class AnAdapter1 {
16. AnAdapter1(int i) { }
17. }

Which two build an anonymous inner class? (Choose two.)

a. AnAdapter1 aa = new AnAdapter1() { }
b. AnAdapter0 aa = new AnAdapter0 { }
c. AnAdapter0 aa = new AnAdapter0(5) { }
d. AnAdapter1 aa = new AnAdapter1(5) { }
e. AnInterface ai = new AnInterface (5) { }
97.)
Click the Exhibit button.

What is the result?

1. class A {
2. public byte getNumber () {
3. return 1;
4. }
5. }
6.
7. class B extends A {
8. public short getNumber () {
9. return 2;
10. }
11.
12. public static void main (String args[]) {
13. B b = new B ();
14. System.out.println(b.getNumber());
15. }
16. }

a. 1
b. 2
c. An exception is thrown at runtime.
d. Compilation fails because of an error in line 8.
e. Compilation fails because of an error in line 14.
98.)
Given:

1. public class Test {
2. public static void main (String args[]) {
3. System.out.println(6 ^ 3);
4. }
5. }

What is the result?

a. 2
b. 4
c. 5
d. 7
e. 216
f. Compilation fails
99.)
Click the Exhibit button.

What is the result?

1. public class Test {
2. private static int j = 0;
3.
4. public static boolean methodB (int k) {
5. j += k;
6. return true;
7. }
8.
9. public static void mehtodA (int i) {
10. boolean b;
11. b = i < 10 | methodB (4);
12. b = i < 10 | | methodB (8);
13. }
14.
15. public static void main (String args[]) {
16. methodA (0);
17. System.out.println(j);
18. }
19. }

a. 0
b. 4
c. 8
d. 12
e. Compilation fails
100.)
Click the Exhibit button.

Which statements at line 17 will ensure that j = 10 at line 19?

1. class A implements Runnable {
2. int i;
c. public void run() {
4. try {
5. Thread.sleep(5000);
6. i = 10;
7. } catch(InterruptedException e) {}
8. }
9. }
10.
11. public class Test {
12. public static void main (String args[]) {
13. try {
14. A a = new A();
15. Thread t = new Thread(a);
16. t.start();
17.
18. int j = a.i;
19.
20. } catch (Exception e) { }
21. }
22. }

a. a.wait();
b. t.wait();
c. t.join();
d. t.yield();
e. t.notify();
f. a.notify();
g. t.interrupt();