Chapter 11, 12, 13

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

concrete class

A concrete class is a class that is not abstract, and hence can be instantiated.

If an object of type objOfExcptType1 is thrown, three catch blocks will execute.

False

If an object of type objOfExcptType3 is thrown, no catch blocks will execute.

False

A throw executed in a method automatically causes a jump to the last return statement in the method.

False A throw in try block causes an immediate jump to the catch block. A throw not within a try block causes immediate exit to the caller without executing any return statement.

An abstract class can be instantiated as an object.

False An abstract class is missing some behaviors. A subclass that implements those behavior can then be instantiated.

Consider a program that catalogs the types of trees in a forest. Each tree object contains the tree's species type, age, and location. This program will benefit from an abstract class to represent the trees.

False Because there is no information specific to each species of tree, each tree object can simply possess the species type, age, and location information.

After an exception is thrown and a catch block executes, execution resumes after the throw statement.

False Execution proceeds to the code that follows the catch block; the code after the throw statement is skipped.

For a method that may contain a throw, all of the method's statements, including the throw, must be surrounded by a try block.

False If no try/catch blocks appear in the method, the throw causes automatic exiting from the method. The calling code is then checked for a try/catch block until the exception is caught or main() is exited. In the latter case, main() must specify a throws clause with the appropriate exception type.

Consider a program that maintains a grocery list. Each item, like eggs, has an associated price and weight. Each item belongs to a category like produce, meat, or cereal, where each category has additional features, such as meat having a "sell by" date. This program will benefit from an abstract class.

False Normal inheritance is sufficient. A superclass like Item might implement price and weight. Then subclasses like MeatItem might add behavior like a "sell by" date. But no behavior was mentioned that must be implemented by all subclasses.

The Object class's toString() method returns a String containing only the Object instance's type.

False Object's default toString() implementation returns the Object instance's type followed by its address in memory.

User-defined classes are not derived from the Object class.

False The Object class is the base class for all classes including built-in classes (e.g., Integer, Double) and user-defined classes.

3. BaseClass' private field can be accessed by a member method of DerivedClass.

No

For BaseClass baseObj = new BaseClass(); in main(), baseObj can access a protected member of BaseClass. Assume main() is defined in a class located in a *different* package as BaseClass.

No

If excptType2 inherits from excptType1, then the inclusion of the second catch block (i.e., catch (excptType2 excptObj)) below the first catch block results in a compiler error as the second catch block would never be executed.

True

If no throw is executed in a try block, then the subsequent catch block is not executed.

True

All classes can access Object's public and protected methods (e.g., toString() and equals()) even if such methods are not explicitly overridden.

True A derived class inherits the methods defined by its base class. Thus, user-defined classes always implement Object's methods.

An item of type ProduceItem may be added to an ArrayList of type ArrayList<GenericItem>.

True ProduceItem is derived from GenericItem. Thus, a ProduceItem object can be implicitly casted to GenericItem and added to such an ArrayList.

The built-in Integer class overrides the toString() method in order to return a String representing an Integer's value.

True The Integer class provides a toString() implementation that is more appropriate for the class's intended use. Instead of returning the Integer instance's type and memory address, toString() returns the Integer's value.

A class that provides default variables.

abstract class

A class that provides default code to other classes that use that class.

abstract class Only abstract classes can provide code to the subclasses. Interfaces provide method declarations but no code implementing methods.

A class that can serve as the basis for another class is called a _____ class.

base A derived class refers to a class that is derived from another class that is known as the base class.

A class that provides only static final fields.

Interface Interfaces can declare public static final fields and don't restrict a class' inheritance.

Assume myItem is declared and initialized as GenericItem, and myProduce as ProduceItem, with classes GenericItem and ProduceItem defined as above.

(quest. 1-4)

Assume public class DerivedClass extends BaseClass {...}

(quest. 1-5)

Chapter 12

-------

Class Dwelling has data members door1, door2, door3. A class House is derived from Dwelling and has data members wVal, xVal, yVal, zVal. How many data members does an initialization House h = new House(); create?

7 4 data members from House itself, plus the 3 data member from the derived class Dwelling.

public

Accessible by self, derived classes, and everyone else.

protected

Accessible by self, derived classes, and other classes in the same package.

private

Accessible by self.

abstract class

An abstract class is a class that cannot be instantiated as an object, but is the superclass for a subclass and specifies how the subclass must be implemented.

If ProduceItem did NOT have its own printItem() method defined, the printItem() method of which class would be called?

GenericItem

myItem.printItem() calls the printItem() method for which class?

GenericItem

A class that provides an API that must be implemented and no other code.

Interface An interface does not restrict future inheritance, so is the best choice if no other code is provided.

Assume that an ArrayList of type ArrayList<Object> called myList contains only three elements of type Double. Is the statement myList.get(0).doubleValue(); valid? Note that the method doubleValue() is defined in the Double class but not the Object class.

No The ArrayList is declared to contain elements of type Object. Thus, the methods a program may invoke on the elements must be defined by the Object class.

myProduce.printItem() calls the printItem() method for which class?

ProduceItem

A checked exception must either be handled via try-catch constructs, or the throwing method must specify that the appropriate exception type may be thrown by appending a throws clause to the method's definition.

True

A goal of exception handling is to avoid polluting normal code with distracting error-handling code.

True

A second catch block can never execute immediately after a first one executes.

True

Consider a program that catalogs the types of trees in a forest. Each tree object contains the tree's species type, age, location, and estimated size based on age. Each species uses a different formula to estimate size based on age. This program will benefit from an abstract class.

True

The JVM automatically performs runtime polymorphism to determine the correct method to call.

True The JVM dynamically determines the correct method to call based on the actual object type to which the variable (or element) refers.

An item of any class type may be added to an ArrayList of type ArrayList<Object>.

True The Object class is the base class for all classes. Thus, an item of any class type is implicitly casted to the Object type and added to the ArrayList.

A compiler generates an error message for the code below. try { System.out.print("Enter length (in inches): "); boardLength = scnr.nextInt(); // Error checking, non-negative length if (boardLength < 0) { throw new Exception("Invalid length."); } } System.out.println("Complete.");

True The try block must be followed by a catch block (or a finally block, which is discussed elsewhere).

1. BaseClass' public member method can be called by a member method of DerivedClass.

Yes

2. BaseClass' protected member method can be called by a member method of DerivedClass.

Yes

For DerivedClass derivedObj = new DerivedClass(); in main(), derivedObj can access a protected member of BaseClass. Assume main() is defined in a class located in the same package as DerivedClass.

Yes

The above program's PrintArrayList() method can dynamically determine which implementation of toString() to call.

Yes Runtime polymorphism enables the Java virtual machine to determine an element's class type and thus call the appropriate implementation of toString().

Provide a statement within printItem() method of the ProduceItem class to call the printItem() method of ProduceItem's base class.

super.printItem();


Set pelajaran terkait

Information Security Fundamentals

View Set

Property & Casualty Insurance Fundamentals

View Set

Social Studies Daily Life in Athens vs Sparta

View Set

Ch 19: Documenting and Reporting

View Set

Chapter 6 Government Based on Study Guide

View Set

Chapter 10 - Acquisition and Disposition of Property, Plant, and Equipment

View Set

Chapter 2 from Intro to Business (ethnics)

View Set

Barrett Chuang Quiz: MDST 1002-581

View Set