Menu
Topics Index
...
`

Classes876


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes876
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 42 84
B. 42 84 84
C. 42 84 126
D. 42 84 42
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.

44 48 48

Posted by Arandala Vinay Kumar    2015-02-17 12:58:41


ans b..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 added by 42,so 42+42 is 84,nw object t value is 84,bcz we uses assignment operation.return statement returns 84.sop print 84 84

Posted by Maheshwari Natarajan    2015-02-17 13:02:21


b

Posted by Shubham Bansal    2015-02-17 17:10:34


ANS IS B 42 84 84

Here the concept of passing object reference i.e. PASS BY ADDRESS is used.

--First of all object of class output is created and string method is called usind that object.

--In string method object of class output 1 is created as class output 1 contains byte variable x with value 42,object of output 1 is used to display value of X. So 42 will get printed.

--After that another object of output 1 is created and fix method() is called by passing previously created object of class output 1 which holds x=42. It is incremented by 42 I.e. X=84 .Now this method returns the object to t2 object. As object reference is passed not the variable so values are changed permanently as here concept of PASS BY ADDRESS is used.

--Now object T1 and t2 will be holding 84 for value of X. They are printed using println() statement.

OUTPUT:
42 84 84

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


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


© meritcampus 2019

All Rights Reserved.

Open In App