Tuesday 6 September 2011

SCJP Questions 61-70


                                 PREVIOUS                       NEXT

Question 61
Give:
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

Question 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

Question 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

Question 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

Question 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

Question 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

Question 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 < 0); }
32.
33. private void stuff3() { assert (stuff4O); }
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

Question 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

Question 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

Question 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

                                    PREVIOUS                       NEXT

1 comment:

  1. excellent but very tough a set of questions

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...