Menu
Topics Index
...
`

Classes inside the class1945


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes inside the class1945
What will be the output of the following program?
public class Price {
    public void doIt(int i, HtcMobile p) {
        i = 10000;
        p.number = 1;
    }
    public static void main(String args[]) {
        int x = 2000;
        HtcMobile p = new HtcMobile();
        new Price().doIt(x, p);
        System.out.println("Price: " + x + ", Mobile Count: " + p.number);
    }
}
class HtcMobile {
    public int number;
}

A. Price: 2000, Mobile Count: 1
B. Price: 10000, Mobile Count: 1
C. Price: 2000, Mobile Count: 0
D. Price: 10000, Mobile Count: 0
E. Some other output
F. Compilation Error or Runtime Error
Topic: Behavior Of Java Classes Using Methods

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.

B.
As x is local to main and value remains 2000.
As far as p.number is concerned, it is being initialized in doIt() method

Posted by Abhishek Kathuria    2014-05-06 12:45:22


Sorry I mistyped, Answer is C. However according to explanation its
"Price : 2000 Mobile Count : 1"

Posted by Abhishek Kathuria    2014-05-06 12:48:35


Ans is A. x's value is printed as it is because it lies within the scope of main method and when doIt method in Price is invoked the value of number in the object is initialized to 1.

Posted by Nikhil Sulibhavi    2014-05-06 13:03:44


ans is A as x=2000 is initialised in main method of price class and is printed and when doIt method is invoked as p.number is 1 in doIt method as p is a object to HtcMobile class and prints its avalue as 1

Posted by Naga Sinst    2014-05-06 13:12:21


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

Posted by Merit Campus    2014-05-08 04:04:02


© meritcampus 2019

All Rights Reserved.

Open In App