10 Fundamental Classes

10.1

(b)

The method hashCode() in the Object class returns a hash code value of type int.

10.2

(e)

All arrays are genuine objects and inherit all the methods defined in the Object class, including the clone() method. Neither the hashCode() method nor the equals() method is declared final in the Object() class, and it cannot be guaranteed that implementations of these methods will differentiate between all objects.

10.3

(a)

The clone() method of the Object class will throw a CloneNotSupportedException if the class of the object does not implement the Cloneable interface.

10.4

(a), (c), and (d)

The class java.lang.Void is considered a wrapper class, although it does not wrap any value. There is no class named java.lang.Int, but there is a wrapper class named java.lang.Integer. A class named java.lang.String also exists, but it is not a wrapper class since all strings in Java are objects.

10.5

(c) and (d)

The classes Character and Boolean are non-numeric wrapper classes and they do not extend the Number class. The classes Byte, Short, Integer, Long, Float, and Double are numeric wrapper classes that extend the Number class.

10.6

(a), (b), and (d)

All instances of wrapper classes are immutable.

10.7

(b) and (c)

All instances of wrapper classes except Void and Character have a constructor that accepts a string parameter. The class Object has only a default constructor.

10.8

(e)

While all numeric wrapper classes have the methods byteValue(), doubleValue(), floatValue(), intValue(), longValue(), and shortValue(), only the Boolean class has the booleanValue() method. Likewise, only the Character class has the charValue() method.

10.9

(b) and (d)

String is not a wrapper class. All wrapper classes except Boolean and Void have a compareTo() method. Only the numeric wrapper classes have an intValue() method. The Byte class, like all other numeric wrapper classes, extends the Number class.

10.10

(b) and (d)

The lines labeled (2) and (4) will print 11 exactly, since their expressions will return the int value 11. The expression Math.ceil(v) will return the double value 11.0, which will be printed as 11.0. The expression Math.floor(v) will return 10.0d.

10.11

(a)

The Math class does not have a method named tan2. However, it does have a method named atan2, which converts rectangular coordinates to polar coordinates.

10.12

(a)

The method round(float) will return a value of type int. A round(double) method also exists, which returns a value of type long.

10.13

(c)

The rounding function ceil() will return a value of type double. This is in contrast to the round() methods that will return values of integer types.

10.14

(b)

The value -0.5 is rounded up to 0 and the value 0.5 is rounded up to 1.

10.15

(b), (c), and (d)

The expression will evaluate to one of the numbers 0, 1, 2, or 3. Each number has an equal probability of being returned by the expression.

10.16

(b) and (e)

The operators - and & cannot be used in conjunction with a String object. The operators + and += perform concatenation on strings, and the dot operator accesses members of the String object.

10.17

(d)

The expression str.substring(2, 5) will extract the substring "kap". The method extracts the characters from index 2 to index 4, inclusive.

10.18

(d)

The program will print str3str1 when run. The concat() method will create and return a new String object, which is the concatenation of the current String object and the String object given as an argument. The expression statement str1.concat(str2) creates a new String object, but its reference value is not stored.

10.19

(c)

The trim() method of the String class returns a string where both the leading and the trailing white space of the original string have been removed.

10.20

(a) and (c)

The String class and all wrapper classes are declared final and, therefore, cannot be extended. The clone() method is declared protected in the Object class. String objects are immutable and, therefore, cannot be modified. The classes String and StringBuffer are unrelated.

10.21

(a), (b), (c), and (e)

The expressions ('c' + 'o' + 'o' + 'l') and ('o' + 'l') are of type int due to numeric promotion. Expression (d) is illegal since the String class has no constructor taking a single int parameter. Expression (a) is legal since string literals denote String objects and can be used just like any other object.

10.22

(d)

The constant expressions "ab" + "12" and "ab" + 12 will, at compile time, be evalutated to the string-valued constant "ab12". Both variables s and t are assigned a reference to the same interned String object containing "ab12". The variable u is assigned a new String object, created using the new operator.

10.23

(a), (c), and (d)

The String class does not have a constructor that takes a single int as a parameter.

10.24

(e)

The String class has no reverse() method.

10.25

(d)

The expression "abcdef".charAt(3) evaluates to the character 'd'. The charAt() method takes an int value as an argument and returns a char value. The expression ("abcdef").charAt(3) is legal. It also evaluates to the character 'd'. The index of the first character in a string is 0.

10.26

(e)

The expression "Hello there".toLowerCase().equals("hello there") will evaluate to true. The equals() method in the String class will only return true if the two strings have the same sequence of characters.

10.27

(c)

The variable middle is assigned the value 6. The variable nt is assigned the string "nt". The substring "nt" occurs three times in the string "Contentment!", starting at indexes 2, 5, and 9. The call s.lastIndexOf(nt, middle) returns the start index of the last occurrence of "nt", searching backwards from position 6.

10.28

(b)

The code will fail to compile since the expression (s == sb) is illegal. It compares references of two classes that are not related.

10.29

(e)

The program will compile without errors and will print have a when run. The contents of the string buffer are truncated down to 6 characters.

10.30

(a), (b), and (d)

The StringBuffer class does not have a constructor that takes an array of char as a parameter.

10.31

(a)

The StringBuffer class does not define a trim() method.

10.32

(d)

The program will construct an immutable String object containing "eenny" and a StringBuffer object containing " miny". The concat() method returns a reference value to a new immutable String object containing "eenny meeny", but the reference value is not stored. The append() method appends the string " mo" to the string buffer.