Menu
Topics Index
...
`

Remove2440


If you need explanation Read this topic

If you need Answer Take test on this topic

Remove2440
What will be the output of the following program?
import java.util.*;
public class Remove {
    public static void main(String args[]) {
        ArrayList arr = new ArrayList();
        arr.add("some");
        try {
            for (int i = 0; i < 4; i++) {
                arr.get(i);
            }
            System.out.print(arr);
        } catch (IndexOutOfBoundsException e) {
            System.out.print("index error");
        }
    }
}

A. some
B. index error
C. Some other output
D. Compilation Error
E. Runtime Error
Topic: Java ArrayList

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

B... Because  we are accessing the ArrayList Out of bound ...

Posted by ?????????? ?????    2015-01-05 13:07:11


Answer is B...when trying to index the second element of the list, i.e arr[1],IndexOutofBoundsException is thrown and control enters catch block and prints index error and program is terminated in this case as there is no method in the call stack

Posted by Shashanka Mogaliraju    2015-01-05 13:11:16


ANS IS B

Here ArrayList data structure is used.

Arraylist is Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null.

--add() method is used to add the String element."some" is added into this arraylist at index 0.

--get() method is used to dislay the elements present in this arraylist using index of the element passed as argument.

--Try Catch block is used to catch any exception occur in the statements written inside "TRY" block.as only one element is present in arraylist at index 0.but for loop runs 4 times which causes the indexoutofbounds exception."Index Error" will get display written inside "CATCH" block.

Posted by Mânïshå Mùlchåndânï    2015-01-05 17:42:16


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment'. The 'lucky liker' is Gopinath Manchikanti. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-01-06 05:23:37


© meritcampus 2019

All Rights Reserved.

Open In App