CSA CB 3 Unit 3 (ArrayList )

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/19

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 9:17 PM on 2/16/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

20 Terms

1
New cards

Consider the following calculate method.
 

public double calculate(double x)
{
   return x + 1.5;
}

 

The following code segment calls the method calculate in the same class.

 

Double d1 = new Double(7.5);
System.out.println(calculate(d1));

What, if anything, is printed when the code segment is executed?

Responses

A

8.5

B

9

C

9.0

D

Nothing is printed because the code does not compile. The actual parameter d1  passed to calculate is a Double, but the formal parameter x is a double.

C. 9.0

2
New cards

Which of the following statements will store the value  34.0  in the  double  variable  value?

Responses

A

double value = (double) "34";

B

double value = Double.parseDouble("34");

C

double value = Integer.parseInt("34") + ".0";

D

double value = Double.parseDouble("34") + ".0";

double value = Double.parseDouble("34");

3
New cards

A text file named  grades.txt  has the following contents.

 

89 92 90 88 79 85 77 

 

In the following code segment, a valid  Scanner  object named  scan  is created to read from the text file. The code segment is intended to store the values from  grades.txt  into an  int  array.

 

File gradeFile = new File("grades.txt"); 
Scanner scan = new Scanner(gradeFile); 

int[] reportCard = new int[7]; 
for (int i = 0; i < reportCard.length; i++)  
{ 
   int currentGrade = /* missing expression */ ; 
   reportCard[i] = currentGrade; 
} 

Which of the following can replace  /* missing expression */  so that the code segment works as intended?

Responses

A

Double.parseDouble(scan.next())

B

Integer.parseInt(scan.next())

C

Integer.parseInt(scan.nextInt())

D

Integer.parseInt(scan.nextLine())

Integer.parseInt(scan.next())

4
New cards

Consider the following code segment.

 

double val1 = 10.0;
Double val2 = 20.0; 
Double val3 = 30.0;
System.out.println(val1 + val2 + val3);

What, if anything, is printed when the code segment is executed?

Responses

A

60.0

B

10.050.0

C

10.020.030.0

D

Nothing is printed due to a syntax error.

60.0

5
New cards

Consider the following method.

 

public static int timesTwo(int n) 
{ 
   return n * 2; 
} 

 

The following code segment appears in a method in the same class as the  timesTwo  method.

 

Integer val = 10; 
int result1 = timesTwo(val); 
Integer result2 = result1; 
System.out.print(result2); 

What, if anything, is printed as a result of executing the code segment?

Responses

A

10

B

20

C

Nothing is printed; the code segment will not compile because  timesTwo  cannot accept an  Integer  argument.

D

Nothing is printed; the code segment will not compile because the  int  variable  result1  cannot be assigned to the  Integer  variable  result2.

20

6
New cards

In the following code segment, assume that the  ArrayList  object  numbers  has been properly declared and initialized to contain  [0, 2, 4, 5].

 

for (int k = numbers.size() - 1; k >= 0; k--) 
{ 
   if (numbers.get(k) > k) 
   { 
      System.out.print(k + " "); 
   } 
} 

What, if anything, is printed as a result of executing the code segment?

Responses

A

2 4 5 

B

3 2 1 

C

5 4 2 

D

Nothing is printed because an  IndexOutOfBoundsException  is thrown.


3 2 1

7
New cards

Consider the following method.

A 10-line code segment reads as follows. Line 1: public static void mystery, open parenthesis, List, open angular bracket, Integer, close angular bracket, nums, close parenthesis. Line 2: open brace. Line 3: for, open parenthesis, int k equals 0, semicolon, k less than nums, dot, size, open parenthesis, close parenthesis, semicolon, k, plus, plus, close parenthesis. Line 4: open brace. Line 5: if, open parenthesis, nums, dot, get, open parenthesis, k, close parenthesis, dot, int Value, open parenthesis, close parenthesis, equals, equals 0, close parenthesis. Line 6: open brace. Line 7: nums, dot, remove, open parenthesis, k, close parenthesis, semicolon. Line 8: close brace. Line 9: close brace. Line 10: close brace.


Assume that a List<Integer> values initially contains the following Integer values.

An array reads as follows: open square bracket, 0 comma 0 comma 4 comma 2 comma 5 comma 0 comma 3 comma 0, close square bracket.


What will values contain as a result of executing mystery(values) ?

Responses

A

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

B

[4, 2, 5, 3]

C

[0, 4, 2, 5, 3]

D

The code throws an ArrayIndexOutOfBoundsException exception.


[0, 4, 2, 5, 3]

8
New cards

Consider the following method, which is intended to return a list containing the elements of the parameter myList with all even elements removed.

public static ArrayList<Integer> removeEvens(ArrayList<Integer> myList)

{

for (int i = 0; i < myList.size(); i++)

{

if (myList.get(i) % 2 == 0)

{

myList.remove(i);

}

}

return myList;

}

Which of the following best explains why the code segment does not work as intended?

Responses

A

The code segment causes an IndexOutOfBoundsException for lists with at least one even element because the indexes of all subsequent elements change by one when a list element is removed.

B

The code segment returns a list with fewer elements than intended because it fails to consider the last element of myList.

C

The code segment removes the wrong elements of myList because the condition in the if statement to test whether an element is even is incorrect.

D

The code segment skips some elements of myList because the indexes of all subsequent elements change by one when a list element is removed.

The code segment skips some elements of myList because the indexes of all subsequent elements change by one when a list element is removed.

9
New cards

The following method is intended to remove all elements from  words  whose length is greater than  max.

 

/** Precondition: All elements of words are non-null. */
public static void removeLongWords(ArrayList<String> words,
                                   int max)
{
   /* missing code */
}

Which of the following can replace  /* missing code */  so that the method works as intended?

Responses

A

for (String w : words)
{
   if (w.length() > max)
   {
      words.remove(w);
   }
}

B

int i = 0;
for (String w : words)
{
   if (w.length() > max)
   {
      words.remove(w);
      i--;
   }
   i++;
}

C

for (int i = 0; i < words.size(); i++)
{
   if (words.get(i).length() > max)
   {
      words.remove(i);
   }
}

D

for (int i = words.size() - 1; i >= 0; i--)
{
   if (words.get(i).length() > max)
   {
      words.remove(i);
   }
}

 

for (int i = words.size() - 1; i >= 0; i--)
{
   if (words.get(i).length() > max)
   {
      words.remove(i);
   }
}

10
New cards

Consider the following code segment.

 

ArrayList<Integer> myList = new ArrayList<Integer>(); 
myList.add(75); 
myList.add(20); 
myList.add(99); 
 
ArrayList<Integer> yourList = new ArrayList<Integer>(); 
/* missing code */

 

After the code segment is executed,  yourList  should contain  [20, 99, 75]

Which of the following can replace  /* missing code */  to produce this result?

Responses

A

int initialSize = myList.size(); 
for (int i = 0; i < initialSize; i++)
{ 
   myList.add(2, myList.remove(0)); 
   yourList.add(myList.get(0)); 
} 

B

int initialSize = myList.size(); 
for (int i = 1; i <= initialSize; i++)
{ 
   yourList.add(myList.get(i)); 
}

C

for (Integer temp : myList)
{ 
   yourList.add(0, temp);
} 

D

for (Integer temp : myList)
{ 
   myList.add(2, myList.remove(0)); 
   yourList.add(temp);  
}

int initialSize = myList.size(); 
for (int i = 0; i < initialSize; i++)
{ 
   myList.add(2, myList.remove(0)); 
   yourList.add(myList.get(0)); 
} 

11
New cards

In the following code segment,  numbers  is a properly declared  ArrayList  variable that has been initialized with the following values.

 

[13, 2, -4, 0, 34, -12, -13, 18, -27]

 

for (int i = 0; i < numbers.size(); i++) // Line 1 
{                                        
   if (numbers.get(i) < 0)                
   {                                     
      numbers.remove(i);                  
   }                          // Line 6
   i--;                       // Line 7 
}                                        

 

The code segment is intended to remove all negative values from  numbers.  However, the code segment is not working as intended. 

Which of the following changes can be made so that the code segment works as intended?

Responses

A

In line 1, changing  i < numbers.size()  to  i < numbers.size() - 1

B

In line 7, changing  i--;  to  i++;

C

Removing line 7

D

Switching lines 6 and 7

D. Switching lines 6 and 7

12
New cards

Consider the following code segment.

ArrayList<String> colors = new ArrayList<String>();

 

colors.add("Red");

colors.add("Orange");

colors.set(1, "Yellow");

colors.add(1, "Green");

colors.set(colors.size() - 1, "Blue");

colors.remove(0);

System.out.println(colors);

What is printed as a result of executing the code segment?

Responses

A

[Red, Green]

B

[Yellow, Blue]

C

[Green, Blue]

D

[Blue, Yellow]

[Green, Blue]

13
New cards

Consider the following code segment.

ArrayList<String> numbers = new ArrayList<String>();

numbers.add("one");

numbers.add("two");

numbers.add(0, "three");

numbers.set(2, "four");

numbers.add("five");

numbers.remove(1);

Which of the following represents the contents of numbers after the code segment has been executed?

Responses

A

["one", "four", "five"]

B

["three", "two", "five"]

C

["three", "four", "two"]

D

["three", "four", "five"]


["three", "four", "five"]

14
New cards

Consider the following code segment.

ArrayList<String> items = new ArrayList<String>();

items.add("A");

items.add("B");

items.add("C");

items.add(0, "D");

items.remove(3);

items.add(0, "E");

System.out.println(items);

What is printed as a result of executing the code segment?

Responses

A

[A, B, C, E]

B

[A, B, D, E]

C

[E, D, A, B]

D

[E, D, A, C]


[E, D, A, B]

15
New cards

Consider the following code segment.

ArrayList<String> animals = new ArrayList<>();

animals.add("fox");

animals.add(0, "squirrel");

animals.add("deer");

animals.set(2, "groundhog");

animals.add(1, "mouse");

System.out.println(animals.get(2) + " and " + animals.get(3));

What is printed as a result of executing the code segment?

Responses

A

mouse and fox

B

fox and groundhog

C

groundhog and deer

D

squirrel and groundhog


fox and groundhog

16
New cards

In the following code segment,  words  is an  ArrayList  of two or more  String  objects.

 

int prev = words.get(0).length(); 
int count = 0; 
 
for (int j = 1; j < words.size(); j++) 
{ 
   int curr = words.get(j).length(); 
   if (curr == prev)
   { 
      count += 1; 
   } 
   prev = curr; 
} 

Which of the following best describes the value assigned to  count  as a result of executing the code segment?

Responses

A

The number of elements in  words  whose length is the same length as the first element

B

The number of elements in  words  whose length is the same length as the element immediately preceding it

C

The number of elements in  words  whose value is equal to the first element

D

The number of elements in  words  whose value is equal to the element immediately preceding it


The number of elements in  words  whose length is the same length as the element immediately preceding it

17
New cards

Consider the following correct implementation of the insertion sort algorithm. The insertionSort method correctly sorts the elements of ArrayList data into increasing order.

public static void insertionSort(ArrayList<Integer> data)

{

for (int j = 1; j < data.size(); j++)

{

int v = data.get(j);

int k = j;

while (k > 0 && v < data.get(k - 1))

{

data.set(k, data.get(k - 1)); /* Statement 1 */

k--;

}

data.set(k, v); /* Statement 2 */

/* End of outer loop */

}

}

Assume that insertionSort is called with an ArrayList parameter that has been initialized with the following Integer objects.

[1, 2, 3, 4, 5, 6]

How many times will the statements indicated by /*Statement 1/ and /Statement 2*/ execute?

Responses

A

Statement 1

Statement 2

B

Statement 1

Statement 2

C

Statement 1

Statement 2

D

Statement 1

Statement 2

B. (0,5)

18
New cards

In the following code segment, assume that the ArrayList numList has been properly declared and initialized to contain the Integer values [1, 2, 2, 3]. The code segment is intended to insert the Integer value val in numList so that numList will remain in ascending order. The code segment does not work as intended in all cases.

int index = 0;

while (val > numList.get(index))

{

index++;

}

numList.add(index, val);

For which of the following values of val will the code segment not work as intended?

Responses

A

0

B

2

C

3

D

4


4

19
New cards

In the code segment below, myList is an ArrayList of integers. The code segment is intended to remove all elements with the value 0 from myList.

int j = 0;

while (j < myList.size())

{

if (myList.get(j) == 0)

{

myList.remove(j);

}

j++;

}

The code segment does not always work as intended. For which of the following lists does the code segment NOT produce the correct result?

Responses

A

{0, 1, 0, 2}

B

{1, 0, 0, 2}

C

{1, 2, 3, 0}

D

{1, 2, 3, 4}

B

{1, 0, 0, 2}

20
New cards

The removeElement method is intended to remove all instances of target from the ArrayList object data passed as a parameter. The method does not work as intended for all inputs.

public void removeElement(ArrayList<Integer> data, int target)

{

for (int j = 0; j < data.size(); j++)

{

if (data.get(j).equals(target))

{

data.remove(j);

}

}

}

Assume that the ArrayList object scores and the int variable low_score have been properly declared and initialized. In which of the following cases will the method call removeElement(scores, low_score) fail to produce the intended result?

Responses

A

When scores is [0, 2, 0, 2, 0, 6] and low_score is 0

B

When scores is [2, 4, 0, 5, 7, 0] and low_score is 0

C

When scores is [3, 4, 5, 7, 7, 2] and low_score is 1

D

When scores is [8, 8, 4, 3, 3, 6] and low_score is 3


When scores is [8, 8, 4, 3, 3, 6] and low_score is 3