ArrayList Methods

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What will be the output of the following code snippet? ArrayList<Integer> list = new ArrayList<>(); list.add(5); System.out.println(list.get(0));

  • ArrayIndexOutOfBoundsException
  • null
  • 0
  • 5 (correct)

Consider the following code: ArrayList<String> list = new ArrayList<>(); list.add("Apple"); list.add(0, "Banana"); System.out.println(list); What will be printed to the console?

  • [Apple, Banana]
  • IndexOutOfBoundsException
  • [Banana, Apple] (correct)
  • [Apple]

What will be the output after executing the following code? ArrayList<String> list = new ArrayList<>(); list.add("Apple"); list.clear(); System.out.println(list);

  • Compilation error
  • null
  • [Apple]
  • [] (correct)

Which method is used to determine if an ArrayList contains a specific element, such as "Banana"?

<p>list.contains(&quot;Banana&quot;) (B)</p> Signup and view all the answers

What output will the following code produce? ArrayList<String> list = new ArrayList<>(); System.out.println(list.isEmpty());

<p>true (C)</p> Signup and view all the answers

Which method is used to convert an ArrayList into a standard Java array?

<p>toArray() (B)</p> Signup and view all the answers

Which of the following code snippets will result in a NullPointerException? ArrayList<String> list = null; System.out.println( /* Insert code here */ );

<p>Both A and B (A)</p> Signup and view all the answers

What is the output of the following code? ArrayList<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); list.add("Apple"); System.out.println(list.lastIndexOf("Apple"));

<p>2 (D)</p> Signup and view all the answers

What will be the size of the ArrayList after executing the following code? ArrayList<String> list = new ArrayList<>(); list.add(null); System.out.println(list.size());

<p>1 (A)</p> Signup and view all the answers

What will be the output of the following code? ArrayList<String> list = new ArrayList<>(); list.add("A"); list.add("B"); ArrayList<String> clonedList = (ArrayList<String>) list.clone(); clonedList.add("C"); System.out.println(list.size() + " " + clonedList.size());

<p>2 3 (D)</p> Signup and view all the answers

What will be the output of the following code? ArrayList<Integer> list = new ArrayList<>(); list.add(10); list.add(20); list.removeIf(n -> n % 2 == 0); System.out.println(list);

<p>[] (C)</p> Signup and view all the answers

What will the following code print? ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.trimToSize(); System.out.println(list.size());

<p>2 (B)</p> Signup and view all the answers

What will happen when attempting to sort an ArrayList containing both Strings and Integers?

<p><code>Collections.sort(list)</code> (C)</p> Signup and view all the answers

What will be the eventual result of the following code execution? ArrayList<String> list = new ArrayList<>(); list.add("A"); list.add("B"); for (int i = 0; i < list.size(); i++) { list.add("C"); } System.out.println(list);

<p>Infinite loop (A)</p> Signup and view all the answers

What is the result of the of the following statements? ArrayList<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); list.add("Cherry"); List<String> subList = list.subList(0, 2); subList.add("Mango"); System.out.println(list);

<p>[Apple, Banana, Mango, Cherry] (D)</p> Signup and view all the answers

What will be the output of the following code? ArrayList<Integer> list = new ArrayList<>(); list.add(10); list.add(20); Integer num = 10; list.remove(num); System.out.println(list);

<p>[20] (A)</p> Signup and view all the answers

What values are printed with this loop construct? ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); for (int i = 0; i < list.size(); i++) { for (int j = 0; j < i; j++) { System.out.print(list.get(j)); } }

<p>Prints 1 1 2 (B)</p> Signup and view all the answers

What is output after executing the following code block? ArrayList<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); for (String s : list) { if (s.equals("B")) { System.out.println(s); } }

<p>B (A)</p> Signup and view all the answers

Consider the code below. What will be the eventual outcome of the 'for' loop? ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); for (int i = 0; i < list.size(); i++) { list.add(4); }

<p>Infinite loop (D)</p> Signup and view all the answers

What is printed to the console after running the code? ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); for (int i = list.size() - 1; i >= 0; i--) { list.remove(i); } System.out.println(list);

<p>[] (A)</p> Signup and view all the answers

What will the following nested loop produce? int[] arr = {1, 2, 3}; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < i; j++) { System.out.print(arr[j] + " "); } }

<p>1 1 2 (C)</p> Signup and view all the answers

What is the output of the following code? int[] arr = {1, 2, 3}; int i = 0; while (i <= arr.length) { System.out.println(arr[i]); i++; }

<p>Prints 1 2 3 and throws ArrayIndexOutOfBoundsException (B)</p> Signup and view all the answers

What happens in this loop with Arrays.asList? List<String> list = Arrays.asList("A", "B", "C"); for (int i = 0; i < list.size(); i++) { list.set(i, list.get(i).toLowerCase()); } System.out.println(list);

<p>[a, b, c] (D)</p> Signup and view all the answers

What happens when accessing a modified list during a loop? ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4)); for (int i = 0; i < list.size(); i++) { if (list.get(i) == 2) { list.add(5); } } System.out.print(list.get(i) + " ");

<p>1 2 3 4 5 (A)</p> Signup and view all the answers

What happens when you execute the following code? ArrayList<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); for (int i = 0; i < list.size(); i++) { if (list.get(i).equals("B")) { list.set(i, "Z"); } } System.out.println(list);

<p>[A, Z, C] (B)</p> Signup and view all the answers

What does the following code output? ArrayList<String> list = new ArrayList<>(Arrays.asList("A", "B", "A", "C")); System.out.println(Collections.frequency(list, "A"));

<p>2 (D)</p> Signup and view all the answers

Flashcards

What does get(index) do?

Retrieves the element at a specific index in an ArrayList.

What does add(int index, E element) do?

Inserts an element at a specified index, shifting existing elements.

What does clear() do?

Removes all elements from the ArrayList, making it empty.

What does contains(Object o) do?

Checks if an ArrayList contains a specific element and returns true if present, false otherwise.

Signup and view all the flashcards

What does isEmpty() do?

Checks if the ArrayList contains no elements (is empty).

Signup and view all the flashcards

What does toArray() do?

Converts the ArrayList into a conventional array.

Signup and view all the flashcards

What causes a NullPointerException?

Occurs when a method is called on a null reference.

Signup and view all the flashcards

What does lastIndexOf(Object o) do?

Returns the index of the last occurrence of a specified element.

Signup and view all the flashcards

What happens adding null?

ArrayList allows null elements to be added; the size increases by one.

Signup and view all the flashcards

What does clone() do?

Creates a shallow copy of the ArrayList.

Signup and view all the flashcards

What does removeIf(Predicate<? super E> filter) do?

Removes all elements that satisfy the given predicate.

Signup and view all the flashcards

What does trimToSize() do?

Adjusts the capacity of the ArrayList to be the list's current size.

Signup and view all the flashcards

What causes a ClassCastException?

Occurs when trying to sort a list with non-comparable objects.

Signup and view all the flashcards

How does adding increase list size?

Adding elements in a loop increases the list's size, the loop may never terminate if the loop condition depends on the list size.

Signup and view all the flashcards

What does subList(int fromIndex, int toIndex) do?

Creates a view of the portion of the list between the specified fromIndex, inclusive, and toIndex, exclusive. Changes in the range are directly reflected in original list.

Signup and view all the flashcards

What list.remove do?

The remove() removes the first occurence of the object

Signup and view all the flashcards

What a set method do?

Creates Arrays.asList, as it doesn't modify the structure of the list. (number of elements)

Signup and view all the flashcards

What Collections.frequency method does ?

The method counts occurrences object in the ArrayList.

Signup and view all the flashcards

What is the File class for?

Manages file and directory pathnames.

Signup and view all the flashcards

What does createNewFile() do?

Creates a new, empty file if it does not already exist.

Signup and view all the flashcards

What isDirectory() method does ?

The isDirectory() method returns true only if the specified path represents a directory, otherwise false

Signup and view all the flashcards

What does file.delete() do?

Deletes the file or directory.

Signup and view all the flashcards

What is PrintWriter for?

Writes formatted text to files or other output streams.

Signup and view all the flashcards

What does file.renameTo() do?

Renames the file or directory to the specified new name.

Signup and view all the flashcards

What does flush() do?

Forces any buffered output to be written immediately to the file.

Signup and view all the flashcards

Study Notes

ArrayList Study Guide

  • get(0) retrieves the first list element; list.get(0) on [5] returns 5.
  • add(int index, E element) inserts an element at a specified index, shifting subsequent elements.
  • Adding "Banana" at index 0 in ["Apple"] results in ["Banana", "Apple"].
  • The clear() method removes all ArrayList elements, resulting in an empty list: [].
  • contains("Banana") checks if the ArrayList contains the element "Banana", and returns true if present.
  • isEmpty() checks if the ArrayList contains elements; returns true if no elements are found.
  • toArray() converts the ArrayList into an array.
  • Using methods on a null ArrayList throws a NullPointerException
  • lastIndexOf("Apple") returns the last index of "Apple" = 2 in ["Apple", "Banana", "Apple"].
  • ArrayList allows null elements; adding null increases the size to 1.
  • clone() creates a shallow copy; modifications to the clonedList does not affect the original list.
  • removeIf(n -> n % 2 == 0) removes all even elements, resulting in an empty list: [].
  • trimToSize() adjusts capacity to the current list size but does not alter the size.
  • Collections.sort() requires mutually comparable elements; otherwise, throws a ClassCastException.
  • Modifying an ArrayList by adding elements increases the list size, causing the loop to never terminate.
  • Changes to a subList are mirrored in the original ArrayList.
  • remove() removes the specified object's first occurrence if num matches the value 10; 10 is then therefore removed.
  • In nested loops printing previous elements: inner loop runs j times for each i, printing previous elements.
  • Enhanced for loops: the if condition matches "B" and prints it.
  • Modifying the ArrayList by adding elements to it increases the size, causing the loop to never terminate.
  • The loop iterates backward, removing elements, eventually leading to an empty list: [].
  • The inner loop prints elements from index 0 to i - 1 for each value of i.
  • A loop iterates through indices 0, 1, and 2, printing elements, but throws an ArrayIndexOutOfBoundsException on the next iteration where i = 3.
  • the set()method works on lists created by Arrays.asList, as it doesn't modify the structure of the list.
  • Loop runs based on the initial list size; adding increases the list's size but doesn't affect already evaluated indices.
  • The set() method updates the element at index 1, without modifying the list structure.

File Study Guide

  • The File class in java.io represents file and directory pathnames abstractly.
  • createNewFile() creates a new, empty file if it does not exist.
  • isDirectory() returns true if the path represents a directory.
  • file.delete() deletes the file or directory.
  • PrintWriter is used to write formatted text to files/output streams.
  • file.renameTo(new File(newName)) renames a file or directory.
  • If the file does not exists before the program runs, then the program will create a new file and will write to it.
  • flush() forces buffered output to be immediately written to a file.
  • The finally block will run the code; whether there is an exception or not.
  • Passing true to FileWriter opens the file in append mode to preserve content.
  • The programmer catches exceptions and handles printing error messages, finalizes cleanup but does not execute if the JVM exits during the exception.
  • Passing true to FileWriter opens the file in append mode, preserving existing content.
  • A scanner reads each file line and prints it, "Hello World" will be printed if that's the only file content.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Java ArrayList Overview Quiz
18 questions

Java ArrayList Overview Quiz

BountifulChrysoprase avatar
BountifulChrysoprase
Java Collections Quiz
40 questions

Java Collections Quiz

WorthwhilePegasus avatar
WorthwhilePegasus
Java Collections Framework Quiz
30 questions
ArrayList in Java
20 questions

ArrayList in Java

AudibleAgate4418 avatar
AudibleAgate4418
Use Quizgecko on...
Browser
Browser