Task 1: What kind of errors are is caused by the following code, if any? How wou
ID: 3914150 • Letter: T
Question
Task 1: What kind of errors are is caused by the following code, if any? How would we fix them? Note: Some errors may be caused before execution (before you run the program). In this case, list the error written in NetBeans when you hover over the problem. //Task 1a. ArrayList arr = new ArrayList<>(); arr.set(0, "This is a String"); Exception:__________________________________________________________ What caused the error:______________________________________________ How to Fix It:______________________________________________________ -------------------------------------------------------------------- //Task 1b. ArrayList arr = new ArrayList<>(); arr.add("Apple"); arr.add("Banana"); arr.add("Carrot"); arr.add(6, "Pineapple Pizza"); Exception:__________________________________________________________ What caused the error:______________________________________________ How to Fix It:______________________________________________________ -------------------------------------------------------------------- //Task 1c. ArrayList arr = new ArrayList<>(); arr.add("Apple"); arr.add("Banana"); arr.add("Carrot"); arr.add(3, "Regular Pizza"); Exception:__________________________________________________________ What caused the error:______________________________________________ How to Fix It:______________________________________________________ -------------------------------------------------------------------- //Task 1d. ArrayList arr = new ArrayList<>(); arr.add("Apple"); arr.add("Banana"); arr.add("Carrot"); arr.get(1) = "Blood Orange"; Exception:__________________________________________________________ What caused the error:______________________________________________ How to Fix It:______________________________________________________ -------------------------------------------------------------------- //Task 1e. ArrayList arr = new ArrayList<>(); arr.add("Apple"); arr.add("Banana"); arr.add("Carrot"); arr.get(arr.size()); Exception:__________________________________________________________ What caused the error:______________________________________________ How to Fix It:______________________________________________________ -------------------------------------------------------------------- //Task 1f. ArrayList arr = new ArrayList<>(); arr.add("Apple"); arr.add("Banana"); arr.add("Carrot"); arr.remove("Potato"); Exception:__________________________________________________________ What caused the error:______________________________________________ How to Fix It:______________________________________________________
Explanation / Answer
Answer 1:
ArrayList<>(); arr.set(0, "This is a String");
Exception: NullPointerException
Cause : this will give NullPointerException as we are not creating object for it, just we created reference for the array list
Fix: ArrayList<String>arr = mew ArrayList<String>();
Answer2:
Exception: IndexOutOfBoundsException
Cause: as there are only 3 elements in the list but we trying to add the 6th elements which is out of bounds
Fix: remove arr.add(6, "Pineapple Pizza");
Answer 3: This will work without any error
Answe 4:it will give compilation error as we are trying to perform invalid assignment
cuase: left side always must be an variable arr.get(1) = "Blood Orange";
Fix: arr.get(i)
Answer 5:
Exception: IndexOutOfBoundsException
Cause:in ArrayList indexes start from 0 to size-1, but here we trying to get the element at size location
Fix: arr.add(arr.getSize()-1);
Answer 6:
This will work with out any errors
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.