Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question 2 If I wanted to iterate through a basic Java array, how would I do it? Would I use the length property or

image text in transcribed imageimageimageimageimageimageimageimage

Question 2 If I wanted to iterate through a basic Java array, how would I do it? Would I use the "length" property or the "size()" method? For basic arrays, use the "length" property. int[] array (1, 2, 3, 4, 5}; for(int i = 0; i < array.length; i++) { System.out.println(array[i]); } For basic arrays, use the ".size()" method. int[] array (1, 2, 3, 4, 5}; for(int i = 0; i E Ensign Homepage Quiz: 8.3 Quiz (30 min.) + C ensign.instructure.com/courses/14050/quizzes/186729/take LDS FAFSA Student Loan ck Credit Karma M Gmail iCloud byu chat page TestOut LabSim Course Hero 2022 Fall Semester Home Announcements 38 Syllabus Modules Grades People Tutoring Resources Question 3 1 pts If I wanted to iterate through Java's ArrayList, how would I do it? Would I use the "length" property or the ".size()" method? For Java's ArrayList, I would use the "length" property. ArrayList arrayList = new ArrayList (Arrays.asList(1, 2, 3, 4, 5)); for(int i = 0; i < arrayList.length; i++) { System.out.println(arrayList.get(i)); K 69F Mostly cloudy } For Java's ArrayList, I would use its ".size()" method. ArrayList arrayList = new ArrayList (Arrays.asList(1, 2, 3, 4, 5)); for(int i = 0; i < arrayList.size(); i++) { System.out.println(arrayList.get(i)); } Question 4 1 pts OneDrive Screenshot saved The screenshot was added to your OneDrive. 4:59 PM 10/31/2022 E Ensign Homepage Quiz: 8.3 Quiz (30 min.) + C ensign.instructure.com/courses/14050/quizzes/186729/take LDS FAFSA Student Loan ck Credit Karma M Gmail iCloud 38 K 2022 Fall Semester Home Announcements Syllabus Modules byu chat page Course Hero TestOut LabSim ArrayList arrayList = new ArrayList (Arrays.asList(1, 2, 3, 4, )); for(int i = 0; i < arrayList.size(); i++) { System.out.println(arrayList.get(i)); } Question 4 1 pts Grades People Tutoring Resources True or False: I would use Java's basic array when I know the size of the data or the array will not change, and I should use Java's ArrayList when I need a dynamic array and I don't know how much data may or may not be added. 69F Mostly cloudy True O False Question 5 1 pts OneDrive What will happen if I try the to access an element in a basic array at the index of -1, like so: Screenshot saved The screenshot was added to your OneDrive. 5:00 PM 10/31/2022 E Ensign Homepage Quiz: 8.3 Quiz (30 min.) + C ensign.instructure.com/courses/14050/quizzes/186729/take LDS FAFSA Student Loan ck Credit Karma M Gmail iCloud byu chat page TestOut LabSim Course Hero 38 2022 Fall Semester Home Announcements Syllabus Question 5 1 pts What will happen if I try the to access an element in a basic array at the index of -1, like Modules so: Grades People Tutoring Resources int[] array = {1, 2, 3, 4, 5}; int test = array[-1]; K 69F Mostly cloudy Because there is no negative index of an array, it will simply return the first element, or the element at index 0 It will throw an Array Index Out Of Bounds Exception. Because there is no negative index, it will wrap around to the back of the array to the last element, or index 4 in this case. Question 6 What will the following code print? OneDrive 1 pts Screenshot saved The screenshot was added to your OneDrive. 5:00 PM 10/31/2022 E Ensign Homepage Quiz: 8.3 Quiz (30 min.) + C ensign.instructure.com/courses/14050/quizzes/186729/take LDS FAFSA Student Loan ck Credit Karma M Gmail iCloud byu chat page TestOut LabSim Course Hero 2022 Fall Semester Home Announcements 38 Syllabus Modules K Question 6 1 pts Grades People Tutoring Resources What will the following code print? int[] array = {2, 4, 6, 8, 10}; int test = array[5]; System.out.println(test); It will throw an exception: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of boun ds for length 5 69F Mostly cloudy It will print the last element: 10 Question 7 2 pts OneDrive Screenshot saved The screenshot was added to your OneDrive. 5:00 PM 10/31/2022 E Ensign Homepage C LDS D 38 K Quiz: 8.3 Quiz (30 min.) + ensign.instructure.com/courses/14050/quizzes/186729/take FAFSA Student Loan ck Credit Karma M Gmail 2022 Fall Semester iCloud byu chat page TestOut LabSim Course Hero Question 7 2 pts Home Announcements The code below prints out something like this: Syllabus Modules [I@626b2d4a [1, 2, 3, 4, 5] Grades People Tutoring Resources Why does printing a basic Java array print something that seems random while Java's ArrayList prints the data in the array in a readable way? public static void main(String[] args) { } int[] array = {1, 2, 3, 4, 5}; ArrayList arrayList = new ArrayList (Arrays.asList(1, 2, 3, 4, 5)); System.out.println(array); System.out.println(arrayList); 69F Mostly cloudy This is an error coming from the IDE. The IDE should print the data in the integer array in a readable way. Java's ArrayList is an object, and has been given the functionality to print a readable String. Java's basic array is not an object, and printing it simply prints a reference of the memory location of the array. Java's arrays don't work properly, so you should always use an ArrayList. OneDrive > Screenshot saved The screenshot was added to your OneDrive. 5:00 PM 10/31/2022 E Ensign Homepage Quiz: 8.3 Quiz (30 min.) + C ensign.instructure.com/courses/14050/quizzes/186729/take LDS FAFSA Student Loan ck Credit Karma M Gmail iCloud byu chat page 38 K 2022 Fall Semester Home Announcements Syllabus Modules TestOut LabSim Course Hero Java's arrays don't work properly, so you should always use an ArrayList. Question 8 2 pts Grades People Tutoring Resources What is printed in the following code? (HINT: I am not passing a primitive type, but an ArrayList.) public static void main(String[] args) { ArrayList list = new ArrayList < (); System.out.println(list); add5Numbers(list); System.out.println(list); 69F Mostly cloudy } public static void add 5Numbers (ArrayList list) { list.add(1); } list.add(2); list.add(3); list.add(4); list.add(5); System.out.println(list); [] [1, 2, 3, 4, 5] [ ] 5:00 PM 10/31/2022 E Ensign Homepage Quiz: 8.3 Quiz (30 min.) + C ensign.instructure.com/courses/14050/quizzes/186729/take LDS FAFSA Student Loan ck Credit Karma M Gmail iCloud byu chat page TestOut LabSim Course Hero list.add(4); list.add(5); 2022 Fall Semester System.out.println(list); } 38 Home Announcements Syllabus Modules [ ] [1, 2, 3, 4, 5] [ ] Grades People Tutoring Resources [] [1, 2, 3, 4, 5] [1, 2, 3, 4, 5] [ ] [1] [1, 2] [1, 2, 3] [1, 2, 3, 4] [1, 2, 3, 4, 5] K 69F Mostly cloudy [1, 2, 3, 4, 5] [1, 2, 3, 4, 5] [1, 2, 3, 4, 5] OneDrive Screenshot saved The screenshot was added to your OneDrive. 5:00 PM 10/31/2022 E Ensign Homepage C D LDS Quiz: 8.3 Quiz (30 min.) + ensign.instructure.com/courses/14050/quizzes/186729/take FAFSA Student Loan ck Credit Karma M Gmail 25 iCloud byu chat page TestOut LabSim Course Hero 2022 Fall Semester Home Announcements 8.3 Quiz (30 min.) Started: Oct 31 at 3:59pm Quiz Instructions Syllabus Modules K Grades People Tutoring Resources 69F Mostly cloudy Question 1 If I wanted to print the first element in an array, how would I do it? int[] array = {1, 2, 3, 4, 5}; System.out.println(array[1]); int[] array = {1, 2, 3, 4, 5}; System.out.println(array[0]); Question 2 1 pts 1 pts Questions Question 1 Question 2 Question 3 Question 4 Question 5 Question 6 Question 7 Question 8 Time Elapsed: Hide Time Attempt due: Nov 5 at 10pm 0 Minutes, 12 Seconds 4:59 PM 10/31/2022

Step by Step Solution

3.25 Rating (146 Votes )

There are 3 Steps involved in it

Step: 1

1 Correct Option is b Systemoutprintlnarray0 The first element of array is present at index 0 Hence ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual C# How to Program

Authors: Paul J. Deitel, Harvey Deitel

6th edition

134601548, 134601793, 978-0134601540

More Books

Students also viewed these Programming questions

Question

Compare and contrast the while and for iteration statements.

Answered: 1 week ago

Question

What does the following code segment do? for (int i = 1; i

Answered: 1 week ago