Menu
Topics Index
...
`

Classes859


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes859
What will be the output of the following program?
class OutPut
{
    public static void main(String [] args)
    {
        Count2 count = new Count2();
        count.count();
    }
}

class Count extends Count2
{
    Count()
    {

    }
}
class Count2
{
    void count()
    {
        int i = 5;
        System.out.print("Output = ");

        for (int x = 0; x <= (i % 7); x += 1)
        {
            System.out.print(" " + x);
        }
    }
}

A. Output = 0 1 2 3 4 5 6 7
B. Output = 0 1 2 3
C. Output = 0 1 2 3 4 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 C.
Here we are creating an object for class Count2 and calling count()... In that we are declaring i with 5 and looping until x becomes equal to (i%7) i.e., 5... so it iterates until x becomes 5 and prints value of x from 0 to 5...
output = 0 1 2 3 4 5

Posted by Gopinath Manchikanti    2015-02-16 14:10:19


Ans is C...here we have the Inheritance concept where Child class inherits the properties of the Parent class except private members..in the given program we have Count2 class as Parent class and Count as Child class so here Count class is Inheriting the Count2 class so Count class is Child and Count2 class is Parent so Child class Count will inherit the properties of the Count2 class it has one method void count() it will inherited into the Count class..
Execution:in the main() we create the instance of the Count class so Constructor in the Count2 class invoked before invoking the child class Constructor it will invooke the Parent class Constructor..next we are calling the count() with the Parent class instance so it will execute the count() method in the Parent..in that we are printing num bw from 0-5

Posted by Uday Kumar    2015-02-17 02:56:33


This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Bolo Raven. 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    2015-02-17 04:07:48


© meritcampus 2019

All Rights Reserved.

Open In App