java

java

Wednesday 14 December 2011

SCJP Questions 91-100

Question 91
Given:
11. public String makinStrings() {
12. String s = “Fred”;
13. s = s + “47”;
14. s = s.substring(2, 5);
15. s = s.toUpperCase();
16. return s.toString();
17. }
How many String objects will be created when this method is invoked?
A. 1
B. 2
C. 3
D. 4
E. 5
F. 6
Answer: C

Question 92
Given:
1. public class TestString3 {
2. public static void main(String[] args) {
3. // insert code here
5. System.out.println(s);
6. }
7. }
Which two code fragments, inserted independently at line 3, generate
the output 4247? (Choose two.)
A. String s = “123456789”;
s = (s-”123”).replace(1,3,”24”) - “89”;
B. StringBuffer s = new StringBuffer(”123456789”);
s.delete(0,3).replace( 1,3, “24”).delete(4,6);
C. StringBuffer s = new StringBuffer(”123456789”);
s.substring(3,6).delete( 1 ,3).insert( 1, “24”);
D. StringBuilder s = new StringBuilder(”123456789”);
s.substring(3,6).delete( 1 ,2).insert( 1, “24”);
E. StringBuilder s = new StringBuilder(”123456789”);
s.delete(0,3).delete( 1 ,3).delete(2,5).insert( 1, “24”);
Answer: BE

Question 93
Given:
11. public class Yikes {
12.
13. public static void go(Long n) {System.out.println(”Long “);}
14. public static void go(Short n) {System.out.println(”Short “);}
15. public static void go(int n) {System.out.println(”int “);}
16. public static void main(String [] args) {
17. short y= 6;
18. long z= 7;
19. go(y);
20. go(z);
21. }
22. }
What is the result?
A. int Long
B. Short Long
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: A

Question 94
Given:
12. public class Wow {
13. public static void go(short n) {System.out.println(”short”); }
14. public static void go(Short n) {System.out.println(”SHORT”);}
15. public static void go(Long n) {System.out.println(” LONG”); }
16. public static void main(String [] args) {
17. Short y= 6;
18.int z=7;
19. go(y);
20. go(z);
21. }
22. }
What is the result?
A. short LONG
B. SHORT LONG
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: C

Question 95
Given:
10. class MakeFile {
11. public static void main(String[] args) {
12. try {
13. File directory = new File(”d”);
14. File file = new File(directory,”f”);
15. if(!file.exists()) {
16. file.createNewFile();
17. }
18. } catch (IOException e) {
19. e.printStackTrace
20. }
21. }
22. }
The current directory does NOT contain a directory named “d.”
Which three are true? (Choose three.)
A. Line 16 is never executed.
B. An exception is thrown at runtime.
C. Line 13 creates a File object named “d.”
D. Line 14 creates a File object named “f.’
E. Line 13 creates a directory named “d” in the file system.
F. Line 16 creates a directory named “d” and a file ‘f’ within it in the
file system.
G. Line 14 creates a file named ‘f’ inside of the directory named “d” in
the file system.
Answer: BCD

Question 96
When comparing java.io.BufferedWriter to java.io.FileWriter, which
capability exists as a method in only one of the two?
A. closing the stream
B. flushing the stream
C. writing to the stream
D. marking a location in the stream
E. writing a line separator to the stream
Answer: E

Question 97
Given:
12. import java.io.*;
13. public class Forest implements Serializable {
14. private Tree tree = new Tree();
15. public static void main(String [] args) {
16. Forest f= new Forest();
17. try {
18. FileOutputStream fs = new FileOutputStream(”Forest.ser”);
19. ObjectOutputStream os = new ObjectOutputStream(fs);
20. os.writeObject(f); os.close();
21. } catch (Exception ex) { ex.printStackTrace(); }
22. } }
23.
24. class Tree { }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. An instance of Forest is serialized.
D. A instance of Forest and an instance of Tree are both serialized.
Answer: B

Question 98
Click the Exhibit button.
1. import java.io.*;
2. public class Foo implements Serializable {
3. public int x, y;
4. public Foo( int x, int y) { this.x = x; this.y = y; }
5.
6. private void writeObject( ObjectOutputStream s)
7. throws IOException {
8. s.writeInt(x); s.writeInt(y)
9. }
10.
11. private void readObject( ObjectInputStream s)
12. throws IOException, ClassNotFoundException {
13.
14. // insert code here
15.
16. }
17. }
Which code, inserted at line 14, will allow this class to correctly
serialize and deserialize?
A. s.defaultReadObject();
B. this = s.defaultReadObject();
C. y = s.readInt(); x = s.readInt();
D. x = s.readInt(); y = s.readInt();
Answer: D

Question 99
Which three concerning the use of the java.io.Serializable interface are
true? (Choose three.)
A. Objects from classes that use aggregation cannot be serialized.
B. Art object serialized on one JVM can be successfully deserialized on
a different JVM.
C. The values in fields with the volatile modifier will NOT survive
serialization and deserialization.
D. The values in fields with the transient modifier will NOT survive
serialization and deserialization.
E. It is legal to serialize an object of a type that has a supertype that
does NOT implement java.io.Serializable.
Answer: BDE

Question 100
Assuming that the serializeBanana() and the deserializeBanana()
methods will correctly use Java serialization and given:
13. import java.io.*;
14. class Food implemertts Serializable {int good = 3;}
15. class Fruit externds Food {int juice = 5;}
16. public class Banana extends Fruit {
17. int yellow = 4;
18. public static void main(String [] args) {
19. Banana b = new Banana(); Banana b2 = new Banana();
20. b.serializeBanana(b); // assume correct serialization
21. b2 = b.deserializeBanana(); // assume correct
22. System.out.println(”restore “+b2.yellow+ b2.juice+b2.good);
24. }
25. // more Banana methods go here
50. }
‘What is the result?
A. restore 400
B. restore 403
C. restore 453
D. Compilation fails.
E. An exception is thrown at runtime.
Answer: C

No comments:

Post a Comment