Chapter 17 Generics

¡Supera tus tareas y exámenes ahora con Quizwiz!

9. Which of the following generic type notations uses a wildcard?

<?>

17.18 Can a generic class be a superclass? Can it be a subclass?

A generic class can be a superclass and/or a subclass.

17.17 Can you constrain a generic class's type parameter with an upper bound? With a lower bound?

A generic class's type can be constrained with either an upper bound or a lower bound.

17.13 In generic type notation, does the super key word establish an upper boundary or a lower boundary?

A lower boundary

17.5 What is a type parameter?

A type parameter is an identifier in a generic class declaration. It represents an actual type that will be used when a generic class is instantiated.

17.12 In generic type notation, does the extends key word establish an upper boundary or a lower boundary?

An upper boundary

17.1 Why is it risky to store objects of different types in an ArrayList?

Because it opens an opportunity for the application to throw an exception at run time. If we retrieve an object from the ArrayList, but cast it to the wrong type, an exception occurs

______is commonly used to represent the type of an element, such as an element in an ArrayList or other collection.

E

11. True or False: It is better to discover an error at runtime than at compile time.

False

13. True or False: Type parameters must be single character identifiers, written in uppercase.

False

14. True or False: You can constrain a type parameter to both an upper bound and lower bound.

False

17. True or False: A generic class's type parameter can be the type of a static field.

False

17.2 When ArrayList is used as a non-generic class, why must you use the cast operator with the get method?

In non-generic mode, the get method returns an Object reference. When the get method is called, the compiler does not know the exact type of the object being returned, only that it is an Object. To assign the reference to any other type of variable we have to cast it to the appropriate type.

17.10 In generic type notation, what does < ? extends Number> mean?

It means that any type that extends Number, or is Number, can be passed as a type argument.

17.11 In generic type notation, what does < ? super Integer> mean?

It means that any type that is Integer or a superclass of Integer can be passed as a type argument.

____is commonly used to represent the type of a key in a class that keeps key/value pairs.

K

17.25 Do generic types still exist at the bytecode level?

No

17.8 Is it required that type parameters be single letters written in uppercase?

No, it is not required, but it is the standard convention.

17.14 In generic type notation, can you use both extends and super to constrain a type to a set of boundaries?

No, you cannot use both together.

When a type argument is not provided for an instance of a generic class, Java will provide ______ as the type argument by default.

Object

When the compiler encounters a generic class, interface, or method with an unbound type parameter, such as <T> or <E> , it replaces all occurrences of the type parameter with ________.

Object

17.6 When you create an instance of a generic class, can you pass reference types, primitive types, or either to the class's type parameter?

Only reference types may be passed to a type parameter

3. Look at the following method header: void displayPoint(Point <Number> myPoint) Which of the following objects would we be allowed to pass as an argument to the displayPoint method? (Select all that apply.)

Point<Number> p;

4. Look at the following method header: void displayPoint(Point<?> myPoint) Which of the following objects would we be allowed to pass as an argument to the displayPoint method? (Select all that apply.)

Point<Number> p; Point<Integer> p; Point <Double> p; Point <String> p;

5. Look at the following method header: void displayPoint(Point <? extends Number> myPoint) Which of the following objects would we be allowed to pass as an argument to the displayPoint method? (Select all that apply.)

Point<Number> p; Point<Integer> p; Point <Double> p;

17.3 Suppose we use the following statement to instantiate the ArrayList class. What type of objects will we be allowed to store in the ArrayList? ArrayList <Rectangle> myList = new ArrayList<>();

Rectangle objects

______is commonly used for a general type when T has already been used.

S

____ is commonly used for a general type.

T

17.9 In generic type notation, what does the ? character (the type wildcard) indicate?

That any type argument can be used in its place.

17.16 Why would you want to constrain the type parameter in a generic class?

To prevent the class from being instantiated with a type argument that does not support some operation that the class is performing.

12. True or False: It is possible to instantiate a generic class without specifying a type argument.

True

15. True or False: A generic class can extend a non-generic class

True

16. True or False: You cannot create an array of generic class objects.

True

18. True or False: An exception class cannot be generic.

True

________ is the opposite of boxing. It is the process of automatically converting a wrapper class object to a primitive type, when needed.

Unboxing

_____is commonly used to represent the type of a value in a class that keeps key/value pairs.

V

17.24 During the process of erasure, when the compiler encounters a class, interface, or method with a bound type parameter, such as <T extends Numbers> or <E extends Comparable> , what does it replace the type parameter with?

When the compiler encounters a class, interface, or method with a bound type parameter, such as <T extends Numbers> or <E extends Comparable>, it replaces all occurrences of the type parameter with the bound that is applied to the parameter.

17.23 During the process of erasure, when the compiler encounters a generic class, interface, or method with an unbound type parameter, such as <T> or <E> , what does it replace the type parameter with?

When the compiler encounters a generic class, interface, or method with an unbound type parameter, such as <T> or <E> , it replaces all occurrences of the type parameter with Object.

1. 17.15 When an instance of a generic class is created, a type argument is explicitly passed to the class's type parameter. When a generic method is called, how is a type argument provided for the method's type parameter?

When you call a generic method, the compiler determines which type to use from the context in which you are using the method.

17.20 Can you mix generic and non-generic classes in a class hierarchy?

Yes

17.21 Can interfaces be defined as generic?

Yes

17.22 Assume that you must define the type parameter T in a generic method. How would you constrain the type parameter T so it only accepts type arguments that implement the Comparable interface?

Yes

17.7 Is it possible to create an instance of a generic class without providing a type argument? If so, is this recommended?

Yes, it is possible. Doing so instantiates the class as a raw type. This is not recommended, however, because you give up the benefits of type-safety that you would have otherwise.

17.19 A generic class extends another generic class. Can you assign a reference to an instance of the subclass to a superclass variable?

Yes. The "is-a" relationship is in effect between the classes.

6. Look at the following method header: void displayPoint(Point<? super Double> myPoint) Which of the following objects would we be allowed to pass as an argument to the displayPoint method? (Select all that apply.) a. Point<Number> p; b. Point<Integer> p; c. Point <Double> p; d. Point <String> p;

a and c

8. In the generic type notation < T super Integer> to what type of bound is T constrained?

a lower bound

7. In the generic type notation <T extends Number> to what type of bound is T constrained?

an upper bound

_______ is Java's process of automatically "boxing up" a primitive value inside an object of an appropriate wrapper class.

autoboxing

Type parameter definitions in a generic method must appear just ______ the method's return type.

before

2. In a generic method, a type parameter is defined ________.

before the method's return type

You _____ make an exception class generic.

cannot

You _______ create an array of generic class objects.

cannot

You_______ create an instance of a type parameter.

cannot

Another reason that we should avoid instantiating a generic class without providing a type argument is that we have to use a ____ ______ to assign an Object reference to a variable of any other type.

cast operator

Java allows you to use the ______ ______to simplify the instantiation of a generic class.

diamond operator ( <> )

10. The process used by the Java compiler to remove generic notation and substitute an actual type for type parameters is known as

erasure

The process is called _____ because the compiler erases the generic notation and substitutes an actual type for each type parameter.

erasure

When the Java compiler encounters generic code, it uses a process known as _________ to compile the code.

erasure

generic notation _______ only in the source code that is written by the programmer. Once the compiler confirms that a generic type is being used safely, it converts it into a raw type, which is simply a non-generic type. At the bytecode level, only raw types exist.

exists

The ______Key Word Constrains a Type to an Upper Bound

extends

To constrain a type parameter to types that implement a particular interface, you use the _________ key word

extends

In generic notation, the extends key word means "_______" or "_________."

extends, implements

A_______ class or method is one that permits you to specify the allowable types of objects that the class or method may work with.

generic

The Java compiler will not allow you to create an array of objects that are instances of a ____ ______

generic type

In generic notation, the key word extends also means "__________."

implements

You do not use the ______ key word in generic type notation.

implements

Both generic and non-generic classes may be used together in an ______ hierarchy,

inheritance

By convention, however, type parameters are usually single ______written in uppercase. This helps distinguish the name of a type parameter from the name of a class.

letters

When you create an instance of a generic class, you can _____ pass a reference type to the class's type parameter.

only

When you instantiate a generic class without passing a type argument, you are using the class as a ______ ______.

raw type

Generic classes and methods can accept only_____ ______ as type arguments.

reference types

1. When you create an instance of a generic class, what types can you pass as arguments to the class's type parameter?

reference types only

A generic class's type parameter cannot be the type of a________ field, and cannot be referred to in a _____ method.

static, static

The _______Key Word Constrains a Type to a Lower Bound

super

Although it is not recommended, it is possible to create an instance of a generic class without providing a ______ _________. When you do this, you are using the class as a raw type.

type argument

If a generic method has its own type parameter, you must write a definition for the ______ _______ in the method header.

type parameter

It is not recommended that you instantiate a generic class without providing a type argument, because you give up the benefits of ____ -______ that you would have otherwise.

type-safety

The only way to use a generic type to hold a primitive value is to _____ the primitive value up in a wrapper class

wrap


Conjuntos de estudio relacionados

Hoorcollege 1 - Inleiding ethiek

View Set

Fair and Equal Credit and Lending Laws: Quiz 1

View Set

What are animals and how was they related to other forms of life?

View Set

Ch 3.2 "How are Minerals Identified?"

View Set

money and banking exam #2 chapter 14

View Set

Incorrect Logical reasoning questions

View Set