Menu
Topics Index
...
`

Final keyword916


If you need explanation Read this topic

If you need Answer Take test on this topic

Final keyword916
What will be the output of the following program?
public class Final
{
    int a = 30;

    public static void main(String[] args)
    {
        final int assign;

        Final b = new Final();
        process(b);
        System.out.println(b.a);
        
        process(b);
        assign = b.a;
        System.out.println(assign);
    }
    
    public static void process(Final a)
    {
         a.a = a.a + 5;
    }
}

A. 30
35
B. 35
35
C. 35
40
D. Compilation Error
E. Runtime 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.

b.both th prnt stmt wil produce samw outpt.

in frst cal to process()..the value of a is aassigned to b..
n prntd ..
in sexnd cal agn 5 is assgnd..n also assign=b.a;
here assign has not b gvn any value earlier hence it can be assigned ovr here..so assign=5.
hemce result is prntd

5
5

Posted by Nasir Khan    2014-09-11 04:16:13


ans is C.
we have initialized final variable before using it. So it doesn't give compilation error.
for the first call of process() b.a will be 35.
for the second call of process() b.a is 40 and is assigned to  final variable 'assign'.

so it prints
35
40.

Posted by Gopinath Manchikanti    2014-09-11 09:09:23


ans is C.we have initialized final variable before using it. So it doesn't give compilation error.for the first call of process() b.a will be 35.for the second call of process() b.a is 40 and is assigned to final variable 'assign'.so it prints3540

Posted by Shamee Shamee    2014-09-11 12:31:32


answer.d
if variable declared with final need to initialized at the tim of declaration.
compilation error

Posted by NagaRaj Nataraj    2014-09-11 17:46:10


answer e.
cant assign value to final variables at run tim.
need to assignd at the tim of declartn

Posted by Anand Kumar    2014-09-11 17:49:41


c :35 :40

Posted by Akshay Bhatwadekar    2014-09-12 03:24:35


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

Posted by Merit Campus    2014-09-12 04:04:01


© meritcampus 2019

All Rights Reserved.

Open In App