CS - Chapter 7 - Array Lists only
What is the value of the count variable after the execution of the given code snippet? ArrayList<Integer> num = new ArrayList<Integer>(); num.add(1); num.add(2); num.add(1); int count = 0; for (int i = 0; i < num.size(); i++) { if (num.get(i) % 2 == 0) { count++; } }
1
What is the output of the following statements? ArrayList<String> cityList = new ArrayList<String>(); cityList.add("London"); cityList.add("New York"); cityList.add("Paris"); cityList.add("Toronto"); cityList.add("Hong Kong"); cityList.add("Singapore"); System.out.print(cityList.size()); System.out.print(" " + cityList.contains("Toronto")); System.out.print(" " + cityList.indexOf("New York")); System.out.println(" " + cityList.isEmpty());
6 true 1 false
Which one of the following statements about declaring an array list as a method parameter is true?
An array list value can be modified inside the method.
What is the output of the following statements? ArrayList<String> names = new ArrayList<String>(); names.add("Bob"); names.add(0, "Ann"); names.remove(1); names.add("Cal"); names.set(1, "Tony"); for (String s : names) { System.out.print(s + ", "); }
Ann, Tony
What is the output of the following code snippet? ArrayList<Integer> num; num.add(4); System.out.println(num.size());
Error because num is not initialized
Which statements are true regarding the differences between arrays and array lists? I. Arrays are better if the size of a collection will not change. II. Array lists are more efficient than arrays. III. Array lists are easier to use than arrays
I, III only
What should you check for when calculating the largest value in an array list?
The array list contains at least one element.
What should you check for when calculating the smallest value in an array list?
The array list contains at least one element.
Consider the following code snippet: public static void check(ArrayList<Integer> chknum1) { int cnt = 0; for(int i = 0; i < chknum1.size(); i++) { if(chknum1.get(i) == 0) { cnt++; } } System.out.println("The total 0 values in the list are: " + cnt); } Which one of the following is true about the check method in the given code snippet?
The check method counts all the elements with value 0 in an array list passed as a parameter to the method.
Which statement is true about the code snippet below? public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Double> inputs = new ArrayList<Double>(); int ind = 0; while (in.hasNextDouble()) { inputs.set(ind, in.nextDouble()); ind++; } }
The code has a run-time error.
Consider the following code snippet, where the array lists contain elements that are stored in ascending order: ArrayList<Integer> list1 = new ArrayList<Integer>(); ArrayList<Integer> list2 = new ArrayList<Integer>(); . . . int count = 0; for (int i = 0; i < list1.size() && i < list2.size(); i++) { if (list1.get(i) == list2.get(i)) { count++; } }
The code snippet compares the values of two array lists and stores the count of total matches found.
Which statement is true about the code snippet below? ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); ArrayList<String> friends = new ArrayList<String>(names); friends.add("Harry");
The final size of names is 2; the final size of friends is 3.
Which statement is true about the code snippet below? ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); ArrayList<String> friends = new ArrayList<String>(names); names.add("Harry");
The final size of names is 3; the final size of friends is 2.
Which statement is true about the code snippet below? ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); ArrayList<String> friends = names; friends.add("Harry");
The final size of names is 3; the final size of friends is 3.
What is the output of the following statements? ArrayList<String> names = new ArrayList<String>(); names.add("Bob"); names.add(0, "Ann"); names.remove(1); names.add("Cal"); names.set(2, "Tony"); for (String s : names) { System.out.print(s + ", "); }
array list bound error
When an integer literal is added to an array list declared as ArrayList<Integer>, Java performs:
auto-boxing
Which statement(s) about the size of a Java array, array list, and string are true? I. The syntax for determining the size of an array, an array list, and a string in Java is consistent among the three. II. The string uses s.size(), while the array list uses a.length(). III. The array uses a.length, which is not a method call.
III only
What is the result of the following definition of an array list? ArrayList<Double> points;
The statement defines an array list reference but does not create an array list.
Java 7 introduced enhanced syntax for declaring array lists, which is termed
diamond syntax.
Which one of the following is the correct code snippet for calculating the largest value in an integer array list arrList of size 10?
int maximum = arrList.get(0); for (int counter = 1; counter < arrList.size(); counter++) { if (arrList.get(counter) > maximum) { maximum = arrList.get(counter); } }