What happens after the following code execution?

class Test {
    public static void main( String[] args ) {
        List list = new ArrayList( 3 ); //1

        list.add( new Integer( 100 ) );
        list.add( new Integer( 200 ) );
        list.add( new Integer( 300 ) );
        list.add( new Integer( 400 ) ); //2

        System.out.println( list.size() ); //3
    }
}
Explanation
Size of the list is set to 3. However, in a case of trying to add an element beyond its boundaries, the size will be dynamically expanded. Therefore, compilation will be successful and 4 will be printed out.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback