Midterm Java II

Ace your homework & exams now with Quizwiz!

An entry in a map a. is called a mapping b. associates a key with a value c. may not share a key value with any other entry in the map d. All of the above

All of the above

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

All of the above

Which of the following is true? a. Both the Set and List interfaces extend the Collection interface b. Both the Set and Map interfaces extend the Collection interface c. Both the List and Map interfaces extend the Collection interface d. None of the above

Both the Set and List interfaces extend the Collection interface

Look at the following code: FileInputStream fstream = new FileInputStream(MyInfo.dat); DataInputStream inputFile = new DataInputStream(fstream); This code can also be written as: a. FileInputStream inputFile = new FileInputStream(new DataInputStream(MyInfo.dat)); b. DataInputStream inputFile = new DataInputStream(new FileInputStream(MyInfo.dat)); c. FileInputStream fstream = new DataInputStream(InputFile.txt); d. DataInputStream inputFile = new DataInputStream(InputFile.txt);

DataInputStream inputFile = new DataInputStream(new FileInputStream(MyInfo.dat));

To read data from a binary file you create objects from the following classes: a. FileInputStream and DataInputStream b. File and PrintWriter c. File and Scanner d. BinaryFileReader and BinaryDataReader

FileInputStream and DataInputStream

The exception classes are in packages in the ________ a. Java API b. JVM c. Compiler d. Ex class

Java API

Which of the following is true? a. The ListIterator interface is a subinterface of the Iterator interface b. The ListIterator interface is a superinterface of the Iterator interface c. The ListIterator interface extends the List interface d. None of the above

The ListIterator interface is a subinterface of the Iterator interface

If the IOData.dat file does not exist, what will happen when the following statement is executed? RandomAccessFile randomFile =new RandomAccessFile(IOData.dat", rw); a. A FileNotFoundException will be thrown. b. An IOExcepton will be thrown. c. The file IOData.dat will be created. d. This is a critical error, the program will stop execution.

The file IOData.dat will be created.

All exceptions are instances of classes that extend this class. a. RunTimeException b. Throwable c. Error d. Exception

Throwable

An object that is used to retrieve objects from a collection is called a. an iterator b. a collector c. an accessor d. All of the above

an iterator

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 a. Point<Number> b. Point<? super Number> c. Point<? extends Number> d. Point<? sub Number>

c. Point<? extends Number>

If the code in a method can potentially throw a checked exception, then that method must: a. handle the exception b. have a throws clause listed in the method header c. neither A nor B d. either A or B

either A or B

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 a. autoboxing b. unboxing c. type casting d. autoconversion

unboxing

A type parameter can be constrained a. with an upper bound and a lower bound, but not at the same time b. with both an upper and a lower bound at the same time c. with an upper bound only d. with a lower bound only

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

Which of the following is true? a. A set allows duplicate values to be stored b. A map allows duplicate keys to be stored c. No Java collection allows duplicate values to be stored d. A list allows duplicate values to be stored

A list allows duplicate values to be stored

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<?>

Point<?>

Which of the following is true? a. The retrieve() method of an iterator can only be called after isEmpty() has returned false b. The remove() method of an iterator can only be called after next() has been called c. Any iterator can move forward as well as backwards through a collection d. Once an iterator has reached the end of the collection, calling any of its methods will throw a NoSuchElement exception

The remove() method of an iterator can only be called after next() has been called

A collection whose elements are pairs of keys and values is called a. an enumerator b. a map c. a hash list d. a paired collection

a map

If you try to add an item to an ArrayList whose size is equal to its capacity a. an exception that indicates that the ArrayList is full is thrown b. a new ArrayList object with twice the capacity is created, and the elements are moved to this new ArrayList. c. The method for adding the new item returns false d. The method for adding the new item returns null

a new ArrayList object with twice the capacity is created, and the elements are moved to this new ArrayList.

The IllegalArgumentException class extends the RuntimeException class, and is therefore: a.a checked exception class b. an unchecked exception class c. never used directly d. none of the above

an unchecked exception class

When a generic class is instantiated without specifying an actual type argument, the generic class is being used a. to enhance the efficiency of the program b. as a raw type c. as an abstract class d. as an interface

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 a. type promotion b. type wrapping c. autoconversion d. autoboxing

autoboxing

What will be the result of the following statements? FileInputStream fstream =new FileInputStream(DataIn.dat); DataInputStream inFile =new DataInputStream(fstream); a. The inFile variable will reference an object that is able to read only text data from the Input.dat file. b. The inFile variable will reference an object that is able to read binary data from the Input.dat file. c. The inFile variable will reference an object that is able to write binary data to the Input.dat file. d. The inFile variable will reference an object that is able to read random access data from the Input.dat file.

b

The declaration ArrayList<int> aL = new ArrayList<int>(); a. allows the programmer to create an ArrayList that holds integer types b. compiles correctly, but causes an exception to be thrown at run time c. causes a compile-time error d. compiles and runs correctly

causes a compile-time error

If your code does not handle and exception when it is thrown, this prints an error message and crashes the program. a. Java error handler b. multi-catch c. default exception handler d. try statement

default exception handler

A(n) ________ is an object that is generated in memory as the result of an error or an unexpected event. a. exception handler b. exception c. default exception handler d. error message

exception

All of the exceptions that you will handle are instances of classes that extend this class. a. RunTimeException b. IOException c. Error d. Exception

exception

An exception's default error message can be retrieved using this method. a. getMessage() b. getErrorMessage() c. getDefaultMessage() d. getDefaultErrorMessage()

getMessage()

An advantage of using generic types is: a. lower compile-time overhead b. increased portability of Java programs c. increased type-safety without the need to do typecasts at run time d. faster execution of programs that use generics

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

A hashcode of an object a. is the value returned when you store the object into a HashSet collection b. is an integer that can be used to characterize an object and help identify it c. is useful when storing objects in HashMap, HashSet, and List collections d. None of the above

is an integer that can be used to characterize an object and help identify it

A Java collection a. is a class that is defined in the java.awt package b. is an abstract class in the java.collect package c. is an object that is used as a container for other objects d. None of the above

is an object that is used as a container for other objects

When an exception is thrown: a. it must always be handled by the method that throws it b. the program terminates even if the exception is handled c. it must be handled by the program or by the default exception handler d. it may be ignored

it must be handled by the program or by the default exception handler

A collection that stores its elements in an (ordered) sequence, and allows access to each element by its position in the sequence is called a a. map b. positional map c. ordered set d. list

list

The three major categories of Java collections are a. lists, sets, and maps b. hash lists, hash tables, and sets c. sets, collections, and maps d. tree sets, list sets, and hash maps

lists, sets, and maps

When you create an instance of a generic class, what types can you pass as arguments for the class type parameters? a. primitive types only b. reference types only c. interface types only d. primitive, reference, and interface types

reference types only

Constraining a type parameter in a generic class a. causes programs to compile faster b. was added to Java in version 1.3 of the language c. can only be used when the generic class will be used as a superclass for other classes d. restricts the types that can be used as type arguments

restricts the types that can be used as type arguments

A collection that does not impose a positional order on its elements, and does not allow duplicates is called a a. set b. linked list c. non-positional map d. hash map

set

In the notation <T extends Number>, the Number class a. specifies an upper bound for the parameter type T b. specifies a lower bound for the parameter type T c. specifies both an upper and lower bound for the type T d. None of the above

specifies an upper bound for the parameter type T

One of the advantages of using generics is a. that more type problems can be uncovered at compile-time rather than at run time b. that programs that use generics are smaller when translated to byte code c. that program that use generics execute faster than programs that do not d. that programs that use generic code require less effort in design and development

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

When a generic method is called a. the programmer must explicitly specify the actual types to use for the type parameters b. the compiler determines the actual types to use for the type parameters from the context c. the compiler uses information explicitly specified by the programmer, if available; otherwise, the type defaults to Object d. None of the above

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

This is a section of code that gracefully responds to exceptions when they are thrown. a. Thrown class b. Default exception handler c. Exception d. Exception handler

Exception handler

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 superclass of Number. We can do this by writing a. Point<Number> b. Point<? super Number> c. Point<? extends Number> d. Point<? sub Number>

Point<? super Number>

An object that permits a pipeline of operations to be applied to a sequence of elements drawn from a source is called a. an operator pipeline b. a stream c. a sequential operator d. a flow

a stream

The default implementation of hashCode in Java returns a. a value determined from the class type of the object b. a value derived from the memory address of the object c. a value determined from the values of the fields in the object d. None of the above

a value derived from the memory address of the object

The ListIterator interface a. specializes the Iterator interface to work with lists, but adds no new methods b. adds to Iterator the ability to move backwards in the collection c. adds to Iterator the ability to iterate over several collections, if those collections are lists d. None of the above

adds to Iterator the ability to move backwards in the collection

A generic class a. can only extend a class that is also generic b. can extend generic and non-generic classes c. can only extend a non generic class d. cannot extend any class

can extend generic and non-generic classes

When a generic class with an unconstrained type parameter is instantiated without specifying an actual type argument, a. the type Object is used for the unspecified type b. the compiler generates an error c. the computer throws a ClassCastException d. None of the above

the type Object is used for the unspecified type

To write data to a binary file you create objects from the following classes a. File and PrintWriter b. File and Scanner c. FileOutputStream and DataOutputStream d. BinaryFileWriter and BinaryDataWriter

FileOutputStream and DataOutputStream

The concrete classes that implement the List interface are a. AbstractList, AbstractSequentialList, and List b. LinkedList, ArrayList, and AbstractList c. LinkedList and ArrayList d. None of the above

LinkedList and ArrayList

When writing a string to a binary file or reading a string from a binary file, it is recommended that you use: a. Methods that use UTF-8 encoding b. The Scanner class methods c. The FileReader and Writer class methods d. The System.In and System.Out methods

Methods that use UTF-8 encoding

The Java notation MyClass<Student> means that a. the contents of Student are greater than the contents of MyClass b. the contents of Student are included in the contents of each MyClass object c. MyClass is a generic class d. MyClass is a subclass of Student

MyClass is a generic class

The numeric classes' parse methods all throw an exception of this type if the string being converted does not contain a convertible numeric value. a. NumberFormatException b. ParseIntError c. ExceptionMessage d. FileNotFoundException

NumberFormatException

In a generic method, a type parameter is defined a. inside the parentheses, along with the method's parameter variables. b. after the method's return type, and before the method's name c. before the method's return type d. inside the body of the method, but before any local variables are declared

before the method's return type

The following catch statement can: catch (Exception e) {...} a. handle all exceptions that are instances of the Exception class, but not a subclass of Exception b. handle all throwable objects by using polymorphic reference as a parameter in the catch clause c. handle all exceptions that are instances of the Exception class or a subclass of Exception d. is an error since no objects are instantiated in the Exception class

handle all exceptions that are instances of the Exception class or a subclass of Exception

A class is generic a. if it is a subclass of the Object class b. if it is a superclass of the Object class c. if it has type parameters d. if it has method parameters

if it has type parameters

An index a. is a list of keys that allows fast look up of values in a map b. is an object that indicates which one of a group of collections contains a given value c. is the position of an element within a list d. is used with generic collections to quickly find stored values

is the position of an element within a list

The ability to catch multiple types of exceptions with a single catch is known as ________, and was introduced in Java 7. a. multi-catch b. super catch c. exception trapping d. compound catch

multi-catch

In a try/catch construct, after the catch statement is executed: a. the program returns to the statement following the statement in which the exception occurred b. the program terminates c. the program resumes at the statement that immediately follows the try/catch construct d. the program resumes at the first statement of the try statement

the program resumes at the statement that immediately follows the try/catch construct.

A LinkedList is the right kind of list to use when a. most of the elements added to the list are being added at the end b. there are lots of insertions and deletions in the middle of the list c. elements are added to the list in groups of twos, threes, or more d. each element added is a reference to another item

there are lots of insertions and deletions in the middle of the list

When the next() method of Iterator is called at the end of a collection, it a. throws NoSuchElementException b. returns null c. wraps around to the beginning of the collection and returns the first element d. returns the a copy of the last element returned

throws NoSuchElementException


Related study sets

Chapter 11 Exception and Advance File I/O (Review)

View Set

MAP review questions nervous tissue

View Set

Verbal Section - GMAT; Manhattan Prep

View Set

10年文法不白學48-was和were的否定句

View Set

PA/AP axial projection: ulnar deviation, scaphoid series, rafert-long method

View Set

Chapter 20: Consumer Choice and Elasticity

View Set

sociology outlines for chapters 3-6

View Set

Module 1c: Cybersecurity Threats, Vulnerabilities and Attacks

View Set