Serialization and java.io Ques: 01

 Serialization and java.io Ques: 02 

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. An instance of Forest and an instance of Tree are both serialized.


Answer: B

Collections and Generics Ques: 02

Collections and Generics Ques: 02

Click the Exhibit button



Which statement is true about the set variable on line 12?



A. The set variable contains all six elements from the coll collection, and the order is guaranteed to be
preserved.
B. The set variable contains only three elements from the coll collection, and the order is guaranteed to be preserved.
C. The set variable contains all six elements from the coll collection, but the order is NOT guaranteed to be preserved.
D. The set variable contains only three elements from the coll collection, but the order is NOT guaranteed to be preserved.

Answer: D

Collections and Generics Ques: 01

Collections and Generics Ques: 01

Given:

34. HashMap props = new HashMap();
35. props.put("key45", "some value");
36. props.put("key12", "some other value");
37. props.put("key39", "yet another value");
38. Set s = props.keySet();
39. // insert code here

What, inserted at line 39, will sort the keys in the props HashMap?
A. Arrays.sort(s);
B. s = new TreeSet(s);
C. Collections.sort(s);
D. s = new SortedSet(s);


Answer: B

Garbage Collection Ques: 03

Garbage Collection Ques: 03

Given:

11. rbo = new ReallyBigObject();
12. // more code here
13. rbo = null;
14. /* insert code here */

Which statement should be placed at line 14 to suggest that the virtual machine expend effort toward
recycling the memory used by the object rbo?
A. System.gc();
B. Runtime.gc();
C. System.freeMemory();
D. Runtime.getRuntime().growHeap();
E. Runtime.getRuntime().freeMemory();


Answer: A

Garbage Collection Ques: 02

Garbage Collection Ques: 02 

Given:

11. public void genNumbers() {
12. ArrayList numbers = new ArrayList();
13. for (int i=0; i<10 font="" i="">
14. int value = i * ((int) Math.random());
15. Integer intObj = new Integer(value);
16. numbers.add(intObj);
17. }
18. System.out.println(numbers);
19. }

Which line of code marks the earliest point that an object referenced by intObj becomes a candidate for
garbage collection?

A. Line 16
B. Line 17
C. Line 18
D. Line 19
E. The object is NOT a candidate for garbage collection.


Answer: D

Garbage Collection Ques: 01

Garbage Collection 
Ques: 01 Given:

1. public class GC {
2. private Object o;
3. private void doSomethingElse(Object obj) { o = obj; }
4. public void doSomething() {
5. Object o = new Object();
6. doSomethingElse(o);
7. o = new Object();
8. doSomethingElse(null);
9. o = null;
10. }
11.}


When the doSomething method is called, after which line does the Object created in line 5 become
available for garbage collection?
A. Line 5
B. Line 6
C. Line 7
D. Line 8
E. Line 9
F. Line 10

Answer: D