What is the output of the following program:import java.util.*;class TestJavaCollection1 { public static void main(String args[]) { ArrayList<String> list = new ArrayList<String>(); // Creating arraylist list.add("Apple"); // Adding object in arraylist list.add("Orange"); list.add("Strawberry"); list.add("Apple"); // Traversing list through Iterator Iterator itr = list.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } }}
Question
What is the output of the following program:import java.util.*;class TestJavaCollection1 { public static void main(String args[]) { ArrayList<String> list = new ArrayList<String>(); // Creating arraylist list.add("Apple"); // Adding object in arraylist list.add("Orange"); list.add("Strawberry"); list.add("Apple"); // Traversing list through Iterator Iterator itr = list.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } }}
Solution
The output of the program will be:
Apple Orange Strawberry Apple
This is because the program creates an ArrayList of Strings and adds the strings "Apple", "Orange", "Strawberry", and "Apple" to the list. It then creates an Iterator object to traverse through the list. The while loop continues as long as there are more elements in the list (as indicated by itr.hasNext()), and for each iteration, it prints out the next element in the list (
Similar Questions
import java.util.*;class TestJavaCollection1 { public static void main(String args[]) { ArrayList<String> list = new ArrayList<String>(); // Creating arraylist list.add("Apple"); // Adding object in arraylist list.add("Orange"); list.add("Strawberry"); list.add("Apple"); // Traversing list through Iterator Iterator itr = list.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } }}Select one:a.AppleOrangeStrawberryAppleb. No outputc.AppleOrangeStrawberryd. Compilation error
What is the output of the following code?import java.util.*;public class Main { public static void main(String[] args) { List list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); Iterator iterator = list.iterator(); while (iterator.hasNext()) { Integer value = iterator.next(); if (value == 2) { iterator.remove(); } } System.out.println(list); }}a)[2, 3]b)[3, 2, 1]c)[1, 2, 3]d)[1, 3]
What is the output of the following program:import java.util.*;public class linked_list { public static void main(String args[]) { LinkedList<String> ll = new LinkedList<String>(); ll.add("Mercedes"); ll.add("Audi"); ll.add("BMW"); Iterator i = ll.descendingIterator(); while (i.hasNext()) { System.out.println(i.next()); } }}Select one:a.BMWAudiMercedesb.AudiMercedesBMWc.MercedesBMWAudid.MercedesAudiBMW
What is the output of the following program:import java.util.ArrayList;class Main { public static void main(String[] args){ ArrayList<String> animals = new ArrayList<>(); animals.add("Dog"); animals.add("Cat"); animals.add("Horse"); animals.remove(2); System.out.println("ArrayList: " + animals); }}Select one:a.ArrayList: [Dog, Cat]b.ArrayList: [Horse]c.ArrayList: [Dog, Cat, Horse]d.ArrayList: [Dog]
What is the output of the following code snippet?ArrayList<String> fruits = new ArrayList<>();fruits.add("Apple");fruits.add("Orange");fruits.add("Banana");System.out.println(fruits.size());Question 17Answera.1b.2c.3d.0
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.