Menu
Topics Index
...
`

Classes858


If you need explanation Read this topic

If you need Answer Take test on this topic

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

class Count extends Count2
{
    Count()
    {

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

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

A. Output = 0 2 4 6
B. Output =
C. Compilation Error
D. 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.

a

Posted by Shubham Bansal    2015-02-09 15:39:41


here option is A
in main method() one object was created for class Count{}
but it inherits another class called class Count2{}
all methods in count2 are inherit into count class
now Count class consist one constructer and one member function
they are Count(){}
void count(){}
these names looking like same but they are differnt because first letter was different in both of these names
when object created for Count its excuted the constructer it does not have any instruction
and next we call another method count{}  it prints output 0 2 4 6
thats why option is A
here main point was here they are same name method called(Count(){},void count(){})
first letter in both names are different because one is capital,one smaller thats why they are different

Posted by Goutham Singarapu    2015-02-09 15:53:46


ANS IS A (0 2 4 6)

--Here,concept of classes and objects with inheritance is used.

--Here two classes are used:
1)count2 (BASE CLASS)
2)Count (SUBCLASS)

--Here,object of count class is created which calls the default constructor of count class where nothing is defined.

--After that,using that object of subclass method count() is called in which for loop is written which runs from 0 till 7

x=0 x<7 (true)
0
x++,x++;x=2

x=2 x<7 (true)
2
x++,x++;x=4

x=4 x<7 (true)
4
x++,x++;x=6

x=6 x<7 (true)
6
x++,x++;x=8

x<7(false),therefore it terminates here.

--OUTPUT: 0 2 4 6.

Posted by Mânïshå Mùlchåndânï    2015-02-09 17:32:29


ans a. here we have main class output,here we created count class object, t.count() invokes default constructor from count class.this class extends or inherits all the features of count2 class.so void count() get executed. here count2 base class,count is subclass which is derived from count2 class.void count() get executed sop prints "output" then for loop x is initialized with0, nxt it iterates upto it reach 7,here post increment operation is 2 times executed. so for each iteration x value get incremented by 1 twice. x is 0 ,x<7,sop prints 0, x increments twice,x=2,x<7,sop prints 2,increments to 4,x<7,sop prints 4,x get incremented x is 6,x<7,sop prints 6 ,nw increment operation executed xis 8,x<7, condition failed control goes out of loop.,0 ,2,4,6 get printd

Posted by Maheshwari Natarajan    2015-02-09 18:54:08


Ans is A...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 Count class invoked before invoking the child class Constructor  it will invooke the Parent class Constructor..next we are calling the count() so it will execute the count() method which is Inherited from the Parent..in that we are printing the even num bw from 0-7.here we are increasing the x twice every tiime we will print even num only..

Posted by Uday Kumar    2015-02-10 03:34:02


This dose is now closed and the winners are Goutham Singarapu,  for 'First Correct Comment', Mânïshå Mùlchåndânï, Maheshwari Natarajan, Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Urvashi Arya. 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-10 04:29:12


© meritcampus 2019

All Rights Reserved.

Open In App