Menu
Topics Index
...
`

Final keyword914


If you need explanation Read this topic

If you need Answer Take test on this topic

Final keyword914
What will be the output of the following program?
public class Final
{
    public static void main(String[] args)
    {
        final int result;
        result = 20;
    
        int assign = process(result);
        result = assign;

        System.out.println(result);
    }
    
    static int process(int a)
    {
        return a + 5;
    }
}

A. 20
B. 25
C. Compilation Error
D. 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.

ANS IS C (COMPILATION ERROR)

--HERE IS ONE ERROR IN PROGRAM stating that:
variable r might already have been assigned.

--Here,concept of final is used in which 20 value is
assignd.

--In java keyword final is used to declare the constants.
once constants are assigned value,after that in entire
execution of program we can not assign another value to it.
Else,Compilation Error will come in program.

Posted by Mânïshå Mùlchåndânï    2015-02-05 14:08:53


ans c. compilation error...result = assign; this line throws compilation error.bcz variable result is declared with final keyword. so we can initialize result variable only once.in this program result variable 1st declared with final and assigned with 20.thn variable assign is assigned with function call process (),here we passed result value as parameter. function returns 20+5=25.thn 25 is stored in assign variable. aft we tried to reassign final variable result with assign value 25. so compiler throws error.reassigning another value is an illegal bcz final variable can be initialized ly once.we cant change this value through out the program, if we try thn compiler throws error

Posted by Maheshwari Natarajan    2015-02-05 15:33:43


c

Posted by Shubham Bansal    2015-02-05 19:07:55


This dose is now closed and the winners are Mânïshå Mùlchåndânï,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Mânïshå Mùlchåndânï 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-06 03:24:47


© meritcampus 2019

All Rights Reserved.

Open In App