MCQ IN COMPUTER SCIENCE & ENGINEERING

COMPUTER SCIENCE AND ENGINEERING

DATA STRUCTURES

Question [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Which is the right way to create an ArrayList of Integers?
A
ArrayList<Integer> list = new ArrayList<Integer>;
B
ArrayList Integer = new ArrayList();
C
Integer ArrayList = new Integer ArrayList();
D
ArrayList<Integer> list = new ArrayList<Integer>();
Explanation: 

Detailed explanation-1: -ArrayList<Integer> numbers = new ArrayList<>(Arrays. asList(1, 2, 3, 4, 5, 6)); This is how you declare an ArrayList of Integer values. You can do the same to create an ArrayList with String objects as well, e.g.

Detailed explanation-2: -The general syntax of this method is: ArrayList<data type> list name = new ArrayList<>(); For Example, you can create a generic ArrayList of type String using the following statement. ArrayList<String> arraylist = new ArrayList<>(); This will create an empty ArrayList named ‘arraylist’ of type String.

Detailed explanation-3: -The size of an ArrayList can be obtained by using the java. util. ArrayList. size() method as it returns the number of elements in the ArrayList i.e. the size.

Detailed explanation-4: -In Java, we can create ArrayList by creating this simple statement: ArrayList<String> arlist = new ArrayList<String>( ); In above syntax, list is of “String” type, so the elements are that going to be added to this list will be string type. The type decide which type of elements list will have.

Detailed explanation-5: -You can also create ArrayLists of integer values. However, you have to use Integer as the type because ArrayLists can only hold objects, not primitive values. All primitive types must be wrapped in objects before they are added to an ArrayList.

There is 1 question to complete.