Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Generics and ArrayList Class in Java - Prof. Susanna Wei, Study notes of Computer Science

Multiple-choice and short answer questions about generics and the arraylist class in java, including concepts such as type parameters, methods, and memory management. It also includes example java statements for creating and manipulating arraylists.

Typology: Study notes

Pre 2010

Uploaded on 08/16/2009

koofers-user-m4s
koofers-user-m4s 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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”);
pf3
pf4

Partial preview of the text

Download Generics and ArrayList Class in Java - Prof. Susanna Wei and more Study notes Computer Science in PDF only on Docsity!

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);

  1. 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()
  2. All of the following are methods of the ArrayList class except: (a) clear() (b) resize() (c) contains() (d) trimToSize()
  3. The remove(Object theElement) method of the ArrayList class returns: (a) Int (b) Double (c) Byte (d) Boolean
  4. All of the following are search methods of the ArrayList class except: (a) isEmpty() (b) lastIndexOf() (c) indexOf() (d) contains()
  5. All of the following are memory management methods of the ArrayList class except: (a) ensureCapacity() (b) clear()

Answer: Write Java statements that use an enhanced loop to cycle through all the elements in an ArrayList of doubles named grades.

  1. 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.
  2. 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.