Topics Index

Family 2437

If you need explanation Read this topic

If you need Answer Take test on this topic

Family 2437
What will be the output of the following program?
public class Family {
    public static void main(String args[]) {
        String array[] = new String[-3];
        array[0] = "father";
        array[1] = "mother";
        array[2] = "children";
        String str = "";
        for (String element : array) {
            str += element + "";

A. father mother children
B. Runtime Error because of java.lang.NullPointerException
C. Runtime Error because of java.lang.ArrayIndexOutOfBoundsException
D. Runtime Error because of java.lang.NegativeArraySizeException
E. Runtime Error because of some other reason
Topic: Java Built In Exceptions Checked Exceptions, Unchecked Exceptions

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.

D is the ans ovisously. as we are trying t create negative strnig aray

Posted by Shabaaz Shaikz    2014-10-17 12:13:12

because array is declared -3 that is less than 0.
hence array always starts from 0 index so thats why negative array exception occur

Posted by Shubham Bansal    2014-10-17 12:14:37

Ans is we are trying to create an array with size -2..which is not meaningful..
size of the arrays must always positive compiler not going to give any error Bcz compiler wil check wthether you gave Integer or  not..if we give any other values..(flaot,double) than compiler wil give here compiler wil nt gv any error...But In java we have Built in Exception named with NegativeArraySizeException, if we try to give array with ngtv size than this exception wil thrown at run time

Posted by Uday Kumar    2014-10-17 15:14:50

ans d.

in this program String array size is initialized with negative numbered.

actually declaration mean allocating memory for required type element.

here string array try to attempt memory allocation  for negative value -3.
which is not possible or an illegal method.

this is unchecked exception,compiler just act as translater between user program and kernel.
linker are used to perform library builtin functions

java virtual machine is responsible for native machine declaration of string array line throws negativeArraySizeException.

Posted by Maheshwari Natarajan    2014-10-17 17:23:25

This dose is now closed and the winners are Shubham Bansal,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Šâí Râm for the 'Popular Comment'. The 'lucky liker' is Uday Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-10-20 13:03:25

© meritcampus 2019

All Rights Reserved.

Open In App