Menu
Topics Index
...
`

Classes875


If you need explanation Read this topic

If you need Answer Take test on this topic

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

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

class Integer2
{
    byte x;
}

A. 0 0 0
B. null null 42
C. 0 0 42
D. 0 42 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.

ans is D,t.x prints 0(byte default value),tt object reference copied to the reference variable t2 ,here t2.x holds 42 and t.x also holds 42

Posted by Raviteja Daggupati    2014-03-20 08:48:47


e because integer value is not declare

Posted by Aruna Dantu    2014-03-20 09:04:02


Answer is D. First SOP statement prints out 0 (as the default value of a byte [instance or static] variable is 0). Then fix(t) passes the value of t to fix (It doesn't pass the object that t ponits to. It is same as passing a pointer to the object). Here the value of field "x" is changed to 42. This change reflects the original object that is pointed to by t. Thus, second SOP prints out "42" (t.x) and "42" (t2.x).

Posted by Shaileshwar Sharma    2014-03-20 12:09:52


ans is E.. B'coz interger value directly we cann't assign to byte variable..

Posted by Nagendra Penumalli    2014-03-20 17:21:14


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

Posted by Merit Campus    2014-03-21 04:16:50


© meritcampus 2019

All Rights Reserved.

Open In App