10 Fundamental Classes



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



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.



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


(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.


(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.


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

All instances of wrapper classes are immutable.


(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.



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.


(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.


(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.



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.



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



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.



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


(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.


(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.



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



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.



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.


(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.


(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.



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.


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

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



The String class has no reverse() method.



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.



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.



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.



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



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.


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

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



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



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.