Menu
Topics Index
...
`

Constructors Output Test


If you need explanation Read this topic

If you need Answer Take test on this topic

Constructors Output Test
What will be the output of the following program?
class ConstructorsTest
{
    public static void main(String[] args)
    {
        U u = new U();
        U u1 = new U(1);
        V v = new V();
        V v1 = new V(1);
        T t = new T();
        T t1 = new T(1);
    }
}

class U {
    V v = new V();

    U() { System.out.print("U"); }

    U(int i) { System.out.print("u");}
}

class V {
    V() { System.out.print("V"); }

    V(int i) { System.out.print("v"); }
}

class T extends V {

    T() { System.out.print("T"); }

    T(int i) { System.out.print("t"); }
}

A. UuVvVTvt
B. VUVuVvVTvt
C. VUVuVvVTVt
D. Compilation Error
E. Runtime Error - goes into infinte loop
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,,

Posted by Raju VarshnEy    2014-11-03 04:39:46


ans c. objects for all the classes created in main class.1st object U() executed and prints VU bcz inside class U Clas V() obj cald.so default constructor printd.2 nd prints Vu.3 rd prints V,4 th prints v,5th prints VT because T extends V.,6 th prints vt

Posted by Maheshwari Natarajan    2014-11-03 05:49:10


C

Posted by Nichitha Veludandi    2014-11-03 07:51:58


c

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


Ans is C...here we have 4 classes and these classes have 2 different constructors...

here in these classes constructor overloading occur sam name with different type n num n order of args

1st we are creating instance for the U and in this class we are creating the instance for the class V without any args so that default constructor in that class wil get executed and printed V..
and Constructor in class U get printed next we are creating instance for the U by passing the value so that 1 arg constrctr in U wil get executed....

next we are creating instance for the V than default constructor wil get executed....similar to v also...

Next we are creating instance for class T than default construcotor in its parent clas wil get executed here V is Parent for T...VT get printed...next creating instance with 1 arg so that again parent class default cons wil printed next 1arg cons in T get printed

Posted by Uday Kumar    2014-11-03 16:18:20


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 Sarfaraz Ahmed. 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 03:57:14


© meritcampus 2019

All Rights Reserved.

Open In App