Thursday, September 25, 2008

SCJP 1.5 Dump 05





61. Given:

11. public static Iterator reverse(List list) {

12. Collections.reverse(list);

13. return list.iterator();

14. }

15. public static void main(String[] args) {

16. List list = new ArrayList();

17. list.add("1"); list.add("2"); list.add("3");

18. for (Object obj: reverse(list))

19. System.out.print(obj + ", ");

20. }

What is the result?

A. 3, 2, 1,

B. 1, 2, 3,

C. Compilation fails.

D. The code runs with no output.

E. An exception is thrown at runtime.

Answer: C

62. 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. A, B, C,

B. B, C, A,

C. Compilation fails.

D. The code runs with no output.

E. An exception is thrown at runtime.

Answer: B




63. Given:

11. public static void main(String[] args) {

12. for (int i = 0; i <= 10; i++) { 13. if (i > 6) break;

14. }

15. System.out.println(i);

16. }

What is the result?

A. 6

B. 7

C. 10

D. 11

E. Compilation fails.

F. An exception is thrown at runtime.

Answer: E

64. 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.)

A. java test

B. java -ea test

C. java test file1

D. java -ea test file1

E. java -ea test file1 file2

F. java -ea:test test file1

Answer: BE





65. Given:

12. public class AssertStuff {

13.

14. public static void main(String [] args) {

15. int x = 5;

16. int y = 7;

17.

18. assert (x > y): "stuff";

19. System.out.println("passed");

20. }

21. }

And these command line invocations:

java AssertStuff

java -ea AssertStuff

What is the result?

A. passed stuff

B. stuff passed

C. passed

An AssertionError is thrown with the word "stuff" added to the stack trace.

D. passed

An AssertionError is thrown without the word "stuff"added to the stack trace.

E. passed

An AssertionException is thrown with the word "stuff" added to the stack trace.

F. passed

An AssertionException is thrown without the word "stuff" added to the stack trace.

Answer: C





66. Click the Exhibit button.

1. public class Test { 2.

3. public static void main(String [] args) {

4. boolean assert = true;

5. if(assert) {

6. System.out.println("assert is true");

7. } 8. } 9. 10. }

Given:

javac -source 1.3 Test.java

What is the result?

A. Compilation fails.

B. Compilation succeeds with errors.

C. Compilation succeeds with warnings.

D. Compilation succeeds without warnings or errors.

Answer: C





67. Given:

23. int z = 5;

24.

25. public void stuff1(int x) {

26. assert (x > 0);

27. switch(x) {

28. case 2: x = 3;

29. default: assert false; } }

30.

31. private void stuff2(int y) { assert (y <>

32.

33. private void stuff3() { assert (stuff4()); }

34.

35. private boolean stuff4() { z = 6; return false; }

Which is true?

A. All of the assert statements are used appropriately.

B. Only the assert statement on line 31 is used appropriately.

C. The assert statements on lines 29 and 31 are used appropriately.

D. The assert statements on lines 26 and 29 are used appropriately.

E. The assert statements on lines 29 and 33 are used appropriately.

F. The assert statements on lines 29, 31, and 33 are used appropriately.

G. The assert statements on lines 26, 29, and 31 are used appropriately.

Answer: C





68. Click the Exhibit button.

SomeException:

1. public class SomeException {

2. }

Class A:

1. public class A {

2. public void doSomething() { }

3. }

Class B:

1. public class B extends A {

2. public void doSomething() throws SomeException { }

3. }

Which is true about the two classes?

A. Compilation of both classes will fail.

B. Compilation of both classes will succeed.

C. Compilation of class A will fail. Compilation of class B will succeed.

D. Compilation of class B will fail. Compilation of class A will succeed.

Answer: D





69. Click the Exhibit button.

Class TestException

1. public class TestException extends Exception {

2. }

Class A:

1. public class A {

2.

3. public String sayHello(String name) throws TestException {

4.

5. if(name == null) {

6. throw new TestException();

7. }

8.

9. return "Hello " + name;

10. }

11.

12. }

A programmer wants to use this code in an application:

45. A a = new A();

46. System.out.println(a.sayHello("John"));

Which two are true? (Choose two.)

A. Class A will not compile.

B. Line 46 can throw the unchecked exception TestException.

C. Line 45 can throw the unchecked exception TestException.

D. Line 46 will compile if the enclosing method throws a TestException.

E. Line 46 will compile if enclosed in a try block, where TestException is caught.

Answer: DE





70. Given:

33. try {

34. // some code here

35. } catch (NullPointerException e1) {

36. System.out.print("a");

37. } catch (RuntimeException e2) {

38. System.out.print("b");

39. } finally {

40. System.out.print("c");

41. }

What is the result if a NullPointerException occurs on line 34?

A. c

B. a

C. ab

D. ac

E. bc

F. abc

Answer: D

71. Given:

11. class A {

12. public void process() { System.out.print("A,"); }}

13. class B extends A {

14. public void process() throws IOException {

15. super.process();

16. System.out.print("B,");

17. throw new IOException();

18. }}

19. public static void main(String[] args) {

20. try { new B().process(); }

21. catch (IOException e) { System.out.println("Exception"); }}

What is the result?

A. Exception

B. A,B,Exception

C. Compilation fails because of an error in line 20.

D. Compilation fails because of an error in line 14.

E. A NullPointerException is thrown at runtime.

Answer: D






72. Given:

11. class A {

12. public void process() { System.out.print("A "); }}

13. class B extends A {

14. public void process() throws RuntimeException {

15. super.process();

16. if (true) throw new RuntimeException();

17. System.out.print("B "); }}

18. public static void main(String[] args) {

19. try { ((A)new B()).process(); }

20. catch (Exception e) { System.out.print("Exception "); }

21. }

What is the result?

A. Exception

B. A Exception

C. A Exception B

D. A B Exception

E. Compilation fails because of an error in line 14.

F. Compilation fails because of an error in line 19.

Answer: B




73. Given:

11. static class A {

12. void process() throws Exception { throw new Exception(); }

13. }

14. static class B extends A {

15. void process() { System.out.println("B "); }

16. }

17. public static void main(String[] args) {

18. A a = new B();

19. a.process();

20. }

What is the result?

A. B

B. The code runs with no output.

C. An exception is thrown at runtime.

D. Compilation fails because of an error in line 15.

E. Compilation fails because of an error in line 18.

F. Compilation fails because of an error in line 19.

Answer: F





74. Given:

11. static class A {

12. void process() throws Exception { throw new Exception(); }

13. }

14. static class B extends A {

15. void process() { System.out.println("B"); }

16. }

17. public static void main(String[] args) {

18. new B().process();

19. }

What is the result?

A. B

B. The code runs with no output.

C. Compilation fails because of an error in line 12.

D. Compilation fails because of an error in line 15.

E. Compilation fails because of an error in line 18.

Answer: A




75. Given:

84. try {

85. ResourceConnection con = resourceFactory.getConnection();

86. Results r = con.query("GET INFO FROM CUSTOMER");

87. info = r.getData();

88. con.close();

89. } catch (ResourceException re) {

90. errorLog.write(re.getMessage());

91. }

92. return info;

Which is true if a ResourceException is thrown on line 86?

A. Line 92 will not execute.

B. The connection will not be retrieved in line 85.

C. The resource connection will not be closed on line 88.

D. The enclosing method will throw an exception to its caller.

Answer: C

76. Click the Exhibit button.

1. public class A {

2. public void method1() {

3. B b = new B();

4. b.method2();

5. // more code here

6. }

7. }

1. public class B {

2. public void method2() {

3. C c = new C();

4. c.method3();

5. // more code here

6. }

7. }

1. public class C {

2. public void method3() {

3. // more code here

4. }

5. }

Given:

25. try {

26. A a = new A();

27. a.method1();

28. } catch (Exception e) {

29. System.out.print("an error occurred");

30. }

Which two are true if a NullPointerException is thrown on line 3 of class C? (Choose two.)

A. The application will crash.

B. The code on line 29 will be executed.

C. The code on line 5 of class A will execute.

D. The code on line 5 of class B will execute.

E. The exception will be propagated back to line 27.

Answer: BE





77. Click the Exhibit button.

1. public class A {

2. public void method1() {

3. try {

4. B b = new B();

5. b.method2();

6. // more code here

7. } catch (TestException te) {

8. throw new RuntimeException(te);

9. }

6. }

7. }

1. public class B {

2. public void method2() throws TestException {

3. // more code here

4. }

5. }

1. public class TestException extends Exception {

2. }

Given:

31. public void method() {

32. A a = new A();

33. a.method1();

34. }

Which is true if a TestException is thrown on line 3 of class B?

A. Line 33 must be called within a try block.

B. The exception thrown by method1 in class A is not required to be caught.

C. The method declared on line 31 must be declared to throw a RuntimeException.

D. On line 5 of class A, the call to method2 of class B does not need to be placed in a try/catch block.

Answer: B




78. Given:

11. public static void main(String[] args) {

12. try {

13. args = null;

14. args[0] = "test";

15. System.out.println(args[0]);

16. } catch (Exception ex) {

17. System.out.println("Exception");

18. } catch (NullPointerException npe) {

19. System.out.println("NullPointerException");

20. } 21. }

What is the result?

A. test

B. Exception

C. Compilation fails.

D. NullPointerException

Answer: C





79. Given:

11. static void test() throws Error {

12. if (true) throw new AssertionError();

13. System.out.print("test ");

14. }

15. public static void main(String[] args) {

16. try { test(); }

17. catch (Exception ex) { System.out.print("exception "); }

18. System.out.print("end ");

19. } What is the result?

A. end

B. Compilation fails. C. exception end

D. exception test end

E. A Throwable is thrown by main.

F. An Exception is thrown by main.

Answer: E

80. Given:

11. static void test() {

12. try {

13. String x = null;

14. System.out.print(x.toString() + " ");

15. }

16. finally { System.out.print("finally "); }

17. }

18. public static void main(String[] args) {

19. try { test(); }

20. catch (Exception ex) { System.out.print("exception "); }

21. }

What is the result?

A. null

B. finally

C. null finally

D. Compilation fails.

E. finally exception

Answer: E







No comments: