Menu
Topics Index
...
`

final variables output


If you need explanation Read this topic

If you need Answer Take test on this topic

final variables output
What will be the output of the following program?
public class Final {
    int lanif = 37;
    int nafi = 21;
    public static void main(String[] args) {
        final Final f = new Final();
        f.process2();
        Final f2 = modify(f);
        f2.process2();
    }
    public static final Final modify(final Final f) {
        f.process();
        Final f2 = new Final();
        f2.process();
        return f2;
    }
    final void process() {
        lanif = nafi + nafi;
        System.out.print(lanif + " " + nafi + " ");
    }
    void process2() {
        nafi = lanif / 2;
        System.out.print(nafi + " " + lanif + " ");
    }
}

A. 42 21 21 42 18 37 36 18
B. 18 37 36 18 18 37 36 18
C. 18 37 36 18 42 21 21 42
D. Compilation Error
Topic: final Keyword 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.

D,missing return statement

Posted by Prasad Kallu    2014-03-10 11:40:42


correct ans is C,first  f object is created then f.process2() is called here the variable nafi becomes 18 and  prints 18 37,next a reference variable f2 is created then modify() method is called which is static then it calls f. process() then here lanif becomes 36(nafi is 18) and prints 36 18,next f2 object is created and lanif becomes 42(nafi is 21) and prints 42 21 and returns f2 object,f2.process2() prints 21 42

Posted by Raviteja Daggupati    2014-03-10 15:58:19


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-11 04:24:09


© meritcampus 2019

All Rights Reserved.

Open In App