Chapter 14: Generics And The Arraylist Class

Total Page:16

File Type:pdf, Size:1020Kb

Chapter 14: Generics And The Arraylist Class

Chapter 14: Generics and the ArrayList Class

Multiple Choice

)1 Class and method definitions that include parameters for types are called: ()a generics ()b ArrayList ()c Base type ()d None of the above

)2 A/An ______can change the length while the program is running. ()a Array ()b ArrayList ()c Type parameter ()d None of the above )3 To use the Java class ArrayList, you must import the package: ()a java.awt ()b java.io ()c java.util ()d java.container )4 All of the following are methods of the ArrayList class except: ()a length() ()b size() ()c set() ()d get() )5 Which is the correct syntax for placing the string “boat” into an ArrayList name recVehicles in position 3 for the first time? ()a recVehicles.set(3, “boat”); ()b recVehicles.set(“boat”, 3); ()c recVehicles.add(3, “boat”); ()d recVehicles.add(“boat”, 3); )6 To place an element in an ArrayList position for the first time, you usually use the method: ()a put() ()b set() ()c add() ()d place() )7 All of the following are methods of the ArrayList class except: ()a clear() ()b resize() ()c contains() ()d trimToSize() )8 The remove(Object theElement) method of the ArrayList class returns: ()a Int ()b Double ()c Byte ()d Boolean )9 All of the following are search methods of the ArrayList class except: ()a isEmpty() ()b lastIndexOf() ()c indexOf() ()d contains() )10 All of the following are memory management methods of the ArrayList class except: ()a ensureCapacity() ()b clear() ()c isEmpty() ()d trimToSize() )11 When using ArrayLists the method ______can be used to save memory: ()a trimToSize() ()b trimDown() ()c trimOff() ()d None of the above )12 The Java standard libraries have a class named ______that behaves almost exactly the same as the class ArrayList. ()a Array ()b Container ()c Vector ()d All of the above )13 A class obtained from a generic class by plugging in a type for the type parameter is said to ______the generic class. ()a Inherit ()b Encapsulate ()c Instantiate ()d None of the above

 True/False )1 An ArrayList object has a fixed size. )2 You can use any primitive type as the base type of an ArrayList class. )3 ArrayList objects do not have the array square-bracket notation. )4 You can not use the set method to put an element at just any index. )5 Inheritance is the reason why some parameters of the ArrayList class are of type Base_Type and others of type Object.

 Short Answer/Essay What are the three main disadvantages of an ArrayList? )1 Write a Java statement to create an ArrayList to hold 25 integers. )2 What is the base type of the ArrayList defined in question number 2? )3 Write Java statements to initialize the objects in the ArrayList created in question number 2 above to -1. )4 Write Java statements to add the integers 1001, 1100, and 1110 to the resulting ArrayList from question 4 above. )5 Write Java statements to display the size of the resulting ArrayList from question 5 above. Answer:System.out.println(list.size()); Answer: Write Java statements that use an enhanced loop to cycle through all the elements in an ArrayList of doubles named grades. )6 Write a Java method named average. The method should take an ArrayList of Integers as a parameter, compute the average of the elements in the ArrayList and return the average to the calling method. )7 Write a complete Java program that prompts the user to create a list of the following types: String, Integer and Double. Your program should create the appropriate list type as determined by the user.

Recommended publications