6.
The java.util.Calendar and java.util.GregorianCalendar classes are introduced in Chapter 11. Analyze the following code.
1. import java.util.*;
2. public class Test {
3. public static void main(String[] args) {
4. Calendar[] calendars = new Calendar[10];
5. calendars[0] = new Calendar();
6. calendars[1] = new GregorianCalendar();
7. }
8. }
单选题 (2 分) 2 分
- A.
The program has no compile errors.
- B.
The program has a compile error on Line 5 because java.util.Calendar is an abstract class.
- C.
The program has a compile error on Line 4 because java.util.Calendar is an abstract class.
- D.
The program has a compile error on Line 6 because Calendar[1] is not of a GregorianCalendar type.
7.
Analyze the following code.
public class Test {
public static void main(String[] args) {
java.util.Date x = new java.util.Date();
java.util.Date y = x.clone();
System.out.println(x = y);
}
}
单选题 (2 分) 2 分
- A.
A java.util.Date object is not cloneable.
- B.
x = y in System.out.println(x = y) causes a compile error because you cannot have an assignment statement inside a statement.
- C.
x = y in System.out.println(x = y) causes a runtime error because you cannot have an assignment statement inside a statement.
- D.
The program has a compile error because the return type of the clone() method is java.lang.Object.
13.
Assume Calendar calendar = new GregorianCalendar(). __________ returns the month of the year.
单选题 (2 分) 2 分
- A.
calendar.get(Calendar.WEEK_OF_MONTH)
- B.
calendar.get(Calendar.WEEK_OF_YEAR)
- C.
calendar.get(Calendar.MONTH)
- D.
calendar.get(Calendar.MONTH_OF_YEAR)
16.
Assume an employee can work for only one company. What is the best suitable relationship between Company and Employee?
单选题 (2 分) 2 分
- A.
Aggregation
- B.
Composition
- C.
None
- D.
Inheritance
17.
Analyze the following code.
1. public class Test {
2. public static void main(String[] args) {
3. Fruit[] fruits = {new Fruit(2), new Fruit(3), new Fruit(1)};
4. java.util.Arrays.sort(fruits);
5. }
6. }
class Fruit {
private double weight;
public Fruit(double weight) {
this.weight = weight;
}
}
回答错误
单选题 (2 分) 0 分
- A.
The program has a compile error because the Fruit class does not have a no-arg constructor.
- B.
The program has a runtime error on Line 4 because the Fruit class does not implement the java.lang.Comparable interface and the Fruit objects are not comparable.
- C.
The program has a compile error on Line 4 because the Fruit class does not implement the java.lang.Comparable interface and the Fruit objects are not comparable.
- D.
The program has a runtime error on Line 3 because the Fruit class does not have a no-arg constructor.
22.
The Rational class in this chapter is defined as a subclass of java.lang.Number. Which of the following expressions is correct?
多选题 (2 分) 2 分
- A.
Rational.doubleValue();
- B.
new Rational(5, 4).intValue();
- C.
new Rational(5, 4).toDoubleValue();
- D.
new Rational(5, 4).doubleValue();
- E.
Rational.doubleValue("5/4");
24.
Which of the following statements are true?
多选题 (2 分) 2 分
- A.
The Date class implements Comparable.
- B.
The Double class implements Comparable.
- C.
The BigInteger class implements Comparable.
- D.
The String class implements Comparable.
25.
Which of the following statements are true?
多选题 (2 分) 2 分
- A.
A class should describe a single entity and all the class operations should logically fit together to support a coherent purpose.
- B.
The constructors may be protected.
- C.
The constructors must always be public.
- D.
A class should always contain a no-arg constructor.
26.
_______ is a reference type.
多选题 (2 分) 2 分
- A.
An array type
- B.
A class type
- C.
A primitive type
- D.
An interface type
28.
The GeometricObject and Circle classes are defined in this chapter. Analyze the following code.
public class Test {
public static void main(String[] args) {
GeometricObject x = new Circle(3);
GeometricObject y = (Circle)(x.clone());
System.out.println(x);
System.out.println(y);
}
}
多选题 (2 分) 2 分
- A.
If GeometricObject implements Cloneable and Circle overrides the clone() method, the clone() method will work fine to clone Circle objects.
- B.
After you override the clone() method and make it public in the Circle class, the problem can compile and run just fine, but y is null if Circle does not implement the Cloneable interface.
- C.
The program has a compile error because the clone() method is protected in the Object class.
- D.
To enable a Circle object to be cloned, the Circle class has to override the clone() method and implement the java.lang.Cloneable interface.
30.
Analyze the following code:
public class Test1 {
public Object max(Object o1, Object o2) {
if ((Comparable)o1.compareTo(o2) >= 0) {
return o1;
}
else {
return o2;
}
}
}
回答错误
多选题 (2 分) 0 分
- A.
The program has a compile error because Test1 does not have a main method.
- B.
The program has a compile error because o1 is an Object instance and it does not have the compareTo method.
- C.
The program has a compile error because you cannot cast an Object instance o1 into Comparable.
- D.
The program would compile if ((Comparable)o1.compareTo(o2) >= 0) is replaced by (((Comparable)o1).compareTo(o2) >= 0).
32.
Which of the following statements are correct?
多选题 (2 分) 2 分
- A.
Number i = 4.5;
- B.
Integer i = 4.5;
- C.
Double i = 4.5;
- D.
Object i = 4.5;
34.
Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a no-arg constructor. Which of the following is correct?
多选题 (2 分) 2 分
- A.
B b = new B();
- B.
A a = new B();
- C.
A a = new A();
- D.
B b = new A();
35.
Which of the following is incorrect?
多选题 (2 分) 2 分
- A.
An interface may contain constructors.
- B.
The constructors in an abstract class are private.
- C.
You may declare a final abstract class.
- D.
The constructors in an abstract class should be protected.
- E.
An abstract class contains constructors.
36.
Which of the following is poor design?
多选题 (2 分) 2 分
- A.
A data field is derived from other data fields in the same class.
- B.
A method is an instance method, but it does not reference any instance data fields or invoke instance methods.
- C.
A parameter is passed from a constructor to initialize a static data field.
- D.
A method must be invoked after/before invoking another method in the same class.
39.
An interface can extend any number of interfaces using the implements keyword.
回答错误
判断题 (2 分) 0 分
- A.
false
- B.
true
49.
All the numeric wrapper classes implement the Comparable interface.
判断题 (2 分) 2 分
- A.
true
- B.
false