Menu
Topics Index
...
`

Classes877


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes877
What will be the output of the following program?
class OutPut
{
    public static void main(String []args)
    {
        OutPut O = new OutPut();
        O.string();
    }
    
    void string()
    {
        OutPut1 t = new OutPut1();
        System.out.print(t.x + " ");

        OutPut1 t2 = fix(t);
        System.out.println(t.x + " " + t2.x);
    }
    
    OutPut1 fix(OutPut1 tt)
    {
        tt.x -= 42;
        return tt;
    }
}

class OutPut1
{
    byte x = 42;
}

A. 42 0 -42
B. 42 0 42
C. 42 42 0
D. 42 0 0
E. Compilation Error
Topic: Java Multiple Methods In One Class

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..here we have the 2 classes named with the OutPut and OutPut. in the main() we create the instance for the class OutPut and we are invoking the string() using the instnace..in the string method we create the instance for the class OutPut1 so here memory is allocated for variables x and assigned with the value 42 we are printing the t.x so it will print as 42 next we are calling the fix() by passing the t  instance in the fix() we are subtractiong 42 from the t.x so it will become the 0 and now we are returning the instance which we received so now this will copied into the t2 when we print t1.x and t2.x is 0 Bcz we subtracted 42 so it became 0

Posted by Uday Kumar    2015-02-10 03:53:38


d

Posted by Shubham Bansal    2015-02-10 12:29:34


ans d..here we have output,Output1 -2 classes, output instance object is created in main method,by using that reference we called string(),it invokes default constructor,here we created object for Output1,t.x prints 42."." operator is used to access output1 class variable x( instance variable), thn we called fix() function and value t is passed as an argument.here return type is output 1,coming to the fix function definition, value of tt which is t subtracted by 42,so 42-42 is 0,nw object t value is 0,bcz we uses assignment operation.return statement returns 0.sop print 0 0

Posted by Maheshwari Natarajan    2015-02-10 19:37:50


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


© meritcampus 2019

All Rights Reserved.

Open In App