[ Pobierz całość w formacie PDF ]

7. int x4 = Integer.parseInt("42");
8. int x5 = b.intValue();
9. }
10. }
which two lines will cause compiler errors? (Choose two.)
A. Line 3
B. Line 4
C. Line 5
D. Line 6
E. Line 7
F. Line 8
12. Given the following,
1. public class NFE {
2. public static void main(String [] args) {
3. String s = "42";
4. try {
5. s = s.concat(".5");
6. double d = Double.parseDouble(s);
7. s = Double.toString(d);
8. int x = (int) Math.ceil(Double.valueOf(s).doubleValue());
9. System.out.println(x);
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 6
Composite Default screen
Self Test
45
10. }
11. catch (NumberFormatException e) {
12. System.out.println("bad number");
13. }
14. }
15. }
what is the result?
A. 42
B. 42.5
C. 43
D. badnumber
E. Compilation fails.
F. An uncaught exception is thrown at runtime.
13. Given the following,
1. public class BoolTest {
2. public static void main(String [] args) {
3. Boolean b1 = new Boolean("false");
4. boolean b2;
5. b2 = b1.booleanValue();
6. if (!b2) {
7. b2 = true;
8. System.out.print("x ");
9. }
10. if (b1 & b2) {
11. System.out.print("y ");
12. }
13. System.out.println("z");
14. }
15. }
what is the result?
A. z
B. xz
C. yz
D. xyz
E. Compilation fails.
F. An exception is thrown at runtime.
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 6
Composite Default screen
Chapter 6: Java.lang The Math Class, Strings, and Wrappers
46
14. Given the following,
1. public class WrapTest3 {
2. public static void main(String [] args) {
3. String s = "98.6";
4. // insert code here
5. }
6. }
which three lines inserted independently at line 4 will cause compiler errors? (Choose three.)
A. floatf1=Float.floatValue(s);
B. floatf2=Float.valueOf(s);
C. floatf3=newFloat(3.14f).floatValue();
D. floatf4=Float.parseFloat(1.23f);
E. floatf5=Float.valueOf(s).floatValue();
F. floatf6=(float)Double.parseDouble("3.14");
15. Given the following,
11. try {
12. Float f1 = new Float("3.0");
13. int x = f1.intValue();
14. byte b = f1.byteValue();
15. double d = f1.doubleValue();
16. System.out.println(x + b + d);
17. }
18. catch (NumberFormatException e) {
19. System.out.println("bad number");
20. }
what is the result?
A. 9.0
B. badnumber
C. Compilation fails on line 13.
D. Compilation fails on line 14.
E. Compilation fails on lines 13 and 14.
F. An uncaught exception is thrown at runtime.
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 6
Composite Default screen
Self Test
47
Using equals() (Exam Objective 5.2)
16. Given the following,
1. public class WrapTest {
2. public static void main(String [] args) {
3. int result = 0;
4. short s = 42;
5. Long x = new Long("42");
6. Long y = new Long(42);
7. Short z = new Short("42");
8. Short x2 = new Short(s);
9. Integer y2 = new Integer("42");
10. Integer z2 = new Integer(42);
11.
12. if (x == y) result = 1;
13. if (x.equals(y) ) result = result + 10;
14. if (x.equals(z) ) result = result + 100;
15. if (x.equals(x2) ) result = result + 1000;
16. if (x.equals(z2) ) result = result + 10000;
17.
18. System.out.println("result = " + result);
19. }
20. }
what is the result?
A. result=1
B. result=10
C. result=11
D. result=11010
E. result=11011
F. result=11111
17. Given the following,
1. public class BoolTest {
2. public static void main(String [] args) {
3. int result = 0;
4.
5. Boolean b1 = new Boolean("TRUE");
6. Boolean b2 = new Boolean("true");
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 6
Composite Default screen
Chapter 6: Java.lang The Math Class, Strings, and Wrappers
48
7. Boolean b3 = new Boolean("tRuE");
8. Boolean b4 = new Boolean("false");
9.
10. if (b1 == b2) result = 1;
11. if (b1.equals(b2) ) result = result + 10;
12. if (b2 == b4) result = result + 100;
13. if (b2.equals(b4) ) result = result + 1000;
14. if (b2.equals(b3) ) result = result + 10000;
15.
16. System.out.println("result = " + result);
17. }
18. }
what is the result?
A. 0
B. 1
C. 10
D. 1100
E. 10001
F. 10010
18. Given the following,
1. public class ObjComp {
2. public static void main(String [] args ) {
3. int result = 0;
4. ObjComp oc = new ObjComp();
5. Object o = oc;
6.
7. if (o == oc) result = 1;
8. if (o != oc) result = result + 10;
9. if (o.equals(oc) ) result = result + 100;
10. if (oc.equals(o) ) result = result + 1000;
11.
12. System.out.println("result = " + result);
13. }
14. }
what is the result?
A. 1
B. 10
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 6
Composite Default screen
Self Test
49
C. 101
D. 1001
E. 1101
19. Which two statements are true about wrapper or String classes? (Choose two.)
A. If x and y refer to instances of different wrapper classes, then the fragmentx.equals(y)
will cause a compiler failure.
B. If x and y refer to instances of different wrapper classes, then x==y can sometimes be
true.
C. If x and y are String references and ifx.equals(y)istrue, then x==y istrue.
D. If x, y, and z refer to instances of wrapper classes andx.equals(y)istrue, and
y.equals(z)istrue, thenz.equals(x)will always betrue.
E. If x and y are String references and x==y istrue, theny.equals(x)will betrue.
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 6
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 6
Composite Default screen
Chapter 6: Java.lang The Math Class, Strings, and Wrappers
50
SELF TEST ANSWERS
Strings (Exam Objective 8.2)
1. C. After line 5 executes, both s2 and s3 refer to a String object that contains the value
 def . When line 6 executes, a new String object is created with the value  ghi , to which s2
refers. The reference variable s3 still refers to the (immutable) String object with the value
 def .
A, B, D, E, and F are incorrect based on the logic described above.
2. C. Line 12 creates a new String object with the value  XYZ , but this new object is
immediately lost because there is no reference to it. Line 13 creates a new String object
referenced by y. This new String object has the value  xyz because there was no  Y in the
String object referred to by x. Line 14 creates a new String object, appends  abc to the value
 xyz , and refers y to the result.
A, B, D, E, and F are incorrect based on the logic described above.
3. C. Line 13 creates two, one referred to by x and the lost String  xyz . Line 14 creates one
(for a total of three). Line 15 creates one more (for a total of four), the concatenated String
referred to by x with a value of  xyzabc .
A, B, and D are incorrect based on the logic described above.
4. B. Bothsubstring()andcharAt()methods are indexed with a zero-base, and
substring()returns a String of length arg2  arg1.
A, C, D, E, and F are incorrect based on the logic described above.
5. E. In line 7 the code calls a StringBuffer method,append()on a String object.
A, B, C, D, and F are incorrect based on the logic described above.
Math (Exam Objective 8.1)
6. B.Math.round()adds .5 to the argument then performs afloor(). Since the code
adds an additional .5 beforeround()is called, it s as if we are adding 1 then doing a
floor(). The values that start out as integer values will in effect be incremented by 1 on the
round()side but not on theceil()side, and the noninteger values will end up equal.
A, C, D, and E are incorrect based on the logic described above.
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 6
Composite Default screen
Self Test Answers
51
7. A, B, and D. Themax()method is overloaded to take two arguments of type int, long,
float, or double. [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • fotocafe.htw.pl
  •  
     
    nclude("menu4/4.php") ?>