Developed by Y. Daniel Liang using Java Servlets (Current time is Sat Nov 21 19:28:05 EST 2009)

The questions are to accompany Introduction to Java Programming, 4E by Y. Daniel Liang. Please report errors to y.daniel.liang@gmail.com.

Introduction to Java Programming, 4E has been superseded by Introduction to Java Programming, 5E.

Chapter 9: Object-Oriented Software Development

Section 9.3 Analyzing Relationships Among Objects
1   _________ is a general binary relationship that describes an activity between two classes.

A. Association
B. Aggregation
C. Composition
D. Inheritance

2   __________ is a special form of association that represents an ownership relationship between two classes.

A. Association
B. Aggregation
C. Composition
D. Inheritance

3   __________ represents an exclusive ownership relationship between two classes.

A. Association
B. Aggregation
C. Composition
D. Inheritance

4   __________ models the is-a relationship between two classes.

A. Association
B. Aggregation
C. Composition
D. Inheritance

5   What is the best suitable relationship between Company and Employee?

A. Association
B. Aggregation
C. Inheritance
D. None

6   A faculty teaches a course. What is the best suitable relationship between Faculty and Course?

A. Association
B. Aggregation
C. Inheritance
D. None

7   What is the best suitable relationship between Employee and Faculty?

A. Association.
B. Aggregation.
C. Inheritance.
D. None.

8   The relationship between an interface and the class that implements it is

A. Association
B. Aggregation
C. Inheritance
D. None

Section 9.4 Class Development
9   The Rational class in this chapter is defined as a subclass of java.lang.Number. Which of the following statemens is correct?

A. Rational.doubleValue();
B. Rational.doubleValue("5/4");
C. new Rational(5, 4).doubleValue();
D. None of the above

10   The Rational class in this chapter extends java.lang.Number and implements java.lang.Comparable. Analyze the following code.


1. public class Test {
2. public static void main(String[] args) {
3. Number[] numbers = {new Rational(1, 2), new Integer(4), new Double(5.6)};
4. java.util.Arrays.sort(numbers);
5. }
6. }


A. The program has a syntax error because numbers is declared as Number[], so you cannot assign {new Rational(1, 2), new Integer(4), new Double(5.6)} to it.
B. The program has a runtime error because numbers is declared as Number[], so you cannot assign {new Rational(1, 2), new Integer(4), new Double(5.6)} to it.
C. The program has a syntax error because numbers is declared as Number[], so you cannot pass it to Arrays.sort(Object[]).
D. The program has a runtime error because the compareTo methods in Rational, Integer, and Double classes do not compare the value of one type with a value of another type.

Section 9.6 Processing Primitive Data Type Values as Objects
11   Which of the following statements will not convert a string s into i of int type?

A. i = Integer.parseInt(s);
B. i = (new Integer(s)).intValue();
C. i = Integer.valueOf(s).intValue();
D. i = Integer.valueOf(s);

12   Which of the following statements will convert a string s into a double value d?

A. d = Double.parseDouble(s);
B. d = (new Double(s)).doubleValue();
C. d = Double.valueOf(s).doubleValue();
D. All of the above.

13   Which of the following statements convert a double value d into a string s?

A. s = (new Double(d)).toString();
B. s = (Double.valueOf(s)).toString();
C. s = new Double(d).stringOf();
D. s = String.stringOf(d);

14   The java.lang.Number and its subclasses are introduced in Chapter 9. Analyze the following code.

     Number numberRef = new Integer(0);
     Double doubleRef = (Double)numberRef;

A. There is no such class named Integer. You should use the class Int.
B. The compiler detects that numberRef is not an instance of Double.
C. A runtime class casting exception occurs, since numberRef is not an instance of Double.
D. The program runs fine, since Integer is a subclass of Double.
E. You can convert an int to double, so you can cast an Integer instance to a Double instance.

15   Analyze the following code.

     Number[] numberArray = new Integer[2];
     numberArray[0] = new Double(1.5);

A. You cannot use Number as a data type since it is an abstract class.
B. Since each element of numbeArray is of the Number type, you cannot assign an Integer object to it.
C. Since each element of numbeArray is of the Number type, you cannot assign a Double object to it.
D. At runtime, new Integer[2] is assigned to numberArray. This makes each element of numberArray an Integer object. So you cannot assign a Double object to it.
E. None of the above.

16   Analyze the following code.
public class Test {
   public static void main(String[] args) {
     Number x = new Integer(3);
     System.out.println(x.intValue());
     System.out.println(x.compareTo(new Integer(4)));
   }
}

A. The program has a syntax error because an Integer instance cannot be assigned to a Number variable.
B. The program has a syntax error because intValue is an abstract method in Number.
C. The program has a syntax error because x does not have the compareTo method.
D. The program compiles and runs fine.
E. None of the above.

17   Analyze the following code.
public class Test {
   public static void main(String[] args) {
     Number x = new Integer(3);
     System.out.println(x.intValue());
     System.out.println((Integer)x.compareTo(new Integer(4)));
   }
}

A. The program has a syntax error because an Integer instance cannot be assigned to a Number variable.
B. The program has a syntax error because intValue is an abstract method in Number.
C. The program has a syntax error because x cannot be casted into Integer.
D. The program has a syntax error because the member access operator (.) is executed before the casting operator.
E. The program compiles and runs fine.

18   Which of the following statements is correct?

A. Integer.parseInt("12", 2);
B. Integer.parseInt(100);
C. Integer.parseInt("100");
D. Integer.parseInt(100, 16);

19   What is the output of Integer.parseInt("10", 2)?
A. 1;
B. 2;
C. 10;
D. Invalid statement;