Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Data Structures & Algorithms Lab 1(JAVA) Qu,1 Which of the following statements are correct? If false explain why? (a) The following code executes without any

Data Structures & Algorithms Lab 1(JAVA)

Qu,1 Which of the following statements are correct? If false explain why?

(a) The following code executes without any error: ArrayList first = new ArrayList<>(); first.add(10);

(b) Because ArrayList stores only objects, we cannot pass an element of an ArrayList to a switch structure.

(c) If we call methods clear( ) or remove( ) on an ArrayList, it will remove all of its elements.

(d) If we frequently add elements to an ArrayList, specifying a larger capacity will improve the code efficiency.

(e) An ArrayList offers a resizable array, which is easily managed using the methods it provides. We can add & remove elements from an ArrayList.

(f) The values stored by an ArrayList can be modified.

(g) We can iterate through the elements of an ArrayList using a for loop, Iterator, or ListIterator.

(h) An ArrayList requires that we specify the total number of elements before we can store any elements inside.

(i) An ArrayList can store any type of object.

(j) An ArrayList can store elements of data type int.

Qu.2

(a) Create an ArrayList called 'listOne' of type String.

(b) Create four String objects: one, two, three, four to store the String values:

{one, two, three, four} (c) Add the String objects to the ArrayList 'listOne'.

(d) Create a second ArrayList called 'listTwo' and assign it the contents of 'listOne'.

(e) Output the contents of listOne.

(f) Output the contents of listTwo.

(g) Create an iterator called 'mona' to reference listTwo.

(h) Use a while loop to iterate over the elements of listTwo. If the element equals two, remove it from the list.

(i) Again output the elements of listTwo using an enhanced-for loop.

(j) Display the size of listTwo

Qu.3

Write down a program using 'ArrayList' to perform the following tasks:-

(a) Declare one ArrayList of data-type String called 'mobile_list'

(b) Declare integer variables: counter1, counter2

(c) Declare String variables: input, output, output2

(d) Declare one JTextArea called: 'area1'

(e) Write down a for loop using counter1 and variable input to populate 'mobile_list' with items:

{nokia, samsung, huawei, ericsson, captur, nexus, bligtz, symphony, maxwel, tiger}

(f) Write down one statement to sort the mobile list in ascending order

(g) Write down statements to display each item of the sorted mobile list

(h) Write down one statement to remove the second element of 'mobile_list'

(i) Write down one statement to remove the ninth element of 'mobile_list

(j) Write down a for loop using counter2, to display the updated list of mobile elements using the text area 'area1'.

(k) End the program

Qu.4

(a) What is the output of the program given below? Explain fully your answer.

(b) What are the changes needed to run the program?

(c) Write down all the outputs.

class EWidget{

static public void main(String[ ] s)

{ ArrayList ewd = new ArrayList<>();

ewd.add(1,ben);

ewd.add(2,charles");

System.out.println(ewd.contains(new String(ben)));

System.out.println(ewd.indexOf(charles"));

ewd.clear( );

System.out.println(ewd);

System.out.println(ewd.get(1));

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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_2

Step: 3

blur-text-image_3

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

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions