SCJP - Page -7


61.)
Given:

1. class Bar { }

1. class Test {
2. Bar doBar() {
3. Bar b = new Bar();
4. return b;
5.}
6. public static void main (String args[]) {
7. Test t = new Test();
8. Bar newBar = t.doBar();
9. System.out.println (“newBar”);
10. newBar = newBar();
11. System.out.println (“finishing”);
12. }
13. }

At what point is the Bar object, created on line 3, eligible for garbase collection?

a. after line 8
b. after line 10
c. after line 4, when doBar() completes
d. after line 11, when main() completes
62.)
Click the Exhibit button.

What is the result?

1. public class Foo {
2. public static void main (String[] args) {
3. StringBuffer a = new StringBuffer (“A”);
4. StringBuffer b = new StringBuffer (“B”);
5. operate (a, b);
6. System.out.println(a + “,” + b);
7. }
8. static void operate (StringBuffer x, StringBuffer y) {
9. x.append(y);
10. y = x;
11. }
12. }

a. A,B
b. A,A
c. B,B
d. AB,B
e. AB,AB
f. Compilation fails.
63.)
Given:

21. int i =1;
22. int j = i++;
23. if (( i == ++j ) | ( i++ == j )) {
24. i += j;
25. }
26. System.out.println (“i = “ + i);

What is the result?

a. i = 1
b. i = 2
c. i = 3
d. i = 4
e. i = 5
f. Compilation fails
64.)
Given:

11. int index = 1;
12. int[] foo = new int[3];
13. int bar = foo[index];
14. int baz = bar + index;
15. System.out.println(“ baz = “ + baz);

What is the result?

a. baz = 0
b. baz = 1
c. baz = 2
d. Compilation fails.
e. An exception is thrown at runtime.
65.)
Given:

11. for (int i = 0; i < 3; i++) {
12. switch(i) {
13. case 0: break;
14. case 1: System.out.print(“one”);
15. case 2: System.out.print(“two”);
16. case 3: System.out.print(“three”);
17. }
18. }
19. System.out.println(“done”);

What is the result?

a. done
b. one two done
c. Compilation fails.
d. one two three done
e. one two three two three done
66.)
Click the Exahibit button.

What is the output?

1. public class Test {
2. public static void StringReplace (String text) {
3. text = text.replace ('j', 'c');
4. }
5.
6. public static void bufferReplace (StringBuffer text) {
7. text = text.append (“c”);
8. }
9.
10. public stativ void main (String args[]) {
11. String textString = new String(“java”);
12. StringBuffer textBuffer = new StringBuffer (“java”);
13.
14. stringReplace(textString);
15. bufferReplace(textBuffer);
16.
17. System.out.println(textString + textBuffer);
18. }
19. }

a. JAVAJAVAC
67.)
Click the Exhibit button.

What is the result?

1. public class Test {
2. public static void add3 (Integer i) {
3. int val = i.intValue();
4. val += 3;
5. i = new Integer (val);
6. }
7.
8. public static void main (String args[]) {
9. Interger i = new Integer (0);
10. add3 (i);
11. System.out.println(i.intValue());
12. }
13. }

a. 0
b. 3
c. Compilation fails.
d. An exception is thrown at runtime.
68.)
Given:

1. class Alpha {
2. private static String[] names;
3.
4. public static void main (String[] args) {
5. System.out.println( names[0]);
6. }
7. }

What is the result?

a. null
b. Compilation fails.
c. An exception is thrown at runtime.
d. The code runs with indeterminate output.
69.)
Which statement is true about assertions in the Java Programming language?

a. Assertion expressions should not contain side effects.
b. Assertion expression values can be any primitive type.
c. Assertions should be used for enforcing preconditions on public methods.
d. An AssertionError thrown as a result of a failed assertion should always be handled by the
enclosing method.
70.)
Click the Exhibit button.

What is the result?

1. class A implements Runnable {
2. public int i = 1;
3. public void run() {
4. this.i = 10;
5. }
6. }
7.
8. public class Test {
9. public static void main (String args[]) {
10. A a = new A();
11. new Thread(a).start();
12. int j = a.i;
13. System.out.println(“j = “ + j);
14. }
15. }

a. j = 1
b. j = 10
c. Compilation fails.
d. The value of j cannot be determined.