Podcast
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));
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?
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);
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"?
Which method is used to determine if an ArrayList
contains a specific element, such as "Banana"?
What output will the following code produce?
ArrayList<String> list = new ArrayList<>(); System.out.println(list.isEmpty());
What output will the following code produce?
ArrayList<String> list = new ArrayList<>(); System.out.println(list.isEmpty());
Which method is used to convert an ArrayList
into a standard Java array?
Which method is used to convert an ArrayList
into a standard Java array?
Which of the following code snippets will result in a NullPointerException
?
ArrayList<String> list = null; System.out.println( /* Insert code here */ );
Which of the following code snippets will result in a NullPointerException
?
ArrayList<String> list = null; System.out.println( /* Insert code here */ );
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"));
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"));
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());
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());
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());
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());
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);
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);
What will the following code print?
ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.trimToSize(); System.out.println(list.size());
What will the following code print?
ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.trimToSize(); System.out.println(list.size());
What will happen when attempting to sort an ArrayList
containing both Strings and Integers?
What will happen when attempting to sort an ArrayList
containing both Strings and Integers?
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);
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);
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);
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);
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);
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);
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)); } }
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)); } }
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); } }
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); } }
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); }
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); }
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);
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);
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] + " "); } }
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] + " "); } }
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++; }
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++; }
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);
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);
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) + " ");
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) + " ");
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);
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);
What does the following code output?
ArrayList<String> list = new ArrayList<>(Arrays.asList("A", "B", "A", "C")); System.out.println(Collections.frequency(list, "A"));
What does the following code output?
ArrayList<String> list = new ArrayList<>(Arrays.asList("A", "B", "A", "C")); System.out.println(Collections.frequency(list, "A"));
Flashcards
What does get(index)
do?
What does get(index)
do?
Retrieves the element at a specific index in an ArrayList.
What does add(int index, E element)
do?
What does add(int index, E element)
do?
Inserts an element at a specified index, shifting existing elements.
What does clear()
do?
What does clear()
do?
Removes all elements from the ArrayList, making it empty.
What does contains(Object o)
do?
What does contains(Object o)
do?
Signup and view all the flashcards
What does isEmpty()
do?
What does isEmpty()
do?
Signup and view all the flashcards
What does toArray()
do?
What does toArray()
do?
Signup and view all the flashcards
What causes a NullPointerException
?
What causes a NullPointerException
?
Signup and view all the flashcards
What does lastIndexOf(Object o)
do?
What does lastIndexOf(Object o)
do?
Signup and view all the flashcards
What happens adding null
?
What happens adding null
?
Signup and view all the flashcards
What does clone()
do?
What does clone()
do?
Signup and view all the flashcards
What does removeIf(Predicate<? super E> filter)
do?
What does removeIf(Predicate<? super E> filter)
do?
Signup and view all the flashcards
What does trimToSize()
do?
What does trimToSize()
do?
Signup and view all the flashcards
What causes a ClassCastException
?
What causes a ClassCastException
?
Signup and view all the flashcards
How does adding increase list size?
How does adding increase list size?
Signup and view all the flashcards
What does subList(int fromIndex, int toIndex)
do?
What does subList(int fromIndex, int toIndex)
do?
Signup and view all the flashcards
What list.remove do?
What list.remove do?
Signup and view all the flashcards
What a set method do?
What a set method do?
Signup and view all the flashcards
What Collections.frequency method does ?
What Collections.frequency method does ?
Signup and view all the flashcards
What is the File class for?
What is the File class for?
Signup and view all the flashcards
What does createNewFile()
do?
What does createNewFile()
do?
Signup and view all the flashcards
What isDirectory()
method does ?
What isDirectory()
method does ?
Signup and view all the flashcards
What does file.delete()
do?
What does file.delete()
do?
Signup and view all the flashcards
What is PrintWriter
for?
What is PrintWriter
for?
Signup and view all the flashcards
What does file.renameTo()
do?
What does file.renameTo()
do?
Signup and view all the flashcards
What does flush()
do?
What does flush()
do?
Signup and view all the flashcards
Study Notes
ArrayList Study Guide
get(0)
retrieves the first list element;list.get(0)
on[5]
returns5
.add(int index, E element)
inserts an element at a specified index, shifting subsequent elements.- Adding
"Banana"
at index0
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 aNullPointerException
lastIndexOf("Apple")
returns the last index of "Apple" =2
in["Apple", "Banana", "Apple"]
.- ArrayList allows
null
elements; adding null increases the size to1
. 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 aClassCastException
.- 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 ifnum
matches the value10
;10
is then therefore removed.- In nested loops printing previous elements: inner loop runs
j
times for eachi
, 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
toi - 1
for each value ofi
. - A loop iterates through indices
0
,1
, and2
, printing elements, but throws anArrayIndexOutOfBoundsException
on the next iteration where i =3
. - the
set()
method works on lists created byArrays.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 index1
, 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
toFileWriter
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.