Menu
Topics Index
...
`

Class with multiple constructors


If you need explanation Read this topic

If you need Answer Take test on this topic

Class with multiple constructors
What will be the output of the following program?
class Tricky {
    int num;
    Tricky(int n) {
        num = n;
    }
    Tricky inc() {
        Tricky temp = new Tricky(num + 10);
        return temp;
    }
}

public class TrickyQuestion {
    public static void main(String s[]) {
        Tricky num1 = new Tricky(10);
        Tricky num2;
        num2 = num1.inc();
        System.out.print("The program will display the output as: ");
        System.out.print(num1.num + ",");
        System.out.print(num2.num + ",");
        num2 = num1.inc();
        System.out.print(num2.num);
    }
}

A. The program will display the output as:
B. The program will display the output as: 10,10,10
C. The program will display the output as: 10,20,30
D. The program will display the output as: 10,20,20
E. Compilation Error or Runtime Error
Topic: Class With Multiple 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 D.. as num1 contains num value as 10 and then num2 objects num vl be incrmntd so it 20 ...

Posted by Om Prakash Om    2014-05-29 04:06:58


Ans is d as num1 is 10 it prints and num2 is 20 as incremented in the inc method of tricky class and prints values 10 20 20

Posted by Bharath Yelchuri    2014-05-29 04:07:24


ans d. num1 is assignd 10,
num1 incremnt and stored in num2,
temp variable is used to stor the incremntd num1.
so value of num1 remains same 10,
again 1 tim num1 incrmnt and assignd to num 2

Posted by Maheshwari Natarajan    2014-05-29 04:19:29


option is d

Posted by Vijay Kumar    2014-05-29 04:53:27


solution is d

Posted by RAweeteja Bhonagiri    2014-05-29 11:26:27


Congratulations Om Prakash Om. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-05-30 06:00:16


© meritcampus 2019

All Rights Reserved.

Open In App