Beginning Java with NetBeans: Chapter 14 How to work with collections, generics,
ID: 3738119 • Letter: B
Question
Beginning Java with NetBeans: Chapter 14
How to work with collections, generics, and lambdas
MULTIPLE CHOICE [Answers are in tables – delete all but the correct answer’s cell]
1. Which of the following is not a difference between arrays and collections?
a. Collections can grow in size, but arrays can’t.
b. Arrays can store unwrapped primitive types, but collections can’t.
c. Arrays can store user-defined objects, but collections can’t.
d. Collections are created from classes, but arrays aren’t.
2. Which of the following is not true of generics?
a. It can restrict the objects that are stored in a collection to a specific type.
b. It can be used to create collections that store primitive types.
c. It uses angle brackets (<>) to specify type information.
3. Which of the following statements creates an array list that can store double values?
a. ArrayList<double> sales = new ArrayList<>();
b. ArrayList sales = new ArrayList<Double>();
c. ArrayList<Double> sales = new ArrayList();
d. ArrayList<Double> sales = new ArrayList<>();
4. Which of the following statements can be used to create an array list prior to Java 7?
a. ArrayList<double> sales = new ArrayList<>();
b. ArrayList sales = new ArrayList<Double>();
c. ArrayList<Double> sales = new ArrayList<Double>();
d. ArrayList<Double> sales = new ArrayList<>();
5. To change the size of an array list, you must
a. code a statement that specifies the new number of elements
b. code a statement that specifies the percent by which you want the size increased
c. either a or b
d. none of the above
6. What is the initial capacity of the array list that follows?
ArrayList<Integer> quantities = new ArrayList<>(20);
a. 10
b. 20
c. 31
d. 40
e. 41
7. What is the value of s after the following code is executed?
ArrayList<String> inventory = new ArrayList<>();
String item = "Computer";
inventory.add(item);
inventory.add("Printer"); inventory.add("Monitor");
b. “Printer”
c. “Computer”
d. “ ”
8. What is printed to the console when the code that follows is executed?
ArrayList<String> passwords = new ArrayList<>();
passwords.add("akjdk12");
passwords.add("buujl32");
passwords.add("chrcl92");
passwords.add("nnnii87");
passwords.set(2, "cb2kr45");
passwords.remove("akjdk12");
System.out.println(passwords.get(1));
a. buuj132
b. akjdk12
c. cb2kr45
d. nnnii87
9. What is the value of m after the code that follows is executed?
ArrayList<Double> miles = new ArrayList<>();
miles.add(37.5);
miles.add(26.2);
miles.add(54.7);
double m = miles.remove(0);
b. 26.2
c. 54.7
10. If an array list has 500 elements, how many elements will need to be moved to insert an element into position 100?
a. 0
b. 100
c. 2
d. 400
11. What happens when the code that follows is executed?
LinkedList<Integer> units = new LinkedList<>();
units.add(8);
units.add(12);
units.add(3);
System.out.println(units.get(1));
a. 8 is printed to the console.
b. 12 is printed to the console.
c. A runtime error occurs because the add method can’t wrap the int value in the Integer class.
d. A compile-time error occurs due to a syntax error.
12. Lambda expressions
a. Allow you to treat data like interfaces.
b. Make stack traces easier to understand.
c. Can allow you to write methods that are more flexible and easier to maintain.
d. Work well with the integrated debugger.
COMPLETION
13. The collection framework is made up of two class hierarchies named Collection and ________.
14. Prior to Java 7, you needed to code the type for a typed collection twice to create the collection. With Java 7 and later, you can use the ____________ operator instead of coding the type a second time.
15. The ArrayList class uses an ______________ internally to store the elements in the list.
16. Java uses a technique called ______________ to automatically add wrapper classes for primitive types whenever necessary.
17. Collections that store one or more key-value pairs are called ________.
18. A/an ________________ interface is an interface that only contains one method.
19. A/an ________________ can be passed as an argument to a method that accepts a functional interface as a parameter.
20. The Predicate interface uses _____________ to specify the type of object that’s passed to its test method.
Explanation / Answer
As per Chegg policy, I am answering only first 4 questions. Kindly upload remaining questions further in order to get them answered.
1.) option c is correct. i.e., Arrays can store user-defined objects, but collections can’t.
2.) option b is correct i.e., It can be used to create collections that store primitive types.
3.) ArrayList<Double> sales = new ArrayList<>(); Therefore, option d is correct.
4.) ArrayList<Double> sales = new ArrayList<Double>(); Therefore, option c is correct.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.