[ Pobierz całość w formacie PDF ]

8. B and C. The result range forrandom()is 0.0 to
A, D, E, and F are incorrect based on the logic above.
9. C. Thesqrt()method returnsNaN(not a number) when it s argument is less than zero.
A, B, D, and E are incorrect based on the logic described above.
10. C. The Math class trigonometry methods expect their arguments to be in radians, not
degrees. Line 5 can be decoded:  Convert 75 (degrees) into radians, then find the sine of
that result.
A, B, D, E, and F are incorrect based on the logic described above.
Wrappers (Exam Objective 8.3)
11. B and D. B is incorrect because thevalueOf()method returns an Integer object. D is
incorrect because theparseInt()method takes a String.
A, C, E, and F all represent valid syntax. Line 5 takes the String  345 to be octal number,
and converts it to an integer value 229.
12. C. All of this code is legal, and line 5 creates a new String with a value of  42.5 . Lines 6
and 7 convert the String to a double and then back again. Line 8 is fun Math.ceil() s
argument expression is evaluated first. We invoke thevalueOf()method that returns an
anonymous Double object (with a value of 42.5). Then thedoubleValue()method is
called (invoked on the newly created Double object), and returns a double primitive (there and
back again), with a value of (you guessed it) 42.5. Theceil()method converts this to 43.0,
which is cast to an int and assigned to x. We know, we know, but stuff like this is on the exam.
A, B, D, E, and F are incorrect based on the logic described above.
13. E. The compiler fails at line 10 because b1 is a reference variable to a Boolean wrapper
object, not a boolean primitive. Logical boolean tests can t be made on Boolean objects.
A, B, C, D, and F are incorrect based on the logic described above.
14. A, B, and D. A won t compile because thefloatValue()method is an instance
method that takes no arguments. B won t compile because thevalueOf()method returns
a wrapper object. D won t compile because theparseFloat()method takes a String.
C, E, and F are all legal (if not terribly useful) ways to return a primitive float.
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
52
15. A is correct. ThexxxValue()methods convert any numeric wrapper object s value to
any primitive type. When narrowing is necessary, significant bits are dropped and the results
are difficult to calculate.
B, C, D, E, and F are incorrect based on the logic described above.
Equals() (Exam Objective 5.2)
16. B. Line 12 fails because==compares reference values, not object values. Line 13 succeeds
because both String and primitive wrapper constructors resolve to the same value (except for
the Character wrapper). Lines 14, 15, and 16 fail because theequals()method fails if the
object classes being compared are different and not in the same tree hierarchy.
A, C, D, E, and F are incorrect based on the logic described above.
17. F. Line 10 fails because b1 and b2 are two different objects. Lines 11 and 14 succeed
because the Boolean String constructors are case insensitive. Lines 12 and 13 fail because
trueis not equal tofalse.
A, B, C, D, and E are incorrect based on the logic described above.
18. E. Even though o and oc are reference variables of different types, they are both referring
to the same object. This means that==will resolve totrueand that the defaultequals()
method will also resolve totrue.
A, B, C, and D are incorrect based on the logic described above.
19. D and E. D describes an example of theequals()method behaving transitively. By
the way, x, y, and z will all be the same type of wrapper. E is true because x and y are referring
to the same String object.
A is incorrect the fragment will compile. B is incorrect because x==y means that the
two reference variables are referring to the same object. C will only be true if x and y refer
to the same String. It is possible for x and y to refer to two different String objects with the
same value.
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
Exercise Answers
53
EXERCISE ANSWERS
Exercise 6-1: Using the Math Class
The following code listing is an example of how you might have written code to
complete the exercise:
class NumberInterrogation {
public static void main(String [] argh) {
double [] num = {10.5, -10.5, Math.PI, 0};
for(int i=0;i
System.out.println("abs("+num[i]+")="+Math.abs(num[i]));
System.out.println("ceil("+num[i]+")="+Math.ceil(num[i]));
System.out.println("floor("+num[i]+")="+Math.floor(num[i]));
System.out.println();
}
}
}
Exercise 6-2: Rounding Random Numbers
The following code listing is an example of how you might have written code to
complete the exercise:
class RandomRound {
public static void main(String [] argh) {
for(int i=0;i
double num = Math.random() * 100;
System.out.print("The number " + num);
System.out.println(" rounds to " + Math.round(num));
}
}
} [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • qus.htw.pl