Generics

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

which of the following statements is true?

A) Generic types do not exist at the byte code level. B) A raw type is a class that cannot be extended. C) A raw type is a class that must be extended to be used. D) A generic class cannot be abstract. Answer: A

which of the following statements is true?

A) Interfaces may not be generic in Java B) The keyword implements cannot be used do describe a type parameter that implements an interface C) a generic class may not have more than one type parameter in its declaration D) None of the above statements are true. Answer: B

look at the following method header void displayPoint(Point<? extends Number> myPoint) which of the following objects can be passed as an argument to the displayPoint method?

A) Point<Number> p; B) Point<Integer> p; C) Point <Double> p; D) All of the above Answer: D

look at the following method header: void displayPoint(Point<? super Double> myPoint) which of the following objects can be passed as an argument to the displayPoint method?

A) Point<Number> p; B) Point<Object> p; C) Point<Double> p; D) All of the above Answer: D

which of the following generic type notations uses a wildcard?

A) Point<W> B) Point<T extends Number> C) Point<T super Integer> D) Point<?> Answer: D

which of the following statements is true?

A) The CompareTo method is implemented by the String class B) The CompareTo method is implemented by all the sub classes of the Number class C) The Comparable interface is defined in the Java class libraries D) All of the above statements are true. Answer: D

which of the following statements are true?

A) The compiler translates generic code by replacing generic types with Object, or with the upper bounds of those types B) The compiler often has to insert type casts into code that results from translating generic code C) Generic types do not exist at the byte code level D) All of the above Answer: D

suppose that Point<T> is a generic type, and that have a method of the form void printPoint(Point<Number> p){ //code not shown }

A) We will get a compiler error unless we declare the method abstract B) We may pass any object as an actual parameter for p C) We may only only pass Number objects as parameters for p D) None of the above Answer: D

which of the following statements are true?

A) You cannot instantiate an object of a generic type B) You cannot create arrays whose elements are instances of a generic type C) You can declare references to arrays whose elements are of a generic type D) All of the above Answer: D

the clause catch(Exception<T> e) { } ....

A) can be used to catch exceptions that are parameterized by type B) compiles correctly, but is best avoided because of a high runtime overhead C) compiles correctly, but may throw a ClassCastException of the wrong type is passed D) None of the above Answer: D

the Java notation MyClass<Student> means that...

MyClass is a generic class

Let Point<T>be a generic type. We want to write a method that takes as parameter Point objects whose type parameter is the Number class, or any subclass of Number. We can do this by declaring the type of the method parameter as...

Point<? extends Number>

let Point<E> be a generic type. We want to write a method that takes as parameter Point objects whose type parameter is the Number class, or any superclass of Number. We can do this by writing....

Point<? super Number>

consider a method of the form void printPoint(Point<Number> p){ //code not shown } where Point<T> is a generic type. When the method is called, the only Objects that may be passed for the perimeter p are...

Point<Number> objects only

the declaration class Point<T extends Number>{ } is an example of...

a generic class extending a non-generic class

the class public class Point3D<T extends Number> extends Point<T>{ } is an example of...

a generic class extending another generic class

the generic method: public static void <E extends Number> void displayArray(E[] Array){ system.out.println(element); } can be passed...

an array whose element type is Integer

exceptions of a generic type...

are not permitted in java

when a generic class is instantiated without specifying an actual type argument, the generic class is being used...

as a raw type

the automatic conversion of a primitive type to the corresponding wrapper type when being passed as parameter to a generic class is called...

autoboxing

in the generic method, a type parameter is defined...

before the method's return type

a generic class...

can extend generic and non-generic classes

a static method of a generic class...

cannot refer to any of the type parameters of its class

the declaration ArrayList<int> aL = new ArrayList<int>(); ....

causes a compile-time error

the nation <E extends Comparable<E>>

declares a generic type than implements the Comparable interface

the process used by the Java compiler to remove notation and substitute actual type arguments for formal type parameters is called...

erasure

the process of erasure....

helps generic code coexist with older code written in pre 1.5 versions of Java

a class is generic...

if it has type parameters

an advantage of using generic types is...

increased type-safety without the need to do typecasts at run time

the code fragment class MySArrayList extends ArrayList<String>{ }

is a correct way to extends a class

Comparable<T> is...

is an interface defined in the java class libraries

a generic class...

may not create instances of any of its type parameters

a static field of a generic class...

may not have its type one of the type parameters of the class

when you create an instance of a generic class, what types can you pass as arguments for the class type parameter?

reference types only

Erasure is process of....

replacing generic types with their upper bound, or with Object, during compilation

constraining a type parameter in a generic class...

restricts the types that can be used as type arguments

in the notation<T super Number> the Number class...

specifies a lower bound for the parameter type T

In the notation <T extends Number>, the Number class...

specifies the upper bound for the parameter type T

one of the advantages of using generics is...

that more type of problems can be uncovered at compile-time rather than at run time

when a generic method is called...

the compiler determines the actual types to use for the type parameters from the context

when a generic class with an unconstrained type parameter is instantiated without specifying an actual type argument,...

the type Object is used for the unspecified type

the automatic conversion of a wrapper type to the corresponding primitive type when the wrapper type is assigned to a variable of the primitive type is called...

unboxing

the notation <E implements Comparable<E>>

will be rejected by the Compiler as an error

Consider the class class Value <T extends Number>{ private T v; public Value(T v1) { v = v1; } public void output(){ System.out.println(v); } } The code Value<Number> nV1 = new Value<Double>(34.5);

will cause a compiler error

consider the class class Value <T extends Number>{ private T v; public Value(T v1) {v = v1; } public void output() { System.out.println(v); } } the code Value<Number> nV = new Value<Number>(12);

will compile and run correctly

consider the class class Value <T extends Number>{ private T v; public Value(T v1) { v = v1; } public void output(){ System.out.println(v); } } The code Value<Double> nV1 = new Value<Double>(34.5);

will compile and run correctly

the code ArrayList<String> [ ] a = new ArrayList<String>[100];

will not compile

the code public class MyClass<T>{ public MyClass() { T myObject = new T(); } }

will not compile

a type parameter can be constrained...

with an upper bound and a lower bound, but not at the same time

a reference to a subclass object can be assigned to a superclass variable

with no restrictions on the genericity of the subclass or superclass


Kaugnay na mga set ng pag-aaral

PN Adult Medical Surgical Online Practice 2020 A with NGN

View Set

PM/490: Project Management Capstone Wk 4 - Practice: Week 4 Quick Check

View Set

Spanish 1: Oral Exam Conversation Questions

View Set