Home / Java Programming / Language Fundamentals :: Discussion

Discussion :: Language Fundamentals

  1. What will be the output of the program?

     public class TestDogs  
     {   
        public static void main(String [] args)        
        {
           Dog [][] theDogs = new Dog[3][];         
           System.out.println(theDogs[2][0].toString());    
        } 
    }
     class Dog { } 
    

  2. A.

    null

    B.

    theDogs

    C.

    Compilation fails

    D.

    An exception is thrown at runtime

    View Answer

    Workspace

    Answer : Option D

    Explanation :

    The second dimension of the array referenced by theDogs has not been initialized. Attempting to access an uninitialized object element (System.out.println(theDogs[2][0].toString());) raises a NullPointerException.


Be The First To Comment