Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


What will be the output of the program?

public class Test {
public static void main(String args[]) {
ArrayList<String> list = new ArrayList<String>();
list.add("2");
list.add("3");

list.add("4");
list.add("5");
System.out.println("size :"+list.size());
for(int i=0;i<list.size();i++) {
list.remove(i);
}
System.out.println("size after:"+list.size());
}
}

Answers were Sorted based on User's Feedback



What will be the output of the program? public class Test { public static void main(String..

Answer / sreekanth madamanchi

Step 1: The array size: 4
array is list[0]=2, list[1]=3, list[2]=4, list[3]=5;

i=0;i<4;i++
it removes the 0th element, means it removes 2.
now the array is list[0]=3, list[1]=4, list[2]=5

Step 2: Now the size of the array is 3
now i=1 (bcz i++)
i<3;i++
it removes the 1st element, means it removes 4 (Bcz i=1).
now the array is list[0]=3, list[1]=5

Step 3: Now the size of the array is 2
now i=2
the condition is fail in for loop (bcz i=2. means 2<2)
So it won't go inside the loop

So the size after:2

The final Output is
size :4
Size after:2

Is This Answer Correct ?    13 Yes 0 No

What will be the output of the program? public class Test { public static void main(String..

Answer / ggpune

Output will be
size :4
Size after: 2

Is This Answer Correct ?    5 Yes 2 No

What will be the output of the program? public class Test { public static void main(String..

Answer / surendrababu koppula

ArrayList class remove method example.remove(int index) Removes the element at the specified position in this list. Shifts any subsequent elements to the left.

Is This Answer Correct ?    1 Yes 0 No

What will be the output of the program? public class Test { public static void main(String..

Answer / vanjinathan

How it comes 2 after removing the list elements.??

please explain..??

Is This Answer Correct ?    0 Yes 0 No

What will be the output of the program? public class Test { public static void main(String..

Answer / qim2010

The final Output is
size :4
Size after:2

While called ArrayList.remove() method, in remove method, we
can pass object or index number to remove.

// remove array list element by index number
list.remove(2); //will remove the second element

// remove ArrayList element by Object value
list.remove("2"); //will remove the element

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More Core Java Interview Questions

What is identifier give example?

0 Answers  


what is meant by package in java?

5 Answers  


Is 0 an irrational number?

0 Answers  


What's the default access specifier for variables and methods of a class?

0 Answers  


What is a java list?

0 Answers  


How to create a custom exception?

0 Answers  


How does compareto method work?

0 Answers  


Is java a pure object oriented language?

0 Answers  


Variables used in a switch statement can be used with which datatypes?

0 Answers  


How to re-get an object that is collected by garbage collector?

1 Answers  


If you are reviewing the code of your team members, what points will you look at, assuming the performance of the application is not so great

2 Answers   KPIT,


what is daemon thread and which method is used to create the daemon thread? : Java thread

0 Answers  


Categories