Core Java - Interview Questions and Answers for 'Final' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Core Java - Interview Questions and Answers for 'Final' - 51 question(s) found - Order By Newest

next 30
Very frequently asked. Favorite question in Walk in Drive of many Indian service companies. Frequently asked in HCL Technologies, TCS and Accenture.
  Q1. What is the difference between final, finally and finalize() ?Core Java
Ans. final - constant variable, objects cannot be de-referenced, restricting method overriding, restricting class sub classing.

finally - handles exception. The finally block is optional and provides a mechanism to clean up regardless of what happens within the try block. Use the finally block to close files or to release other system resources like database connections, statements etc.

finalize() - method helps in garbage collection. A method that is invoked before an object is discarded by the garbage collector, allowing it to clean up its state.

  Sample Code for final

  Sample Code for finally

  Sample Code for finalize

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   final   finally   finalize   final vs finally vs finalize     Asked in 61 Companies      basic        frequent

Try 4 Question(s) Test


Not frequently asked as it was introduced with Java 8.
 Q2. Difference between final and effectively final ? Why is effectively final even required ?Core Java
Ans. Final variable means a variable that has been declared final and hence cannot be de referenced after initialization. Effective final means a variable that has not been declared final but haven't been reassigned the value after initialization.

First is the regulation that restricts the reassignment and will raise a compilation error if we try to do so. Second is the outcome without the restriction.

Effective Final is the eventual treatment of the variable that is required for many features. For eq - Java 8 requires that local variables referenced from a lambda expression must be final or effectively final.It means all local referenced from lambda expressions must be such that their value shouldn't be changed after initialization whether declared final or not.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   java8   java 8   final   effective final   final vs effective final   lambda expressions      expert

Try 2 Question(s) Test


Very frequently asked. Favorite question in Walk in Drive of many Indian service companies.
  Q3. What is a final method ?Core Java
Ans. Its a method which cannot be overridden. Compiler throws an error if we try to override a method which has been declared final in the parent class.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   final   final method     Asked in 30 Companies      basic        frequent

Try 1 Question(s) Test


Very frequently asked. Favorite question in Walk in Drive of many Indian service companies.
  Q4. What is a Final Variable ?Core Java
Ans. Final variable is a variable constant that cannot be changed after initialization.

  Sample Code for final variable

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     oops   java   final   final variable   basic interview question     Asked in 12 Companies      basic        frequent

Try 1 Question(s) Test


 Q5. Should we override finalize method ?
Ans. Finalize is used by Java for Garbage collection. It should not be done as we should leave the Garbage Collection to Java itself.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   garbage collection   java memory management   finalize

Try 2 Question(s) Test


 Q6. Will the static block be executed in the following code ? Why ?

class Test
{
static
{
System.out.println("Why I am not executing ");
}
public static final int param=20;
}

public class Demo
{
public static void main(String[] args)
{
System.out.println(Test.param);
}
}
Ans. No the static block won't get executed as the referenced variable in the Test class is final. Compiler replaces the content of the final variable within Demo.main method and hence actually no reference to Test class is made.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   static   static block   final variable

Try 1 Question(s) Test


 Q7. Can we override compareTo method for Enumerations ?Core Java
Ans. No. compareTo method is declared final for the Enumerations and hence cannot be overriden. This has been intentionally done so that one cannot temper with the sorting order on the Enumeration which is the order in which Enum constants are declared.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   compareto   final methods   enum   enumeration      expert


 Q8. When we say final x = 10, is the reference final or the value ?Core Java
Ans. final keyword have meaning only to referenced and not the value. It means that the specified reference cannot be dereferenced. It doesn't control the value assigned to the memory that's being referenced. This is the reason that final object references doesn't mean that the object is immutable but means that the reference cannot be changed to point to new object.

In case of primitive types too, when we assign a reference to another, values are passed and not the object reference, and hence a new placeholder is created in memory with the same value. That is why final to that context means that you cannot change the assigned memory and there is no way we can have that memory place have another value.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     final keyword  final variables  references


 Q9. What is a Final Method ?Core Java
Ans. A Method that cannot be overriden in the sub class.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   java keywords   final   final method   overriding   basic interview question      basic        frequent

Try 1 Question(s) Test


Very Frequently asked.Favorite question in Walk in drive for many Indian service companies.
 Q10. What is a Final Class ?
Ans. A Class that cannot be sub classed.

  Sample Code for final class

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   final   final class   java keyword   basic interview question     Asked in 3 Companies      basic        frequent


 Q11. Does Declaring an object "final" makes it immutable ?Core Java
Ans. Only declaring variables as final makes them immutable. Making objects final means that the object handler cannot be used to target some other object but the object is still mutable.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   final   immutable  immutability   immutable  immutability object


 Q12. How can we make sure that a code segment gets executed even in case of uncatched exceptions ?
Ans. By putting it within finally.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   exceptions   finally   uncatched exceptions      basic        frequent


 Q13. Is it necessary that each try block to be followed by catch block ? Core Java
Ans. It should be followed by either catch or finally block.

  Sample Code for Retry in case of exception

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   exceptions   exception handling   try   catch   finally  Oracle OCA Test     Asked in 3 Companies      basic        frequent

Try 1 Question(s) Test


 Q14. Can we have try and catch blocks within finally ?Core Java
Ans. Yes, if we have a cleanup code that might throw an exception in the finally block, then we can have a try-catch block

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   exceptions   exception handling   finally   try   catch   yesno


Frequently asked.
 Q15. When does the finally block gets executed ?Core Java
Ans. A finally block of code always executes, whether or not an exception has occurred.The only time finally won't be called is if you call System.exit() or if the JVM crashes first.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     finally  exception handling  exceptions     Asked in 4 Companies      basic        frequent


 Q16. When is the situation when finally section won't execute ?Core Java
Ans. If the process / app is abruptly killed or terminated.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     exception handling  finally     Asked in 2 Companies      intermediate

Try 1 Question(s) Test


 Q17. Is String final in Java ?Core Java
Ans. Strings are immutable in Java and not final.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     is string final  final  string  immutable  immutability  immutability


 Q18. Does use of Final class enforces composition over Inheritance in Java ? Core Java
Ans. Yes, to a certain extent. But the objective for Final class could be beyond just enforcing composition as certain classes might have been created without inheritance or composition in mind.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     final class   composition  object oriented programming (oops)  oops concepts  inheritance  object oriented programming (oops)  oops concepts  composition  object oriented programming (oops)  oops concepts vs inheritance  object oriented programming (oops)  oops concepts      expert


 Q19. What could be the reasons for restricting inheritance using final class ?Core Java
Ans. 1. Enforcing composition over inheritance
2. Restricting overriding of certain methods
3. Final methods are faster than regular instance methods
4. Enforcing Immutability

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     final class   reasons for final class   restricting inheritance  object oriented programming (oops)  oops concepts      expert


 Q20. What is a finalize method ?Core Java
Ans. finalize() method is called just before an object is destroyed.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   finalize   garbage collection   basic interview question     Asked in 1 Companies      basic        frequent

Try 1 Question(s) Test


 Q21. Can finally block be used without catch ?Core Java
Ans. Yes but should follow "try" block then.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   exceptions   exception handling   try   catch   finally   yes-no

Try 1 Question(s) Test


 Q22. How finally used under Exception Handling?Core Java
Ans. The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   exceptions   exception handling   try   finally   basic interview question     Asked in 1 Companies

Try 1 Question(s) Test


 Q23. Will finally be called always if all code has been kept in try block ?Core Java
Ans. The only time finally won't be called is if you call System.exit() or if the JVM crashes first.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   exceptions   exception handling   try   catch   finally   system   system.exit


 Q24. Will static block for Test Class execute in the following code ?

class Test
{
static
{
System.out.println("Executing Static Block.");
}
public final int param=20;

public int getParam(){
return param;
}
}

public class Demo
{
public static void main(String[] args)
{
System.out.println(new Test().param);
}
}
Ans. Yes.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   static   static block   final variable   yes no

Try 1 Question(s) Test


 Q25. Can finally block throw an exception ?Core Java
Ans. Yes. Methods invoked from within a finally block can throw an exception. Failure to catch and handle such exceptions results in the abrupt termination of the entire try block.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   exceptions   exception handling   finally   yes-no


 Q26. Why is a constant defined as a static final in Java?Core Java
Ans. Final makes sure that the value doesn't change after initialization and static makes sure that there is only one copy that can be shared across objects.

Making it non static will unnecessarily create a different copy per object wherein the same value will kept for all copies ( as its final and cannot be changed ).

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   static   final variables   variable constants      basic        frequent


 Q27. Which of the following is not possible ?

a. try block followed by catch
b. try block followed by finally
c. try block followed by catch block and then finally
d. try block without catch or finally block
Ans. try block without catch or finally block

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     exceptions   java   try-catch   finally

Try 1 Question(s) Test


 Q28. Which of the following is not true for final variables ?

a. They cannot be changed after initialization
b. They can be initialized within static method
c. They can be declared and initialized together at the same place
d. They can be initialized within constructor
Ans. They can be initialized within static method

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   final   final variable   java keywords


 Q29. Which of the following is false for final ?

a. Final methods cannot be overriden
b. Final methods cannot be overloaded
c. Final classes cannot be subclassed
d. Final class cannot be abstract
Ans. Final methods cannot be overloaded

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   final   java keyword


Rarely asked as it was introduced with Java 8.
 Q30. Which keyword specify that a variable is effectively final ? a. final b. No Keyword c. Both of the above d. None of the aboveCore Java
Ans. No Keyword

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   java 8   java8   final   effectively final        rare


next 30

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: