Apply Your Knowledge

Review Questions

1:For what purpose are the Object and Class classes used?
2:What are wrapped classes?
3:What is the difference between the String and StringBuffer classes?
4:Why are the methods of the Math class static?
5:Which class is extended by all other classes?
6:Which class should you use to obtain design information about an object?
7:What is the purpose of the Runtime class?
8:What is the purpose of the System class?
9:Which Math method is used to calculate the absolute value of a number?
10:What are E and PI?

Exam Questions

1:Which of the following is true?
  1. The Class class is the superclass of the Object class.

  2. The Object class is final.

  3. The Class class can be used to load other classes.

  4. The ClassLoader class can be used to load other classes.

2:Which of the following classes is used to perform basic console I/O?
  1. System

  2. SecurityManager

  3. Math

  4. Runtime

3:Which of the following is not a wrapper class?
  1. String

  2. Integer

  3. Boolean

  4. Character

4:What's wrong with the following code?
public class Question {
 public static void main(String args[]) {
  Boolean b = new Boolean("TRUE");
  if(b) {
   for(Integer i=0;i<10;++i) {
    System.out.println(i);
   }
  }
 }
}

  1. There is nothing wrong with the code. It compiles and runs fine.

  2. The if condition should be a boolean instead of a Boolean.

  3. The index of the for statement should be an int instead of an Integer.

  4. It is illegal to construct a Boolean value.

5:Which of the following methods causes the String object referenced by s to be changed?
  1. s.concat()

  2. s.toUpperCase()

  3. s.replace()

  4. None of the above

6:What is the output of the following program?
public class Question {
 public static void main(String args[]) {
  String s1 = "abc";
  String s2 = "def";
  String s3 = s1.concat(s2.toUpperCase());
  System.out.println(s1+s2+s3);
 }
}

  1. abcdefabcdef

  2. abcabcDEFDEF

  3. abcdefabcDEF

  4. None of the above

7:Which of the following methods is a method of the Math class?
  1. absolute()

  2. log()

  3. cosine()

  4. sine()

8:Which of the following methods is a method of the String class?
  1. delete()

  2. append()

  3. reverse()

  4. replace()

9:Which of the following is true about the Error and Exception classes?
  1. Both classes extend Throwable.

  2. The Error class is final and the Exception class is not.

  3. The Exception class is final and the Error class is not.

  4. Both classes implement Throwable.

10:Which of the following is true?
  1. The Void class extends the Class class.

  2. The Float class extends the Double class.

  3. The System class extends the Runtime class.

  4. The Integer class extends the Number class.

Answers to Review Questions

A1: The Object class is the highest-level class in the Java class hierarchy. The Class class is used to represent the classes and interfaces that the Java program loads. See the section “The Object, Class, and Package Classes.”
A2: Wrapped classes are classes that allow primitive types to be accessed as objects. See the section “The Wrapped Classes.”
A3: String objects are constants. StringBuffer objects are not. See the section “The String and StringBuffer Classes.”
A4: Methods of the Math class are static so that they can be invoked as if they are part of a mathematical code library. See the section “The Math and StrictMath Classes.”
A5: The Object class is extended by all other classes. See the section “The Object, Class, and Package Classes.”
A6: The Class class is used to obtain information about an object's design. See the section “The Object, Class, and Package Classes.”
A7: The purpose of the Runtime class is to provide access to the Java runtime system. See the section “The ClassLoader, SecurityManager, Runtime, and RuntimePermission Classes.”
A8: The purpose of the System class is to provide access to system resources. See the section “The System Class.”
A9: The abs() method is used to calculate absolute values. See the section “The Math and StrictMath Classes.”
A10: E is the base of the natural logarithm and PI is mathematical value pi. See the section “The Math and StrictMath Classes.”

Answers to Exam Questions

A1: C. and D. The Object class is the highest-level class in the Java 2 API; therefore, it cannot be a subclass of Class or final. See the section “The Object, Class, and Package Classes.”
A2: A. System.in, System.err, and System.out support console I/O. See the section “The System Class.”
A3: A. There is no primitive String type to wrap. See the section “The String and StringBuffer Classes.”
A4: B. and C. The conditions of if statements take boolean values. Arithmetic operators cannot be used with Integer objects. See the sections “The Byte, Short, Integer, and Long Classes.”
A5: D. String objects are immutable and cannot be changed. See the section “The String and StringBuffer Classes.”
A6: C. The last three characters are converted to uppercase in the creation of String s3. See the section “The String and StringBuffer Classes.”
A7: B. log() is a method of Math. absolute(), cosine(), and sine() are not methods of Math (though Math does have cos(), sin(), and abs() methods). See the section “The Math and StrictMath Classes.”
A8: D. A., B., and C. look like String methods, but they are not. See the section “The String and StringBuffer Classes.”
A9: A. Throwable is the superclass of both Error and Exception. See the section “The Throwable Class.”
A10: D. The Integer class extends the Number class. See the section “The Number Class.”
Suggested Readings and Resources

1. The JDK 1.3 java.lang package API description. You can download this or browse it online at http://java.sun.com/j2se/1.3/docs.html.

2. Sun's tutorial at http://java.sun.com/docs/books/tutorial.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.20.224.107