Menu
Topics Index
...
`

Classes895


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes895
What will be the output of the following program?
class OutPut
{
    public static void main(String s[])
    {
        int i = 2;
        B b = new B();
        b.execute();
    }
}

class A
{
    int i = 0;
    A(){}
}

class B extends A
{
    void execute()
    {
        for(; i < 6; i++)
        
            switch(i)
            {
                case 0:
                System.out.println("i is zero.");
                break;
        
                case 1:
                System.out.println("i is one.");
                break;
        
                case 2:
                System.out.println("i is two.");
                continue;
        
                case 3:
                System.out.println("i is three.");
                break;
        
                default:
                System.out.println("i is greater than three.");
            }
    }
}

A. i is zero.
i is one.
i is two.
i is three.
i is three.
i is greater than three.
i is greater than three.
B. i is zero.
i is one.
i is two.
i is three.
i is greater than three.
i is greater than three.
C. i is two.
i is three.
i is three.
i is greater than three.4
i is greater than three.5
D. Compilation Error
E. Runtime Error
Topic: Inheritance And Constructors In Java

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.

Ans is B,,, n.execute take i=0 initially,, by its super class

Posted by Raju VarshnEy    2014-11-03 09:04:30


Answer is D...it throws compiler error since we are not allowed to use the continue statement inside a switch block

Posted by Shashanka Mogaliraju    2014-11-03 11:01:47


ans b.here class B inherits all the features of class A .so i value it takes 0.continue statement also allowed.bt in this program it does'nt show major difference

Posted by Maheshwari Natarajan    2014-11-03 11:26:58


b.

Posted by Shubham Bansal    2014-11-03 11:42:55


Ans is B...here we have inheritance concept...child class wil inherit the properties of the Parent class...

in the execute() the i which we are using is from the parent class only...

using for loops we are iterrating 5 times...every time we are passing i value to switch case...which is used to execute one statement out of n statements...here initially 0 is passed and i is zero get printed...
break--> is used to stop executing the remaining cases..here continue wil comes under the for loop...which is used to skip the remaining statements in current iteration and goes to next iteration....

default is an special case which wil execute if no case lable is matched with given value in switch....

Posted by Uday Kumar    2014-11-03 16:11:01


This dose is now closed and the winners are Maheshwari Natarajan,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Sai Ram. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-04 04:04:39


© meritcampus 2019

All Rights Reserved.

Open In App