Menu
Topics Index
...
`

Final keyword933


If you need explanation Read this topic

If you need Answer Take test on this topic

Final keyword933
What will be the output of the following program?
public class Final
{
    final int assign = 35;

    Final()
    {
        final int assign = 10;
        print(assign);
    }

    public static void main(String[] args)
    {
        final int result = 20;
        final int assign;
        
        Final f = new Final();
        assign = 30;

        f.print(result);
        System.out.println(process(result) + ", " + assign + ", " + f.assign);
    }

    static int process(int a)
    {
        return a + 5;
    }

    void print(int assign)
    {
        System.out.print(assign + ", ");
    }
}

A. 10, 20, 25, 30, 10
B. 10, 20, 25, 30, 35
C. 30, 20, 25, 30, 35
D. Compilation Error because variable assign is declared multiple times
E. Compilation Error for some other reason
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.

Ans is B..here we have the final keyword which is used to make the variable value fixed through out the program...
* if we make variable than we cant change value of that variable through out the program
* if we make a method as final than we can t override that method in the Child classes
* if we make a class as final than no other class cant extend that class(String,Math)

Execution:
here we are creating the instance for the Final class than memory will allocate for the instance variable assign than initialized to 35  and in constructor we are creating assign local variable with 10 calling print so it will print 10..in the main we have the 2 local variables res and assign assign is assgned with the 30 and result is assigned with the 20 next we are calling the print() by passing   result so there it will print 20..next we are passing result to process this will return by adding 5 so it will print 25  next we are printing the assign which is 30 next we are passing f.assign which is 35

Posted by Uday Kumar    2015-01-08 09:28:22


ans b

Posted by Shubham Bansal    2015-01-08 10:40:00


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Asad Ahmed. 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-01-09 04:07:00


© meritcampus 2019

All Rights Reserved.

Open In App