Chapter 14 Generics and the ArrayList Class – absolute java test bank
Download file with the answers
Chapter 14 Generics and the ArrayList Class - absolute java test bank
1 file(s) 86.84 KB
Not a member!
Create a FREE account here to get access and download this file with answers
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
14) A generic class can be instantiated as any of the following base types except:
(a) Character
(b) Array
(c) String
(d) Double
15) The keyword ___________ can be used to place a bound on a type parameter.
(a) Implements
(b) Inherits
(c) Encapsulates
(d) Extends
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.
6) A for loop is an enhanced loop.
7) The Type parameter is always indicated as a T.
8) The compiler option –Xlint is good for debugging.
9) The definition of a parameterized class is a class with one or more type parameters.
10) You can plug in a primitive type for a type parameter.
11) A class definition can have more than one type parameter.
Short Answer/Essay
1) What are the three main disadvantages of an ArrayList?
Answer: 1) They are less efficient than arrays; (2) they do not have the square bracket notation, and so using an ArrayList is sometimes notationally more awkward than using ordinary arrays; and (3) the base type of an ArrayList must be a class type (or other reference type); it cannot be a primitive type, such as int, double, or char.
2) Write a Java statement to create an ArrayList to hold 25 integers.
Answer: ArrayList list = new ArrayList(25);
3) What is the base type of the ArrayList defined in question number 2?
Answer: Integer
4) Write Java statements to initialize the objects in the ArrayList created in question number 2 above to -1.
for(int i=0; i < 25; i++)
list.add(-1);
5) Write Java statements to add the integers 1001, 1100, and 1110 to the resulting ArrayList from question 4 above.
list.set(0, 1001);
list.set(1, 1100);
list.set(2, 1110);
6) Write Java statements to display the size of the resulting ArrayList from question 5 above.
Answer: System.out.println(list.size());
7) Write Java statements that use an enhanced loop to cycle through all the elements in an ArrayList of doubles named grades.
for(Double grade : grades)
System.out.println(grade);
8) What is a parameterized class?
Answer: A parameterized class has a parameter that can be replaced by any reference type to obtain a class with the specified base type.
9) 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.
public static int average(ArrayList list)
{
int sum = 0;
for(int element : list)
sum += element;
return (sum/list.size());
}
10) 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.
import java.util.*;
public class FunWithLists
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int listChoice;
ArrayList<?> list;
String listElement;
//Display a list of list choices for the user
System.out.println(“Enter the number of the list you wish to create:”);
System.out.println(” 1. String”);
System.out.println(” 2. Integer”);
System.out.println(” 3. Double\n\n”);
listChoice = sc.nextInt();
switch(listChoice)
{
case 1:
list = new ArrayList(100);
break;
case 2:
list = new ArrayList(100);
break;
case 3:
list = new ArrayList(100);
break;
default:
System.out.println(“Invalid choice”);
break;
}
}
}
11) Rewrite the following line of code, but use type inference.
ArrayList mylist = new ArrayList();
Answer: ArrayList myList = new ArrayList<>();
Leave a reply