Topics Index


If you need explanation Read this topic

If you need Answer Take test on this topic

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();
        try {
            for (int i = 0; i < 4; i++) {
        } 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


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" 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 to raise the recharge.

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

© meritcampus 2019

All Rights Reserved.

Open In App