









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










13.1 Which of the following classes defines a legal abstract class? (a) class A { abstract void unfinished() {}} (b) public class abstract A {abstract void unfinished(); } (c) public class abstract A {abstract void unfinished(); } (d) abstract class A {protected void unfinished(); } (e) abstract class A {abstract void unfinished(); } (f) abstract class A {abstract int unfinished(); } ✓a.no: abstract method requires abstract class b. no: abstract keyword is in the wrong place c. no: abstract keyword is in the wrong place
d. technically, can have an abstract class with out abstract method, unsure if protected counts. answer key just says no. e. yes. f. yes. 13.10 Which method in the Calendar class is abstract? ✓public void abstract add(int field, int amount) {} 13.11 How do you create a Calendar object for the current time? ✓Use the GregorianCalendar class's constructor, you can create an instance of Calendar. 13.12 For a Calendar object c, how do you get its year, month, date, hour, minute, and second? ✓c.get(Calendar.YEAR) c.get(Calendar.MONTH) c.get(Calendar.DAY_ON_MONTH) c.get(Calendar.HOUR) c.get(Calendar.MINUTE) c.get(Calendar.SECOND) 13.13 Suppose A is an interface. Can you create an instance using new A()? ✓No. Interfaces are abstract. 13.14 Suppose A is an interface. Can you declare a reference variable x with type A like this? A x; ✓No. Interfaces are abstract. 13.15 Which of the following is a correct interface? (a) interface A { void print() { }
13.16 Show the error in the following code: interface A { void m1(); } class B implements A { void m1() { System.out.println("m1"); } } ✓All methods defined in an interface are public. When a class implements the interface, the method must be declared public. The visibility cannot be reduced. 13.17 True or false? If a class implements Comparable, the object of the class can invoke the compareTo method. ✓True. 13.18 Which of the following is the correct method header for the compareTo method in the String class? public int compareTo(String o) public int compareTo(Object o) ✓public int compareTo(String o) 13.19 Can the following code be compiled? Why? Integer n1 = new Integer(3); Object n2 = new Integer(4); System.out.println(n1.compareTo(n2)); ✓n1 is an Integer object whose compareTo method require an Integer argument, but n2 is declared as Object. The compiler will raise an error. 13.2 The getArea() and getPerimeter() methods may be removed from the GeometricObject class. What are the benefits of defining getArea() and
getPerimeter() as abstract methods in the GeometricObject class? ✓The benefits are for generic programming. A variable of GeometricObject type can use the getArea and getPerimeter methods at compilation time. 13.20 You can define the compareTo method in a class without implementing the Comparable interface. What are the benefits of implementing the Comparable interface? ✓By implementing the Comparable interface, the object of the class can be passed to a method that requires a Comparable type. 13.21 What is wrong in the following code? public class Test { public static void main(String[] args) { Person[] persons = {new Person(3), new Person(4), new Person(1)}; java.util.Arrays.sort(persons); } } class Person { private int id; Person(int id) { this.id = id; } } ✓The Person class does not implement the Comparable interface, two persons can not be compared using the compareTo method. (java.util.Arrays.sort() uses the compareTo method to sort arrays). 13.22 Can you invoke the clone() method to clone an object if the class for the object does not implement the java.lang.Cloneable? Does the Date class implement Cloneable? ✓No. Yes.
System.out.println(list == list1); System.out.println(list == list2); System.out.println("list is " + list); System.out.println("list1 is " + list1); System.out.println("list2.get(0) is " + list2.get(0)); System.out.println("list2.size() is " + list2.size()); ✓A syntax error is reported because clone() is protected in Object. To enable cloning, do two things: (1) override clone() in the class for the object to be cloned; (2) implement java.lang.Cloneable for the class. 13.26 What is wrong in the following code?What is wrong in the following code? public class Test { public static void main(String[] args) { GeometricObject x = new Circle(3); GeometricObject y = x.clone(); System.out.println(x == y); } } ✓A syntax error is reported because clone() is protected in Object. To enable cloning, do two things: (1) override clone() in the class for the object to be cloned; (2) implement java.lang.Cloneable for the class. 13.27 Give an example to show why interfaces are preferred over abstract classes. ✓Interfaces are preferred over abstract classes because an interface can define a common supertype for unrelated classes and is more flexible. EX: An how to eat method defined as an abstract class method can only be applied to subclasses that are animals, excluding all other non-animal edibles. An Edible interface however, can be impletmented by any class, animal, mineral, vegetable, fruit etc.
13.28 Define the terms abstract classes and interfaces. What are the similarities and differences between abstract classes and interfaces? ✓See the definitions above. 13.29 True or false? a. An interface is compiled into a separate bytecode file. b. An interface can have static methods. c. An interface can extend one or more interfaces. d. An interface can extend an abstract class. e. An abstract class can extend an interface. ✓a. True. b. False. c. True. d. False. e. True. 13.3 True or false? a. An abstract class can be used just like a nonabstract class except that you cannot use the new operator to create an instance from the abstract class. b. An abstract class can be extended. c. A subclass of a nonabstract superclass cannot be abstract. d. A subclass cannot override a concrete method in a superclass to define it as abstract. e. An abstract method must be nonstatic. ✓a. true. b. true. c. false. d. false. e. true. 13.30 Show the output of the following code?
13.35 Class Design Guidelines ✓Cohesion - should describe a single entity and all methods support a coherent purpose. Consistency - Follow standard style and naming conventions. Encapsulation - make private to hid data form direct access by clients Clarity - a clear contract that is easy to explain and to understand Completeness - should take dynamic users in mind and create cohesive methods Instance vs. Static - methods not dependant on a specific instance should be static; always reference static variables and mthods from a class name to improve readibility and avoid errors Inheritance vs Aggregation - inheritance is an "is-a" relationship and aggregation is a "has-a" relationship Interfaces vs. Abstract classes - Strong "is-a" relationship should be an abstract class while a weak is-a relationship should be an interface. Interfaces should be uses for unrelated classes with similar properties as needed and is much more flexible. 13.35 The preceding question shows a bug in the toString method. Revise the toString() method to fix the error. ✓public String toString() { if (numerator == 0 || denominator == 1) return numerator + ""; else return numerator + "/" + denominator; } 13.4 Why do the following two lines of code compile but cause a runtime error? Number numberRef = new Integer(0);
Double doubleRef = (Double)numberRef; ✓At runtime, JVM attempts to convert numberRef to a Double object, but numberRef is an instance of Integer, not Double. 13.5 Why do the following two lines of code compile but cause a runtime error? Number[] numberArray = new Integer[2]; numberArray[0] = new Double(1.5); ✓numberArray[0] is of the Integer type. It will be a casting error at runtime. 13.6 Show the output of the following code. public class Test { public static void main(String[] args) { Number x = 3; System.out.println(x.intValue()); System.out.println(x.doubleValue()); } } ✓ 3
13.7 What is wrong in the following code? (Note that the compareTo method for the Integer and Double classes was introduced in Section 10.7.) 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))); }
@UML denoted by italicizing the name of the class and the specific abstract method @abstract method cannot exist in a non abstract class, therefore all subclasses must override any abstract methods of the abstract superclass or are themselves abstract Cloneable Interface ✓Contains constants and abstract method to allow the copy of an object in implement. @clone() default method performs a shallow copy. Comparable Interface ✓Defines the compareTo() method for comparing objects. This allows one to choose the properties of the objects rather than their reference location. @Comparable is a GENERIC interface defined by
Interface inheritance ✓The relationship between the class and the interface. Marker Interface ✓an empty Interface that does not contain constants or methods native keyword ✓indicates that a method is not written in java but is implemented in the JVM for the native platform Number Class ✓Abstract superclass for double, int, float, long, short, byte, BigInteger, BigDecimal Rational Class ✓This class is for representing and processing rational numbers ( numerator / denominator) as JAVA does not have a data type that represents rational numbers and double/float are not precise. @the rational number is encapsulated in a rational object @immutable shallow copy ✓when an object is copies, if a field houses an object, only the reference to the object is copied. a new object for the field is not created. Show the output of the following code. public class Test { public static void main(String[] args) { House house1 = new House(1, 1750, 50); House house2 = (House)house1.clone(); System.out.println(house1.equals(house2); } } ✓false