Chapter 13 Quiz Study Guide

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

False

A subclass cannot override a concrete method in a superclass to define it as abstract.

False

A subclass of a nonabstract superclass cannot be abstract.

True

An abstract class can be extended.

True

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.

True

An abstract method must be nonstatic.

False

An interface can extend an abstract class.

True

An interface can extend one or more interfaces.

True in Java 8

An interface can have default methods.

The program has a compile error because the member access operator (.) is executed before the casting operator.

Analyze the following code. Which of the following statements is correct? 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))); } }

The program has a compile error because x does not have the compareTo method.

Analyze the following code. Which of the following statements is correct? 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))); } }

The program has a compile error because o1 is an Object instance and it does not have the compareTo method. The program would compile if ((Comparable)o1.compareTo(o2) >= 0) is replaced by (((Comparable)o1).compareTo(o2) >= 0). Explanation: The . operator is performed before casting.

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; } } }

calendar.get(Calendar.MONTH)

Assume Calendar calendar = new GregorianCalendar(). __________ returns the month of the year.

calendar.getActualMaximum(Calendar.DAY_OF_MONTH)

Assume Calendar calendar = new GregorianCalendar(). __________ returns the number of days in a month.

calendar.get(Calendar.WEEK_OF_YEAR)

Assume Calendar calendar = new GregorianCalendar(). __________ returns the week of the year.

Composition

Assume an employee can work for only one company. What is the best suitable relationship between Company and Employee?

n1 is an Integer object whose compareTo method require an Integer argument, but n2 is declared as Object. The compiler will raise an error.

Can the following code be compiled? Why? Integer n1 = new Integer(3); Object n2 = new Integer(4); System.out.println(n1.compareTo(n2));

No. Calendar is an abstract class.

Can you create a Calendar object using the Calendar class?

that a class contains a data field that references another object

Composition means ______________.

false

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); } }

No

Suppose A is an interface. Can you create an instance using new A()?

The program has a compile error on Line 5 because java.util.Calendar is an abstract class. Explanation: (A) is incorrect since it is OK to use abstract class as data type for arrays. new Calendar[10] does not create Calendar objects. It just creates an array with 10 elements, each of which can reference to a Calendar object. (B) is correct since you cannot create an object from an abstract class. (C) is incorrect since it is fine to create a GregorianCalendar object and assign its reference to a variable of its superclass type.

The java.util.Calendar and java.util.GregorianCalendar classes are introduced in Chapter 11. Analyze the following code. Which of the following statements is correct? 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. }

[New York, Dallas] Explanation: The code added New York to list and cloned list1 from list. list1 now contains New York. Dallas is added to list1. So list1 contains New Your and Dallas.

The output from the following code is __________. java.util.ArrayList<String> list = new java.util.ArrayList<String>(); list.add("New York"); java.util.ArrayList<String> list1 = (java.util.ArrayList<String>)(list.clone()); list.add("Atlanta"); list1.add("Dallas"); System.out.println(list1);

Inheritance

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

True

True or false? If a class implements Comparable, the object of the class can invoke the compareTo method.

Inheritance

What is the best suitable relationship between Employee and Faculty?

The add method in the Calendar class is abstract.

Which method in the Calendar class is abstract?

By implementing the Comparable interface, the object of the class can be passed to a method that requires a Comparable type.

You can define the compareTo method in a class without implementing the Comparable interface. What are the benefits of implementing the Comparable interface?

A primitive type Explanation: Objects and arrays are reference types. Primitive types are not.

_______ is not a reference type.

b is an instance of A followed by b is an instance of C.

Show the output of running the class Test in the following code lines: interface A { } class C { } class B extends D implements A { } public class Test { public static void main(String[] args) { B b = new B(); if (b instanceof A) System.out.println("b is an instance of A"); if (b instanceof C) System.out.println("b is an instance of C"); } } class D extends C { }

3 3.0

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()); } }

true false true

Show the output of the following code: java.util.Date date = new java.util.Date(); java.util.Date date1 = date; java.util.Date date2 = (java.util.Date)(date.clone()); System.out.println(date == date1); System.out.println(date == date2); System.out.println(date.equals(date2));

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.

Analyze the following code. Number[] numberArray = new Integer[2]; numberArray[0] = new Double(1.5);

interface A { void print();} Explanation: In A, the print() method in the interface is a concrete method. In B and C, the abstract keyword is used before the interface, which is wrong. The correct answer is D.

Which of the following is a correct interface? A. interface A { void print() { }; } B. abstract interface A { print(); } C. abstract interface A { abstract void print() { };} D. interface A { void print();}

Use the GregorianCalendar class's no-arg constructor, you can create an instance of Calendar.

How do you create a Calendar object for the current time?

that a class can extend another class

Inheritance means ______________.

True in Java 8

An interface can have static methods.

True

An interface is compiled into a separate bytecode file.

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. Explanation: (A) and (B) are incorrect since it is OK to define a class without a no-arg constructor. (C) is incorrect since it is OK to pass fruits to Arrays.sort(Object[]) without compile errors. (D) is correct because the Arrays.sort method requires the objects in the array to be comparable and their class must implement the java.lang.Comparable interface.

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; } }

The program has a compile error because the return type of the clone() method is java.lang.Object. Explanation: (A) is wrong because Date implements and Cloneable and overrides the clone() method. (B) and (C) are wrong because x = y is an assignment expression, which assigns y to x. (D) is correct. You have to cast it into Date in order to assign it to y.

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); } }

that data fields should be declared private

Encapsulation means ______________.

that a variable of supertype can refer to a subtype object

Polymorphism means ______________.

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.

Show the error in the following code: interface A { void m1(); } class B implements A { void m1() { System.out.println("m1"); } }

true false list is [New York, Atlanta] list1 is [New York, Atlanta] list2.get(0) is New York list2.size() is 1

Show the output of the following code: ArrayList<String> list = new ArrayList<>(); list.add("New York"); ArrayList<String> list1 = list; ArrayList<String> list2 = (ArrayList<String>)(list.clone()); list.add("Atlanta"); 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());

-1 3 0 0.333333333333

Show the output of the following code? Rational r1 = new Rational(-2, 6); System.out.println(r1.getNumerator()); System.out.println(r1.getDenominator()); System.out.println(r1.intValue()); System.out.println(r1.doubleValue());

A a = new B(); B b = new B(); Explanation: Since B is a concrete class with a no-arg constructor, d is correct. Since an instance of B is also an instance of A, b is also correct.

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? A. A a = new A(); B. A a = new B(); C. B b = new A(); D. B b = new B();

A a = new B(); B b = new B(); Explanation: Since B is a concrete class with a no-arg constructor, d is correct. Since an instance of B is also an instance of A, b is also correct.

Suppose A is an interface, B is a concrete class with a no-arg constructor that implements A. Which of the following is correct? A. A a = new A(); B. A a = new B(); C. B b = new A(); D. B b = new B();

Yes

Suppose A is an interface. Can you declare a reference variable x with type A like this? A x;

The program has a compile error because the clone() method is protected in the Object class. 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. To enable a Circle object to be cloned, the Circle class has to override the clone() method and implement the java.lang.Cloneable interface. If GeometricObject implements Cloneable and Circle overrides the clone() method, the clone() method will work fine to clone Circle objects.

The GeometricObject and Circle classes are defined in this chapter. Analyze the following code. Which statements are correct? 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); } }

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. Explanation: (A) and (B) are incorrect because Rational, Integer, and Double are subclasses of Number and any instances of these classes can be elements of the Number[] array. (C) is incorrect because it is fine to pass an instance of Number[] to a parameter of the Object[] type. (D) is correct because the compareTo method in Rational, Integer, Double only compare two Rational objects, two Integer objects, or two Double objects.

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

new Rational(5, 4).doubleValue(); new Rational(5, 4).intValue();

The Rational class in this chapter is defined as a subclass of java.lang.Number. Which of the following expressions is correct? A. Rational.doubleValue(); B. Rational.doubleValue("5/4"); C. new Rational(5, 4).doubleValue(); D. new Rational(5, 4).toDoubleValue(); E. new Rational(5, 4).intValue();

A runtime class casting exception occurs, since numberRef is not an instance of Double.

The java.lang.Number and its subclasses are introduced in Chapter 11. Analyze the following code. Number numberRef = new Integer(0); Double doubleRef = (Double)numberRef;

BEDC

What is the output of running class Test? public class Test { public static void main(String[] args) { new Circle9(); } } public abstract class GeometricObject { protected GeometricObject() { System.out.print("A"); } protected GeometricObject(String color, boolean filled) { System.out.print("B"); } } public class Circle9 extends GeometricObject { /** No-arg constructor */ public Circle9() { this(1.0); System.out.print("C"); } /** Construct circle with a specified radius */ public Circle9(double radius) { this(radius, "white", false); System.out.print("D"); } /** Construct a circle with specified radius, filled, and color */ public Circle9(double radius, String color, boolean filled) { super(color, filled); System.out.print("E"); } }

In (a), a compile 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. In (b), no compile errors, because Test5 extends Object and the clone() method is defined in the Obejct class, which is visible in Test5. However, when you run the code, a CloneNotSupportedException will be thrown, because the clone() method is not implemented in Test5 and Test5 does not implement the Cloneable interface.

What is wrong in the code in (a)? Why does the code in (b) have no compile errors? (a) public class Test { public static void main(String[] args) { GeometricObject x = new Circle(3); GeometricObject y = x.clone(); System.out.println(x == y); } } (b) public class Test5 { public static void main(String[] args) throws CloneNotSupportedException { Test5 x = new Test5(); GeometricObject y = (GeometricObject)x.clone(); } }

The program has a syntax error because x does not have the compareTo method.

What is wrong in 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 Person class does not implement the Comparable interface, two persons can not be compared using the compareTo method.

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 constructors in an abstract class are private. You may declare a final abstract class. An interface may contain constructors. Explanation: A and B are correct. C is wrong. D is wrong. E is wrong because an interface does not have constructors.

Which of the following are incorrect? A. An abstract class contains constructors. B. The constructors in an abstract class should be protected. C. The constructors in an abstract class are private. D. You may declare a final abstract class. E. An interface may contain constructors.

abstract class A { abstract void unfinished(); } Explanation: In A and B, abstract keyword is missing for the class. In D, class and abstract are in a wrong order. The correct answer is C.

Which of the following class definitions defines a legal abstract class? A. class A { abstract void unfinished() { } } B. class A { abstract void unfinished(); } C. abstract class A { abstract void unfinished(); } D. public class abstract A { abstract void unfinished(); }

public abstract void method(); Explanation: In A, the method has no return type. In C, void and abstract are in a wrong order. In D, the abstract keyword is missing. In E, the body {} should be removed. The correct answer is B.

Which of the following declares an abstract method in an abstract Java class? A. public abstract method(); B. public abstract void method(); C. public void abstract method(); D. public void method() {} E. public abstract void method() {}

A data field is derived from other data fields in the same class. A method must be invoked after/before invoking another method in the same class. A method is an instance method, but it does not reference any instance data fields or invoke instance methods. A parameter is passed from a constructor to initialize a static data field. Explanation: A is not good because there is no need to define the data field if it can be derived from other data fields. B is not good because it is highly problematic to impose any order for invoking the methods. C is a bad design because in this case the method should be defined as static. D is not good because a static data field is now tied to the creation of a specific object, which should not be the case.

Which of the following is poor design? A. A data field is derived from other data fields in the same class. B. A method must be invoked after/before invoking another method in the same class. C. A method is an instance method, but it does not reference any instance data fields or invoke instance methods. D. A parameter is passed from a constructor to initialize a static data field.

A class should describe a single entity and all the class operations should logically fit together to support a coherent purpose. The constructors may be protected. Explanation: (B) is not true. Most classes have a no-arg constructor. But sometimes, it does not make any sense to provide a no-arg constructor. For example, StringTokenizer does not have a no-arg constructor. (C) is not true. For example, the constructor in the Math class is private to prevent creating instances from the class, because there is no need to create instances for the Math class since all methods are static in Math. The constructors for abstract classes should be protected in most cases.

Which of the following statements are true? A. A class should describe a single entity and all the class operations should logically fit together to support a coherent purpose. B. A class should always contain a no-arg constructor. C. The constructors must always be public. D. The constructors may be protected.

Inheritance models the is-a relationship between two classes. A strong is-a relationship describes a direct inheritance relationship between two classes. A weak is-a relationship describes that a class has certain properties. A strong is-a relationship can be represented using class inheritance. A weak is-a relationship can be represented using interfaces.

Which of the following statements are true? A. Inheritance models the is-a relationship between two classes. B. A strong is-a relationship describes a direct inheritance relationship between two classes. C. A weak is-a relationship describes that a class has certain properties. D. A strong is-a relationship can be represented using class inheritance. E. A weak is-a relationship can be represented using interfaces.

The String class implements Comparable. The Date class implements Comparable. The Double class implements Comparable. The BigInteger class implements Comparable. Explanation: Many classes in the Java API implements the Comparable interface if the objects of the class can be compared. Strings, Dates, Doubles, and BigIntegers can all be compared. These classes implement the Comparable interface.

Which of the following statements are true? A. The String class implements Comparable. B. The Date class implements Comparable. C. The Double class implements Comparable. D. The BigInteger class implements Comparable.

If you compile a class with errors, a .class file is created for the class. Explanation: A .class file is created for each Java class and interface. But if it has a compile error, no .class file is created.

Which of the following statements is false? A. If you compile an interface without errors, a .class file is created for the interface. B. If you compile a class without errors but with warnings, a .class file is created. C. If you compile a class with errors, a .class file is created for the class. D. If you compile an interface without errors, but with warnings, a .class file is created for the interface.

Integer i = 4.5; Explanation: i is an Integer. You cannot assign 4.5 to i. A is incorrect. B, C, and D are correct. 4.5 is auto-boxed to new Double(4.5). A Double object is an instance of Object and Double.

Which of the following statements is incorrect? A. Integer i = 4.5; B. Double i = 4.5; C. Object i = 4.5; D. Number i = 4.5;

A data field can be declared abstract. Explanation: E is wrong, because a data field cannot be declared abstract. Only methods and classes can be declared abstract.

Which of the following statements regarding abstract methods is false? A. Abstract classes have constructors. B. A class that contains abstract methods must be abstract. C. It is possible to declare an abstract class that contains no abstract methods. D. An abstract method cannot be contained in a non-abstract class. E. A data field can be declared abstract.

An abstract class can have instances created using the constructor of the abstract class. Explanation: A is wrong because you cannot create an instance using the constructor from an abstract class.

Which of the following statements regarding abstract methods is false? A. An abstract class can have instances created using the constructor of the abstract class. B. An abstract class can be extended. C. A subclass of a non-abstract superclass can be abstract. D. A subclass can override a concrete method in a superclass to declare it abstract. E. An abstract class can be used as a data type.


Ensembles d'études connexes

Culture/Spiritual Taylor Chapter 5

View Set

Chapter 34 Pediatric Emergencies

View Set

SCI 1030 Week 1 - Chapters 1, 2, & 4

View Set