Java | final keyword | Question 2

Output of follwoing Java program

filter_none

edit
close

play_arrow

link
brightness_4
code

class Main {
 public static void main(String args[]){
   final int i;
   i = 20;
   System.out.println(i);
 }
}

chevron_right


(A) 20
(B) Compiler Error
(C) 0
(D) Garbage value


Answer: (A)

Explanation: There is no error in the program. final variables can be assigned value only once. In the above program, i is assigned a value as 20, so 20 is printed.

Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.