Module 07: ArrayList Flashcards

1
Q

7.1 ArrayList

What is an ArrayList?

A

Mutable list of object references

(A more convenient way to create adjustable arrays in through the ArrayList class)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

7.1 ArrayList

How does an ArrayList look like?

A

import java.util.ArrayList;

ArrayList list = new ArrayList();

  • Need import java.util.ArrayList; to use array list
  • E represents the data type that is used
  • declaration type must match the initialization type
  • Can only store objects, so must use the Integer and Double class
  • ArrayList () creates an empty ArrayList
  • Dont need to specify the length of the arrayList
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

7.1 ArrayList

What is the difference between ArrayLists and Arrays?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

7.1 ArrayList

How do you create an ArrayList of Strings?

  1. String[] list = new String[];
  2. ArrayList list = new ArrayList();
  3. ArrayList list = new ArrayList;
  4. List list = new List();
    5.
A

2. ArrayList list = new ArrayList();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

7.1 ArrayList

What is one of the primary differences between an ArrayList and an Array?

  1. ArrayLists have a fixed number of values, while Arrays can change in size.
  2. Arrays have a fixed number of values, while ArrayLists can change in size.
  3. Arrays are only able to store objects, while ArrayLists can store primitive and object values.
  4. ArrayLists are lists, while arrays are data structures.
A
  1. Arrays have a fixed number of values, while ArrayLists can change in size
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

7.1 ArrayList

True or False:

Attempting to add an int or double primitive value to an ArrayList will result in an error.

A

False

When an int or double value is added to an ArrayList, it is automatically converted to its Wrapper class, and when it is accessed, is transformed back into its primitive value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

7.2 ArrayList Methods

boolean add(E obj):

A

Appends obj to end of list

arrrayList.add(”Add!”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

7.2 ArrayList Methods

void add(int index, E obj):

A

Inserts obj at position index moves element index and higher up and adds 1 to size

  • arrayList.add(2, “Add”)
  • Increases index of higher values
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

7.2 ArrayList Methods

E get(int index)

A

Returns element at the position index

String elem = arrayList.get(2);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

7.2 ArrayList Methods

int size()

A

Returns the number of elements in the list

int num = arrayList.size()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

7.2 ArrayList Methods

E set(int index, E obj)

A

Replace elements at index with obj. Returns replaced element

arrayList.set(2, “NewValue”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

7.2 ArrayList Methods

E remove(int index)

A

Removes an element from position index. Substracts 1 from size and moves elements index and higher down one index.

arrayList.remove(2);

  • Decreases index of value index and higher
  • Can also return the value being removed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

7.2 ArrayList Methods

How do the features of arrays and ArrayLists compare?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

7.2 ArrayList Methods

What value will be printed in the console after this series of commands? Assume that the ArrayList package has been imported.

ArrayList array = new ArrayList();

array.add(3);

array.add(9);

array.add(10);

array.remove(1);

array.set(0, 3);

int num = array.get(1);

System.out.println(array.size());

  1. 1
  2. 2
  3. 3
  4. There will be an error caused by an incorrect use of ArrayList Methods.
A
  1. 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

7.2 ArrayList Methods

What value will be printed in the console after this series of commands? Assume that the ArrayList package has been imported.

ArrayList array = new ArrayList();

array.add(3);

array.add(9);

array.add(10);

array.remove(1);

array.set(0, 3);

int num = array.get(1);

System.out.println(num);

  1. 3
  2. 9
  3. 10
  4. 2
A
  1. 10
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

7.2 ArrayList Methods

What value will be printed in the console after this series of commands? Assume that the ArrayList package has been imported.

ArrayList names = new ArrayList();

names.add(“Michael”);

names.add(“Lebron”);

names.add(“Serena”);

names.add(“Shaq”);

String firstName = names.remove(0);

String secondName = names.set(0, “Venus”);

names.add(2, firstName);

System.out.println(“Names:” + secondName + “ and “ + names.get(1));

  1. Names: Lebron and Michael
  2. Names: Venus and Serena
  3. Names: Michael and Shaq
  4. Names: Lebron and Serena
  5. This will cause an error, as names.remove cannot be assigned to a value.
A
  1. Names: Lebron and Serena
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

7.3 Traversing ArrayList

How do you traverse an ArrayList?

A

ArrayList scores = new ArrayLists();

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

{

//This prints out the ith element System.out.println(scores.get(i));

}

  • i iterates from 0 to size()
  • Each iteration of i accesses the index of scores at index i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

7.3 Traversing ArrayList

How do you traverse an ArrayList using a while loop?

A

int i = 0;

while(i < scores.size())

{

System.out.println(scores.get(i)); i ++;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

7.3 Traversing ArrayList

How do you traverse an ArrayList using an enhance loop?

A

for(int score: scores)

{

System.out.printlnt(score);

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

7.3 Traversing ArrayList

How do you remove an element from the array?

A

public void removeA(ArrayList list)

{

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

{

if(list.get(i).startsWith(“A”))

{

list.remove(i);

i–;

}

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

7.3 Traversing ArrayList

How do you remove elements from an ArrayList using a while loop?

A

int index = 0;

while(index < list.size())

{

if(list.get(index).startsWith(“A”))

{

list.remove(index);

}

else

{

index++;

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

7.3 Traversing ArrayList

Why should you not use enhanced for loops to remove elements from an ArrayList?

A

Trying to remove() with an enhanced for loop will often result in a ConcurrentModificationException

Error occurs when objects are being modified while they are being iterated on

Generally speaking, we should not iterate with enhanced for loops when adding or removing items

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

7.3 Traversing ArrayList

True or False: Enhanced for loops should not be used to traverse ArrayLists.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

7.3 Traversing ArrayList

Will this method correctly traverse an ArrayList and remove all multiples of 3?

public void remove3s(ArrayList array)

{

int counter = 0;

while(counter < array.size())

{

if(array.get(counter) %3 == 0)

{

array.remove(counter);

counter++;

}

else

{

counter++;

}

}

}

  1. Yes, this method is written correctly, and will remove all multiples of 3 in an ArrayList.
  2. No, this method is not written correctly, as the counter in the if statement will skip the next value, as the values will shift down in the ArrayList.
  3. No, this method is not written correctly, as the counter in the else statement will skip the next value, as the values will shift down in the ArrayList.
  4. No, this method will not work because the methods used to access the ArrayList are incorrect.
A
  1. No, this method is not written correctly, as the counter in the if statement will skip the next value, as the values will shift down in the ArrayList
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

7.4 Developing Algorithms

What is the structure to insert elements into an array?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

7.4 Developing Algorithms

Transverse an ArrayList to add number + 2 after an odd element in the ArrayList:

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

7.4 Developing Algorithms

What are common ArrayList Algorithms

A
  1. Determine a min or max value
  2. Compute a sum, average, or mode
  3. Determine if at least 1 element has a particular property
  4. Determine if all elements have a particular property
  5. Access all consecutive Paris of elements
  6. Determine the presence or absence of duplicate elements
  7. Determine the number of elements meeting specific criteria
  8. Shifts or rotates elements
  9. Reverse the order of arrays
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

7.4 Developing Algorithms

How to you shift, rotate, or reverse elements in an ArrayList?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

7.4 Developing Algorithms

How do you transverse an ArrayList to add two ArrayList variables?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

7.5 Searching

Which of these method implementations would correctly insert an element into an ArrayList before every even index?

(1)

public void addEven(ArrayList array, E element)

{

for(int index = 0; index < array.size(); index++)

{

if(index %2 == 0)

{

array.add(index, element);

index++;

index++;

} } }

(2)

public void addEven(ArrayList array, E element)

{

for(int index = 0; index < array.size(); index++)

{

if(index %2 == 0)

{

array.add(index -1, element);

index++;

} } }

(3)

public void addEven(ArrayList array, E element)

{

for(int index = 0; index < array.size(); index++)

{

if(index %2 == 0)

{

array.add(index, element);

index++;

} } }

A

(3)

public void addEven(ArrayList array, E element)

{

for(int index = 0; index < array.size(); index++)

{

if(index %2 == 0)

{

array.add(index, element);

index++;

} } }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

7.5 Searching

What is Linear Searching?

A
  • How: Given a list and element to search for, return the index of that element in the list
  • if the element does not exist = return -1
  • Called Linear or Sequential search

Checks each element in order until the desired value or end of the array is reached

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

7.5 Searching

What is the structure of Linear Searching?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

7.5 Searching

What are the pros and cons of linear searching?

A

Pros:

Linear Search is fairly easy to implement and understand

Cons:

As the size of the data increase; however, the longer Linear Search takes to complete

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

7.5 Searching

True or False: Linear Search becomes more efficient as more values are added to a particular data set.

A

False

35
Q

7.5 Searching

How does Linear Search work?

  1. Linear Search traverses an array until the desired value is found, or until the end of the array.
  2. Linear Search traverses an array from the last value to the first value until the desired value is found.
  3. Linear Search uses a while loop to traverse an array for the desired value, or until the end of the array.
  4. Linear Search searches for desired values by searching the the next highest value in an array, and seeing if that is the desired value.
A
  1. Linear Search traverses an array until the desired value is found, or until the end of the array
36
Q

7.6 Sorting

What is sorting and its purpose?

How many types are there?

A
  • Data sets grow, they can be harder to manage and sort through!
  • When data is disorganized, it can be hard to find values easily - sorting makes it easier to manage

Sorting Algorithms:

Types: (1) Selection Sort and (2) Insertion Sort

37
Q

7.6 Sorting

What are selection sorting and its steps?

A

Sorts an array by repeatedly finding the minimum value, and moving it to the front of the array

Steps:

  1. Pick current index
  2. Find minimum in the rest of the line
  3. Swap to put the minimum in the correct position
38
Q

7.6 Sorting

Pseudocode for Selection Sort

A
  1. traverse each index up to the second to the last element
    1. Find the minimum in the rest of the line
      1. set the current index to minimum
      2. traverse from current index to end of list
        1. if index < current index, index is minimum
  2. Swap the index and minIndex
    1. Create temporary variable to store current index value
    2. Make current index value that minIndex value
    3. Make minIndex value the temporary variable value
39
Q

7.6 Sorting

What is the structure for selection sorting?

A
40
Q

7.6 Sorting

How many comparisons will the selection sort algorithm make on an array of 8 elements?

A

28

= 7 + 6 + 5 + 4 + 3 + 2 + 1

41
Q

7.6 Sorting

How does Selection Sort work to sort an array?

  1. Selection Sort swaps the first value of an array with the last index of an array, then swaps the second value in an array with the second to last value, until all values have swapped places.
  2. Selection Sort iterates through each index and swaps the current index with the minimum value that exists in the indices greater than the current index.
  3. Selection Sort sets the first index as sorted, and shifts each minimum value into the correct position by finding the lowest value on the sorted side of the array at indices lower than the current index.
  4. Selection Sort finds the lowest value in an array and shifts all elements to the right of that value.
A
  1. Selection Sort iterates through each index and swaps the current index with the minimum value that exists in the indices greater than the current index
42
Q

7.6 Sorting

What is Insertion Sort?

A

Sorts an array by sorting each element compared to the element already sorted to their left

43
Q

7.6 Sorting

What is the steps for Insertion Sort?

A
  1. Mark first element as sorted
  2. Choose the next unsorted element
  3. Shift unsorted element into the correct position in sorted part of the list
  4. Now left part of list is sorted
  5. Choose the next unsorted element
  6. Shift unsorted element into correct position in sorted part of list
44
Q

7.6 Sorting

What is the pseudocode for selection sort?

A
  1. Traverse each element starting from index 1
  2. Traverse sorted elements to find current element position
  3. Shift sorted elements to place current element
45
Q

7.6 Sorting

What is the structure for Insertion Sort?

A
46
Q

7.6 Sorting

What is the difference between Insertion vs. Selection Sort and when should you use each?

A

The efficiency of both depends on how sorted the list order is at the start

Use Statement Execution Count can be used to compare the efficiency of sorting algorithms

array1: {0, 1, 2, 4, 3}

  • When the list is almost sorted, Insertion Sort has a lower execution count than Selection short - because the while loop doesn’t execute

array2: {4, 3, 2, 1, 0}

  • When the list is in reversed order, Selection Sort has a lower execution count because it only needs to swap two values, while Insertion Sort has to shift every single value
47
Q

7.6 Sorting

When is it more appropriate to use Insertion Sort than Selection Sort?

  1. When the array is in reverse order.
  2. There is no difference in the speed of either sorting method.
  3. When the array is already slightly sorted.
  4. Selection sort is always faster than Insertion sort.
A
  1. When the array is already slightly sorted
48
Q

7.6 Sorting

How does insertion sort work to sort an array?

  1. Insertion Sort finds the lowest value in an array and shifts all elements to the right of that value.
  2. Insertion Sort iterates through each index and swaps the current index with the minimum value that exists in the indices greater than the current index.
  3. Insertion Sort sets the first index as sorted, and shifts each subsequent value into the correct position by finding the lowest value on the sorted side of the array at indices lower than the current index.
  4. Insertion Sort swaps the first value of an array with the last index of an array, then swaps the second value in an array with the second to last value, until all values have swapped places.
A
  1. Insertion Sort sets the first index as sorted, and shifts each subsequent value into the correct position by finding the lowest value on the sorted side of the array at indices lower than the current index
49
Q

7.6 Sorting

The following array is to be sorted biggest to smallest using insertion sort.

[10, 40, 50, 30, 20, 60]

What will the array look like after the third pass of the for loop?

  1. [60, 50, 40, 30, 20, 10]
  2. [50, 40, 10, 30, 20, 60]
  3. [50, 40, 30, 10, 20, 60]
  4. [60, 50, 40, 30, 10, 20]
  5. [10, 30, 40, 50, 20, 60]
A
  1. [50, 40, 30, 10, 20, 60]
50
Q

7.7 Ethical Issues Around Data Collections

What is privacy?

A

Privacy is the “right to be free from unwarranted intrustion and keep certain matters from public view”

51
Q

7.7 Ethical Issues Around Data Collections

What is security (and cybersecurity)?

A

🖇️ Security is the state of being free from danger or threat

Cybersecurity:

  • 2017 Median Pay: $95,510
  • Bachelor’s degree
  • Job outlook (2016-26): 28% growth (much higher than average)
52
Q

7.7 Ethical Issues Around Data Collections

What is your digital footprint?

A

Information about particular person that exists on the Internal as a result of their online activity

  • Email you send
  • Comments you leave
  • Pictures you post
  • Topics you search
  • Apps you use

Builds as you use the internet

53
Q

7.7 Ethical Issues Around Data Collections

How can you protect personal data?

A
  1. Use privacy settings to limit exposure
  2. Review posts you are tagged in
  3. Permanently delete old social media accounts you don’t use
  4. Google yourself on a regular basis
  5. Strong password
  6. Stay on secure sites with https://
    • Not just http://
    • The “s” is for Secure
  7. Monitor your webcam and microphone use
  8. Be aware of your location sharing
  9. Know site privacy policies when you agree to them
  10. Avoid phishing attempts in emails
  11. Be careful logging into public computers
  12. Stay up to date on best web security practices
  13. If you think you’re being violated - contact parent/gaurdian, company owns the sire/app, and school or IT person
54
Q

7.7 Ethical Issues Around Data Collections

What are protocols?

A

🖇️ Protocols for data sharing on the internet define rules and conventions for communication between network devices

  • Internet is a packet-switched system through which digital data is sent by breaking the data into blocks of bits called packets
  • Packets contain both data being transmitted and control information (Metadata) that helps route the data
  • It is only able to work because all machines have agreed to use the same protocols for creating and reading packets
55
Q

7.7 Ethical Issues Around Data Collections

What is the impact of the internet?

A
  • Collaboration
  • Communication
  • Dissemination of information
  • Crowdsourcing
  • Anonymity
  • Censorship
  • Concerns over privacy and security
56
Q

7.7 Ethical Issues Around Data Collections

What are the benefits of sharing data?

A
  • Communication - Email, Video calls, Social media
  • Collaborative problem solving
  • E-commerce or online shopping
  • Access to information
  • Open databases for scientific publications
  • Online learning
  • GPS
  • Entertainment
57
Q

7.7 Ethical Issues Around Data Collections

What are the harmful effects of shared data?

A
  • Access to information (Wikileaks)
  • Several legal and ethical concerns arise from sharing data
    • Access to copyrighted material
  • Peer networks allow people to share content (though others would not want it to be shared)
  • Easier than ever to distribute information or content that is not your own
  • Anonymity:
    • How identifiable internet users should be
    • Equiality on the internet
    • Cyberbulling
  • Censorship:
    • Should google display search results that have explicit or illegal content?
    • Should government be able to filer what content citizens see?
58
Q

7.7 Ethical Issues Around Data Collections

What is the ethical code and the Code of Ethics?

A

Responsibility to make sure programs and applications are ethical, legal, and socially acceptable

Code of Ethical:

Contribute to society’s well-being

Avoid harm

Honest and trustworthy

Be fair and take action to not discriminate

Respect the work required to produce new ideas, inventions, creative works, and computing artifacts

Respects privacy

Honor confidentially

59
Q

7.7 Ethical Issues Around Data Collections

Which of the following are some good ways to protect your own personal data?

I. Use privacy settings to limit exposure
II. Review posts you are tagged in and take action if needed
III. Permanently delete old social media accounts you don’t use
IV. Google yourself on a regular basis
V. Use strong passwords
Vi. Stay on sites with http://

  1. I, II, and III
  2. I-V all
  3. IV, V, and VI
  4. I- VI all
A

2. I-V all

60
Q

7.7 Ethical Issues Around Data Collections

Some good and practical ways to avoid a phishing attempt are to

I. Not click through any links in an email that seem suspicious
II. Not download any attachments in an email that seem suspicious
III. Not use email services at all
IV. Consider the source of each email
V. Consider if the email contents sound too good to be true, like you have won a prize or something

  1. I and II
  2. IV and V
  3. I - V all
  4. I, II, IV and V
A
  1. I, II, IV and V
61
Q

7.7 Ethical Issues Around Data Collections

Protocols for data sharing on the internet are

  1. rules and conventions for communication between network devices.
  2. ways to avoid email phishing attempts.
  3. ideas for establishing a positive digital footprint.
  4. are now considered obsolete with technologies like Web 2.0 and HTML5.
A
  1. rules and conventions for communication between network devices
62
Q

7.7 Ethical Issues Around Data Collections

A field of computer science that’s related to keeping data private and secure called

  1. The CIA
  2. Computer Ethics
  3. Cybersecurity
  4. Crowdsourcing
A
  1. Cybersecurity
63
Q

7.7 Ethical Issues Around Data Collections

Which of the following only has beneficial impacts on our society?

I. Collaboration
II. Communication
III. Sharing of information
IV. Anonymity

  1. I and II
  2. III
  3. IV
  4. None of these
A
  1. None of these
64
Q

ArrayList Exam Quiz

Question: 1

What is the proper way to get the number of items in an ArrayList called list?

  1. list.length
  2. list.size
  3. list.length()
  4. list.size()
A
  1. list.size()
65
Q

ArrayList Exam Quiz

Question: 2

What will the following code print?

ArrayList list = new ArrayList();

list.add(“Hello”);

list.add(“World”);

System.out.println(list[0]);

  1. “Hello”
  2. “World”
  3. “list[0]”
  4. There will be a compiler error
  5. None of the above
A
  1. There will be a compiler error
66
Q

ArrayList Exam Quiz

Question: 3

What is wrong with the following code?

ArrayList list = new ArrayList();

list.add(1);

System.out.println(list.get(0));

  1. It will throw an IndexOutOfBoundsException.
  2. You cannot use int as the type in an ArrayList.
  3. list is a reserved word, so you can’t call the variable that.
  4. You need to define the new ArrayList on a new line of code.
  5. Nothing. The above code is correct.
A
  1. You cannot use int as the type in an ArrayList
67
Q

ArrayList Exam Quiz

Question: 4

What will the following code print?

ArrayList list = new ArrayList();

list.add(0);

list.add(1);

list.add(2);

list.add(3);

list.add(4);

list.add(5);

int sum = 0;

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

{

sum += list.get(i);

}

System.out.println(sum);

  1. 0
  2. 5
  3. 6
  4. 9
  5. 15
A
  1. 6
68
Q

ArrayList Exam Quiz

Question: 5

What will the following code print?

ArrayList list = new ArrayList();

list.add(“I”);

list.add(“love”);

list.add(“coding”);

list.add(“in”);

list.add(“Java”);

list.remove(3);

System.out.println(list.get(3));

  1. 3
  2. coding
  3. in
  4. Java
  5. It will throw an IndexOutOfBoundsException.
A
  1. Java
69
Q

ArrayList Exam Quiz

Question: 6

Consider the following statement:

ArrayList newList = /* Missing Code */

Which of the following can be replaced with /* Missing Code */ so that the statement works as intended?

I.

new ArrayList;

II.

new ArrayList();

III.

new ArrayList();

  1. I only
  2. III only
  3. I and III
  4. II and III
  5. I, II, and III
A
  1. II and III
70
Q

ArrayList Exam Quiz

Question: 7

What would the proper initialization and declaration be for an ArrayList myTemps meant to store precise temperature measurements?

  1. ArrayList myTemps = new ArrayList();
  2. ArrayList myTemps = new ArrayList();
  3. ArrayList myTemps = new ArrayList();
  4. ArrayList myTemps = ArrayList();
  5. ArrayList myTemps = ArrayList();
A
  1. ArrayList myTemps = new ArrayList();
71
Q

ArrayList Exam Quiz

Question: 8

Consider the following code segment:

Which of the following represents the value of nums after the code segment has been executed?

  1. [20, 40, 90, 50]
  2. [10, 20, 30, 40, 50]
  3. [20, 30, 90, 50]
  4. [20, 40, 50, 90]
  5. [20, 30, 80, 50]
A
  1. [20, 30, 90, 50]
72
Q

ArrayList Exam Quiz

Question: 9

Consider the following code segment:

Which of the following represents the value of scales after the code has been executed?

  1. [SO, RE, FA, DO]
  2. [SO, RE, MI, FA, DO]
  3. [SO, RE, MI, DO]
  4. [FA, RE, MI, DO]
  5. [FA, SO, RE, MI, DO]
A
  1. [SO, RE, MI, FA, DO]
73
Q

ArrayList Exam Quiz

Question: 10

Consider the following code segment:

What is printed when the code segment is executed?

  1. [9, 8, 7, 6, 5]
  2. [10, 9, 8, 7, 6]
  3. [9, 8, 7, 6]
  4. [9, 8, 7, 6, 4, 3]
  5. Nothing will print, as the while loop condition occurs infinitely.
A
  1. [9, 8, 7, 6]
74
Q

ArrayList Exam Quiz

Question: 11

Consider the following code segment:

What is printed when the code segment is executed?

  1. []
  2. [“Two”, “Four”]
  3. [“Two”, “Four”, “Six”]
  4. [“Four”, “Five”, “Six”]
  5. [“Six”]
A
  1. [“Two”, “Four”, “Six”]
75
Q

ArrayList Exam Quiz

Question: 12

The following method is a search method intended to return the index at which a String value occurs within an ArrayList:

However, there is currently an error preventing the method to work as intended. Which of the following Lines needs to be modified in order for the method to work as intended?

  1. Line 1 - The method should be returning a String value, not an int.
  2. Line 4 - The loop should go to < list.size() - 1 so as to avoid an IndexOutofBoundsException.
  3. Line 8 - The return should be the counter, not list.get(counter).
  4. Line 10 - As written, the counter will skip every other value.
  5. Line 12 - The return value should be a String, not an int.
A
  1. Line 8 - The return should be the counter, not list.get(counter).
76
Q

ArrayList Exam Quiz

Question: 13

Consider the following implementation of a search method:

An ArrayList nums, is initialized with the values [1, 2, 4, 3, 4, 5]. What value would be returned from the call search(nums, 4)?

-1

2

4

This search method is written improperly, and will result in an error.

3

A

4

77
Q

ArrayList Exam Quiz

Question: 14

Consider the following correct implementation of the selection sort algorithm:

Given an ArrayList initialized with the values [6, 5, 4, 3, 2, 1], how many times does Line 19 execute when the ArrayList is sorted using the selection sort algorithm?

6

3

5

30

4

A

3

78
Q

ArrayList Exam Quiz

Question: 15

Consider the following correct implementation of the selection sort algorithm:

What would be the most effective way to alter this selection sort algorithm so that an ArrayList would be sorted from greatest to smallest value?

  1. Change Line 3 to int currentMaxIndex;
  2. Change Line 5 to for(int i= arr.size() -1; i > 0; i –)
  3. Change Line 10 to if(arr.get(j) > arr.get(currentMinIndex))
  4. Change Line 16 to if (i > currentMinIndex)
  5. Change Line 19 to arr.set(arr.get(i), currentMinIndex);
A
  1. Change Line 10 to if(arr.get(j) > arr.get(currentMinIndex))
79
Q

ArrayList Exam Quiz

Question: 16

Consider the following correct implementation of the insertion sort algorithm:

The following declaration and method call are made in another method in the same class as insertionSort:

int[] nums = {6, 5, 4, 3, 2, 1};

list = insertionSort(nums);

How many times is the statement on Line 13 executed as a result of the call to insertionSort?

5

30

15

16

6

A

15

80
Q

ArrayList Exam Quiz

Question: 17

A website organizes its list of contributors in alphabetical order by last name. The website’s new manager would prefer the contributor list to be ordered in reverse alphabetical order instead. Which classic algorithm would be best suited to complete this task?

  1. Linear/Sequential Search
  2. Selection Sort
  3. Insertion Sort
  4. None of these algorithms are viable options.
A
  1. Selection Sort
81
Q

ArrayList Exam Quiz

Question: 18

A company organizes all of its client database in order of the amount of money that the account is worth. When a new account is added, it is placed in the correct order in the database based on the worth of the account. Which classic algorithm would be best suited to perform this function?

  1. Linear/Sequential Search
  2. Insertion Sort
  3. Selection Sort
  4. None of these are viable options
A
  1. Insertion Sort
82
Q

ArrayList Exam Quiz

Question: 19

The following segment of code is meant to remove the even numbers from an ArrayList list and print the results:

The method as written, however, is incorrect. Which ArrayList(s) list would prove that this method was written incorrectly?

I.
[1, 2, 3, 4, 5]

II.
[2, 4, 5, 6, 7]

III.
[2, 4, 6, 8, 10]

IV.
[2, 5, 6, 7, 8]

  1. III only
  2. II and IV
  3. II only
  4. I and IV
  5. II and III
A
  1. II and III
83
Q

ArrayList Exam Quiz

Question: 20

Consider the following code segment:

What is printed as a result of this code segment?

  1. [10, 15, 20, 25, 30]
  2. [5, 15, 20, 25, 30]
  3. [1, 2, 3, 4, 5]
  4. [5, 10, 15, 20, 30]
  5. An IndexOutofBoundsException occurs.
A
  1. An IndexOutofBoundsException occurs