Tuesday 6 September 2011

SCJP Questions 101-110

                                   PREVIOUS                       NEXT

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

Question 101
Assuming that the serializeBanana2() and the deserializeBanana2()
methods will correctly use Java serialization and given:
13. import java.io.*;
14. class Food {Food() { System.out.print(”1”); } }
15. class Fruit extends Food implements Serializable {
16. Fruit() { System.out.print(”2”); } }
17. public class Banana2 extends Fruit { int size = 42;
18. public static void main(String [] args) {
19. Banana2 b = new Banana2();
20. b.serializeBanana2(b); // assume correct serialization
21. b = b.deserializeBanana2(b); // assume correct
22. System.out.println(” restored “+ b.size + “ “); }
23. // more Banana2 methods
24. }
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

Question 102
Given:
10. public class Foo implements java.io.Serializable {
11. private int x;
12. public int getX() { return x; }
12.publicFoo(int x){this.x=x; }
13. private void writeObject( ObjectOutputStream s)
14. throws IOException {
15. // insert code here
16. }
17. }
Which code fragment, inserted at line 15, will allow Foo objects to be
correctly serialized and deserialized?
A. s.writeInt(x);
B. s.serialize(x);
C. s.writeObject(x);
D. s.defaultWriteObject();
Answer: D

Question 103
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 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: CF

Question 104
Given:
11. double input = 314159.26;
12. NumberFormat nf= NumberFormat.getInstance(Locale.ITALIAN);
13. String b;
14. //insert code here
Which code, inserted at line 14, sets the value of b to 3 14.159,26?
A. b = nf.parse( input);
B. b = nf.format( input);
C. b = nf.equals( input);
D. b = nf.parseObject( input);
Answer: B

Question 105
Given:
14. DateFormat df;
15. Date date = new Date();
16. //insert code here
17. String s = df.format( date);
Which two, inserted independently at line 16, allow the code to
compile? (Choose two.)
A. df= new DateFormat();
B. df= Date.getFormatter();
C. df= date.getFormatter();
D. df= date.getDateFormatter();
E. df= Date.getDateFormatter();
F. df= DateFormat.getInstance();
G. df = DateFormat.getDateInstance();
Answer: FG

Question 106
Given:
12. Date date = new Date();
13. df.setLocale(Locale.ITALY);
14. String s = df.format(date);
The variable df is an object of type DateFormat that has been
initialized in line 11. What is the result if this code is run on December
14, 2000?
A. The value of s is 14-dic-2004.
B. The value of s is Dec 14, 2000.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 13.
Answer: D

Question 107
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
Which will create the appropriate DateFormat object and add 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.getDateJnstance();
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.getDateJnstance();
42. d.setLocalTime( (60 * 60 * 24) + d.getLocalTime());
Answer: B

Question 108
Given a valid DateFormat object named df, and
16. Date d = new Date(0L);
17. String ds = “December 15, 2004”;
18. // insert code here
What updates d’s value with the date represented by ds?
A. 18. d = df.parse(ds);
B. 18. d = df.getDate(ds);
C. 18. try {
19. d = df.parse(ds);
20. } catch(ParseException e) { };
D. 18. try {
19. d = df.getDate(ds);
20. } catch(ParseException e) { };
Answer: C

Question 109
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

Question 110
Given:
11. String test= “a1b2c3”;
12. String[] tokens = test.split(”\\d”);
13. for(String s: tokens) System.out.print(s +“ “);
What is the result?
A. a b c
B. 1 2 3
C. a1b2c3
D. a1 b2 c3
E. Compilation fails.
F. The code runs with no output.
G. An exception is thrown at runtime.
Answer: A

                                  PREVIOUS                       NEXT

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...