Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Saturday, August 15, 2009

Java Quiz -4

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 Click the Task button.
Answer: Check TestNows eEngine, Download from Member Center
www.TestNows.com Q: 43 Given:
11. String test = "This is a test";
12. String[] tokens = test.split("\s");
13. System.out.println(tokens.length);
What is the result?

A. 0
B. 1
C. 4
D. Compilation fails.
E. An exception is thrown at runtime.

Answer: D

Q: 2 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

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

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

Answer: D

Q: 4 Given:
33. Date d = new Date(0);
34. String ds = "December 15, 2004";
35. // insert code here
36. try {
37. d = df.parse(ds);
38. }
39. catch(ParseException e) {
40. System.out.println("Unable to parse " + ds);
41. }
42. // insert code here too
What creates the appropriate DateFormat object and adds a day to the Date object?

A. 35. DateFormat df = DateFormat.getDateFormat();
42. d.setTime( (60 * 60 * 24) + d.getTime());
B. 35. DateFormat df = DateFormat.getDateInstance();
42. d.setTime( (1000 * 60 * 60 * 24) + d.getTime());
C. 35. DateFormat df = DateFormat.getDateFormat();
42. d.setLocalTime( (1000*60*60*24) + d.getLocalTime());
D. 35. DateFormat df = DateFormat.getDateInstance();
42. d.setLocalTime( (60 * 60 * 24) + d.getLocalTime());

Answer: B

Q: 5 Given:
12. NumberFormat nf = NumberFormat.getInstance();
13. nf.setMaximumFractionDigits(4);
14. nf.setMinimumFractionDigits(2);
15. String a = nf.format(3.1415926);
16. String b = nf.format(2);
Which two statements are true about the result if the default locale is Locale.US? (Choose two.)

A. The value of b is 2.
B. The value of a is 3.14.
C. The value of b is 2.00.
D. The value of a is 3.141.
E. The value of a is 3.1415.
F. The value of a is 3.1416.
G. The value of b is 2.0000.

Answer: C, F

Q: 6 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

Q: 7 Assuming that the serializeBanana() and the deserializeBanana() methods will
correctly use Java serialization and given:
13. import java.io.*;
14. class Food implements Serializable {int good = 3;}
15. class Fruit extends 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

Q: 8 Given this method in a class:
21. public String toString() {
22. StringBuffer buffer = new StringBuffer();
23. buffer.append('<');
24. buffer.append(this.name);
25. buffer.append('>');
26. return buffer.toString();
27. }
Which statement is true?

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

Answer: B

Q: 9 Given:
1. package geometry;
2. public class Hypotenuse {
3. public InnerTriangle it = new InnerTriangle();
4. class InnerTriangle {
5. public int base;
6. public int height;
7. }
8. }
Which statement is true about the class of an object that can reference the variable base?

A. It can be any class.
B. No class has access to base.
C. The class must belong to the geometry package.
D. The class must be a subclass of the class Hypotenuse.

Answer: C

Q: 10 Which two statements are true about has-a and is-a relationships? (Choose
two.)

A. Inheritance represents an is-a relationship.
B. Inheritance represents a has-a relationship.
C. Interfaces must be used when creating a has-a relationship.
D. Instance variables can be used when creating a has-a relationship.

Answer: A, D

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: