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?
class Room {
    int r1, r2;
class House extends Room {
    int paints;
public class Windows {
    public static void main(String[] args) {
        Room[] r = new House[2];
        r[1] = null;
        try {
            r[0] = new Room();
        } catch (ArrayStoreException e) {
            System.out.println("array store exception");
        } finally {
            System.out.print("final block execute");

A. null
array store exception
B. array store exception
final block execute
C. null
final block execute
D. null
array store exception
final block execute
E. final block execute
F. Compilation Error or Runtime Error
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.

Answer is D ....printing r[1] will print null as it is assigned value of null...r[0] = new Room() will throw an arraystoreexception because we are actualy tring to store wrong kind of object(can store house object but not room ) into array,.Control goes into catch block and then finally will execute whenever try is executed

Posted by Shashanka Mogaliraju    2014-11-07 08:52:57

ans is d,, r[0]=new room() is a wrong declaration

Posted by Raju VarshnEy    2014-11-07 09:01:18

ans is d..

Posted by Shubham Bansal    2014-11-07 14:02:48

Ans is we have Room class As parent class to the House class...

Coming to the main class here we creata array of House objects with size pointed by the Room class java Parent class Reference can refer Child Object class Object...

in 1st location we are assigning null to it than we are printing that value...than it wil print as null...than we are creating Object of the Room class try to to Store that in the Room class reference here initially it is pointed by the House objects so that here ArrayStoreException will raise than it wil enter into catch block than here it will print "array store exception"...than finally is an block which wil executes always.......

Posted by Uday Kumar    2014-11-07 15:40:51

ans d.           in this program we have  3 classes.window is main class, house class extends room class. in main method first array is created .room[] array assigned with house[2] array 1st r[1]  is assigned with null object,null mean r[1] pointing to nothing.sop prints r[1] value which is null. thn r[0] assigned with room object which is incompatible with previously assigned one .so this line throws run time error-ArrayStoreexception which extends runtimeexception.this exception is thrown whn there is an attempt to store a value in an array element that is incompatible with the type of the array.that is why here try-catch mechanism is used to handle this runtime try block throws error thn catch block get executed sop printd.finally block is always executed regardless of the execution of catch block.

Posted by Maheshwari Natarajan    2014-11-09 05:26:31

This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Asma Mujtaba Khan. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-11-10 01:13:14

© meritcampus 2019

All Rights Reserved.

Open In App