Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Tuesday, September 22, 2009

Java Quiz -17

Hi friends,

Today I would like to post few more questions to test your capability.
Dont worry guys, I will give you the answer too! Try to answer it yourself without referring to the answers to test yourself.

Q: 1 Given a valid DateFormat object named df, and
1. Date data1 = new Date(0L);
2. String data2 = "September 22, 2009";
3. // insert code here
What updates data1's value with the date represented by data2?

A. 3. data1 = df.parse(data2);
B. 3. data1 = df.getDate(data2);
C. 3. try {
4. data1 = df.parse(data2);
5. } catch(ParseException e) { };
D. 3. try {
4. data1 = df.getDate(data2);
5. } catch(ParseException e) { };

Answer: C

Q: 2 Given:
1. public class ClassA {
2. private StringBuilder testStringBuilder = new StringBuuilder();
3. public void logTest(String data1, String data2) {
4. testStringBuilder.append(data1);
5. testStringBuilder.append(data2);
6. }
7. }
The programmer must guarantee that a single ClassA object works properly for a multi-threaded system. How must this code be changed to be thread-safe?

A. Synchronize the logTest method
B. No change is necessary, the current ClassA code is already thread-safe.
C. replace StringBuilder with just a String object and use the string concatenation (+=) within the logTest method
D. replace StringBuilder with StringBuffer

Answer: A

Q: 3 Given:
1 . import java.io.*;
2. public class ClassA implements Serializable {
3. private ClassB tree = new ClassB();
4. public static void main(String [] args) {
5. ClassA classA = new ClassA();
6. try {
7. FileOutputStream fileOutputStream = new FileOutputStream("ClassA.ser");
8. ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
9. objectOutputStream.writeObject(classA);
10.objectOutputStream.close();
11. } catch (Exception ex) { ex.printStackTrace(); }
12. } }
13.
14. class ClassB { }
What is the result?

A. An exception is thrown at runtime.
B. Compilation fails.
C. An instance of ClassA is serialized.
D. An instance of ClassA and an instance of ClassB are both serialized.

Answer: A

Q: 4 Assuming that the serializeClassC() and the deserializeClassC() methods
will correctly use Java serialization and given:
1. import java.io.*;
2. class ClassA {ClassA() { System.out.print("1"); } }
3. class ClassB extends ClassA implements Serializable {
4. ClassB() { System.out.print("2"); } }
5. public class ClassC extends ClassB { int size = 42;
6. public static void main(String [] args) {
7. ClassC b = new ClassC();
8. b.serializeClassC(b); // assume correct serialization
9. b = b.deserializeClassC(b); // assume correct
10. System.out.println(" restored " + b.size + " "); }
11. // more ClassC methods
12. }
What is the result?

A. Compilation fails.
B. 1 restored 42
C. 12 restored 42
D. 121 restored 42
E. 1212 restored 42
F. An exception is thrown at runtime.

Answer: D

Q: 5 Given this method in a class:
1. public String toString() {
2. StringBuffer testStringBuffer = new StringBuffer();
3. testStringBuffer.append('<');
4. testStringBuffer.append(this.name);
5. testStringBuffer.append('>');
6. return testStringBuffer.toString();
7. }
Which statement is true?

A. This code will perform well and converting the code to use StringBuilder will not enhance the performance.
B. This code will perform poorly. For better performance, the code should be rewritten:
return "<" + this.name + ">";
C. The programmer can replace StringBuffer with StringBuilder with no other changes.
D. This code is NOT thread-safe.

Answer: C

Q: 6 Given:
1. double data = 314159.26;
2. NumberFormat nf = NumberFormat.getInstance(Locale.ITALIAN);
3. String b;
4. //insert code here
Which code, inserted at line 4, sets the value of b to 314.159,26?

A. b = nf.parse( data );
B. b = nf.format( data );
C. b = nf.equals( data );
D. b = nf.parseObject( data );

Answer: B

Q: 7 Given:
System.out.format("Pi is approximately %d.", Math.PI);
What is the result?

A. Pi is approximately 3.141593.
B. Pi is approximately 3.
C. Compilation fails.
D. An exception is thrown at runtime.

Answer: D

Q: 8 Given:
1. public class ClassA implements Comparable {
2. private int data1, data2;
3. public ClassA(int data3, int data4) { data1 = data3; data2 = data4; }
4. public int getTestdata1() { return data1; }
5. public int getTestdata2() { return data2; }
6. public String toString() {
7. return "<" + data1 + "," + data2 + ">";
8. }
9. // insert code here
10. }
Which method will complete this class?

A. public int compareTo(Object object1){/*more code here*/}
B. public int compareTo(ClassA classA){/*more code here*/}
C. public int compare(ClassA classA1,ClassA classA2){/*more code here*/}
D. public int compare(Object object1,Object object2){/*more code here*/}

Answer: B

Q: 9 Given:
1. public class ClassA {
2. private String data1;
3. public ClassA(String data1) {
4. this.data1 = data1;
5. }
6. public boolean equals(Object o) {
7. if ( ! o instanceof ClassA ) return false;
8. ClassA p = (ClassA) o;
9. return p.data1.equals(this.data1);
10. }
11. }
Which statement is true?

A. A HashSet could contain multiple ClassA objects with the same data1.
B. Compilation fails because the hashCode method is not overridden.
C. All ClassA objects will have the same hash code because the hashCode method is not overridden.

Answer: A

Q: 10 Given:
13. public static void search(List listObject) {
14. listObject.clear();
15. listObject.add("data2");
16. listObject.add("data1");
17. listObject.add("data3");
18. System.out.println(Collections.binarySearch(listObject, "data1"));
19. }
What is the result of calling search with a valid List implementation?

A. 0
B. 1
C. 2
D. data1
E. data2
F. data3
G. The result cannot be defined.

Answer: G

Hope you found this useful! Kindly give me your comments regarding the same.Many more Quizzes are to be posted! Kindly make use of them as much as possible!

No comments: