AP CS A Chapter 7 MCQ

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What is the low score in [ 8, 8, 4, 3, 3, 6]

3

Consider the following method findValue, which takes an ArrayList of String elements and a String value as parameters and returns true if the String value is found in the list and false otherwise. public static boolean findValue(ArrayList<String> arr, String key) { for (int j = 0; j < arr.size(); j++) // Line 3 { if (arr.get(j).equals(key)) { return true; } } return false; } Which of the following best explains the impact to the findValue method when, in line 3, int j = 0 is replaced by int j = 1 ?

It will cause the method to return a different result when the key value is found only at the first index in the list.

Apple Banana

4

Consider the following method that is intended to modify its parameter nameList by replacing all occurances of name with newValue. public void replace (ArrayList nameList, String name, String newValue) { for(int j = 0; j < nameList.size(); j++) { if( /* expression */ ) { nameList.set(j, newValue); } } } Which of the following can be used to replace /* expression */ so that replace will work as intended? A. nameList.get(j).equals(name) B. nameList.get(j) == name C. nameList.remove(name) D. nameList[j] == name E. nameList[j].equals(name)

A. nameList.get(j).equals(name)

Consider the following statement, which is intended to create an ArrayList namedtheater_club to store elements of type Student. Assume that the Student class has been properly defined and includes a no-parameter constructor.ArrayList theater_club = new /* missing code */;Which choice can replace /* missing code */ so that the statement compiles without error?

ArrayList(Student)

Consider the following statement, which is intended to create an ArrayList named numbers that can be used to store Integer values. ArrayList<Integer> numbers = / missing code /; Which of the following can be used to replace / missing code / so that the statement works as intended? I. new ArrayList() II. new ArrayList<Integer> III. new ArrayList<Integer>()

I and III only

Consider the following method countNegatives, which searches an ArrayList of Integer objects and returns the number of elements in the list that are less than 0. public static int countNegatives(ArrayList<Integer> arr) { int count = 0; for (int j = 0; j < arr.size(); j++) // Line 4 {if (arr.get(j) < 0){count++; } } return count;} Which of the following best explains the impact to the countNegatives method when, in line 4, j < arr.size() is replaced with j <= arr.size() - 1 ?

It has no impact on the behavior of the method.

Consider the following method. public ArrayList<Integer> mystery(int n) { ArrayList<Integer> seq = new ArrayList<Integer>(); for (int k = 1; k <= n; k++) seq.add(new Integer(k * k + 3)); return seq; } Which of the following is printed as a result of executing the following statement? System.out.println(mystery(6));

[4, 7, 12, 19, 28, 39]

Consider the following code segment. List<String> animals = new ArrayList<String(); animals.add("dog"); animals.add("cat"); animals.add("snake"); animals.set(2, "lizard"); animals.add(1, "fish"); animals.remove(3); System.out.println(animals); What is printed as a result of executing the code segment?

[dog, fish, cat]

The following sort method correctly sorts the integers in elements into ascending order. Line 1: public static void sort(int[] elements) Line 2: { Line 3: for (int j = 0; j < elements.length - 1; j++) Line 4: { Line 5: int index = j; Line 6: Line 7: for (int k = j + 1; k < elements.length; k++) Line 8: { Line 9: if (elements[k] < elements[index]) Line 10: { Line 11: index = k; Line 12: } Line 13: } Line 14: Line 15: int temp = elements[j]; Line 16: elements[j] = elements[index]; Line 17: elements[index] = temp; Line 18: } Line 19: } Which of the following changes to the sort method would correctly sort the integers in elements into descending order? I. Replace line 9 with: Line 9: if (elements[k] > elements[index]) II. Replace lines 15-17 with: Line 15: int temp = elements[index]; Line 16: elements[index] = elements[j]; Line 17: elements[j] = temp; III. Replace line 3 with: Line 3: for (int j = elements.length − 1; j > 0; j−−) and replace line 7 with: Line 7: for (int k = 0; k < j; k++) a. I only b. II only c. I and II only d. I and III only e. I, II, and III

d. I and III only

Consider the following method. public static void mystery(List<Integer> nums) { for (int k = 0; k < nums.size(); k++) { if (nums.get(k).intValue() == 0){nums.remove(k); } } } Assume that a List<Integer> values initially contains the following Integer values. [0, 0, 4, 2, 5, 0, 3, 0] What will values contain as a result of executing mystery(values)? a. [0, 0, 4, 2, 5, 0, 3, 0] b. [4, 2, 5, 3] c. [0, 0, 0, 0, 4, 2, 5, 3] d. [0, 4, 2, 5, 3] e. The code throws an ArrayIndexOutOfBoundsException exception.

d. [0, 4, 2, 5, 3]

remDups (7 numbers)

{ 1, 2, 2 3, 3, 4, 5}


Kaugnay na mga set ng pag-aaral

True or false on sampling distribution

View Set

MAN: Ch 10 Organizational Change & Innovation

View Set

Chapter 34: Comfort & Pain (Fund.)

View Set

2.02: Graphing Trigonometric Functions

View Set

Programming in C# 70-483 Real Questions V14.02 | Killtest

View Set