Java final keyword

Last Updated :
Discuss
Comments

Question 1

What is the use of final keyword in Java?

  • A

    When a class is made final, a subclass of it can not be created.

  • B

    When a method is final, it can not be overridden.

  • C

    When a variable is final, it can be assigned value only once.

  • D

    All of the above

Question 2

Output of following Java program Java
class Main {
 public static void main(String args[]){
   final int i;
   i = 20;
   System.out.println(i);
 }
}
  • A
    20
  • B
    Compiler Error
  • C
    0
  • D
    Garbage value

Question 3

Output of the below Java Code?

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


  • A

    30

  • B

    Compiler Error

  • C

    Garbage value

  • D

    0

Question 4

Output of the below Java Code?

Java
class Base {
  public final void show() {
       System.out.println("Base::show() called");
    }
}
class Derived extends Base {
    public void show() {  
       System.out.println("Derived::show() called");
    }
}
public class Main {
    public static void main(String[] args) {
        Base b = new Derived();;
        b.show();
    }
}


  • A

    Derived::show() called

  • B

    Base::show() called

  • C

    Compiler Error

  • D

    Exception

There are 4 questions to complete.

Take a part in the ongoing discussion