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 Rating

next 30
 Q1. If we have a return statement inside the finally block, Then what will happen ?Core Java
Ans. Returning from inside a finally block will cause exceptions to be lost. A return statement inside a finally block will cause any exception that might be thrown in the try block to be discarded.

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

   Like         Discuss         Correct / Improve     exception handling  finally block     Asked in 1 Companies


 Q2. Can constructors be final ?Core Java
Ans. No. They are never inherited and therefore are not subject to hiding or overriding.

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

   Like         Discuss         Correct / Improve     constructor  final  final constructor     Asked in 1 Companies


 Q3. Are final methods faster than regular instance methods ?Core Java
Ans. Yes. As they cannot be overridden , there is no use of virtual table concept which is used for dynamic binding resolution.

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

   Like         Discuss         Correct / Improve     final methods   final keyword      expert


 Q4. 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


 Q5. 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


 Q6. Why Java has final class ?Core Java
 This question was recently asked at 'Avis'.This question is still unanswered. Can you please provide an answer.


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

   Like         Discuss         Correct / Improve     final class   final keyword     Asked in 1 Companies      basic


 Q7. What will happen if there is an exception in Java finally block ?Core Java
Ans. The regular behavior of exception handling will occur. It will look for any immediate catch handler and if none is provided, it would be transmitted to the callers until a catch handler is found or it's out of main function.

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

   Like         Discuss         Correct / Improve     exceptions  finally


 Q8. What will the following code print and Why

class Class1 {
static{
System.out.println("Wow");
}
public static final int xyz=1;
}

class Class2{
public static void main(String[] args) {
System.out.println(String.valueOf(Class1.xyz));
}
}
Core Java
Ans. 1

Reason being that Class1.xyz is replaced with 1 during pre compilation only and a reference to Class1 is never made.

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

   Like         Discuss         Correct / Improve     static final   pre compilation


 Q9. How different is it when final applied to variables and object references ? Core Java
Ans. final when assigned to object references doesn't make them immutable. It means that the references cannot be de-referenced.

final when applied to variables means that the value cannot be changed.

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

   Like         Discuss         Correct / Improve     final keyword  final variable


 Q10. 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


 Q11. 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


 Q12. What is the difference between being final and being immutable ? Are string final or immutable in Java ?Core Java
Ans. Being final in the sense of objects or object reference means that the reference cannot be reassigned to a different object whereas being immutable means that the object contents cannot be changed.

Strings in Java are immutable.

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

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


 Q13. Will the following code compile ?

final HashMap hm = new HashMap();
hm.put("Ravi", 221);
Core Java
Ans. Yes, it will compile without error. final in this context means that the reference hm cannot be assigned to a new hash map but didn't restrict from changing the state of collection already held by hm.

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

   Like         Discuss         Correct / Improve     final  final variable     Asked in 1 Companies


 Q14. How do we define constant variables in Java ?Core Java
Ans. By using static and final modifiers.

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

   Like         Discuss         Correct / Improve     constant variables   static final      basic        frequent


 Q15. What is the use of final keyword ?Core Java
Ans. final keyword is a modifier that means differently when applied to variables, methods and classes.

1. Final variable cannot be changed once initialized
2. Final method cannot be overridden
3. Final class cannot be sub classed

  Sample Code for final variable

  Sample Code for final method

  Sample Code for final class

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

   Like         Discuss         Correct / Improve     final  final keyword     Asked in 1 Companies      Basic        frequent


 Q16. Which of following is / are valid static final declaration ?

public static final String MAX_NUM = "10";
public static final Object NULL = null;
public static final MathContext MATH_CONTEXT = new MathContext(2,RoundingMode.CEILING);
Core Java
Ans. All are valid declarations.

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

   Like         Discuss         Correct / Improve     static final  constants      basic


 Q17. What is a blank final variable ? Can we initialize a blank final variable ?Core Java
Ans. A final variable that is not initialized while declaration is a blank final variable.

Yes, we can initialize it within a constructor.

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

   Like         Discuss         Correct / Improve     blank final variable  final variable


 Q18. Would you consider creating a constant name for "null" ?

Core Java
Ans. Objective of constant variable is to provide a context to the value and provide a single point of change. In terms of context, i don't see much need for having constant name for null as null is already self explanatory. In terms of providing single point of change, it doesn't make much sense as it would necessitate that all such placeholders either hold null or a common value.



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

   Like         Discuss         Correct / Improve     constant variables  static final variable  java conventions  java naming conventions


 Q19. If we have to maintain a value "31" denoting "number of days in a month" as constant variable, Which of the following is the best constant name and Why ?

THIRTY_ONE
NUMBER_OF_DAYS_IN_MONTH
NUMBER_OF_DAYS_IN_MONTH_31
Core Java
Ans. NUMBER_OF_DAYS_IN_MONTH_31

Constant should be such that it uniquely identifies the value it holds and context of the value. THIRTY_ONE uniquely identifies the value but doesn't express the context. NUMBER_OF_DAYS_IN_MONTH expresses the context but doesn't specify the value clearly as different month can have different number of days.


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

   Like         Discuss         Correct / Improve     constant variables  static final variable  java conventions  java naming conventions


 Q20. If we have to maintain a value "7" denoting "number of days in a week" as constant variable, Which of the following is the best constant name and Why ?

SEVEN
NUMBER_OF_DAYS_IN_WEEK
NUMBER_OF_DAYS_IN_WEEK_7
Core Java
Ans. NUMBER_OF_DAYS_IN_WEEK

Constant should be such that it uniquely identifies the value it holds and context of the value. SEVEN uniquely identifies the value but doesn't express the context.

Though NUMBER_OF_DAYS_IN_WEEK and NUMBER_OF_DAYS_IN_WEEK_7 makes sense in terms of unique value identification and context but 7 at the end of constant name appears useless as all weeks have 7 days and hence it's the only value it can hold. If it would have been "number of days in a month", it would have made sense to include number of days at the end like

NUMBER_OF_DAYS_IN_MONTH_30
NUMBER_OF_DAYS_IN_MONTH_31
NUMBER_OF_DAYS_IN_MONTH_27
NUMBER_OF_DAYS_IN_MONTH_28

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

   Like         Discuss         Correct / Improve     constant variables  static final variable  java conventions  java naming conventions


 Q21. Will finally run if we have return statement and it exits the method early ?Core Java
Ans. finally will execute in all graceful situations - graceful executions as well as graceful exceptions. The only situation when finally block won't execute is when the app is abruptly stopped, killed or unplugged.

  Sample Code for finally

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

   Like         Discuss         Correct / Improve     finally      Intermediate


 Q22. 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


 Q23. What is the advantage of using static final or constant variables in Java ? Core Java
Ans. Better Control - If the value is being used at multiple locations , that can be controlled better from single place. Any change would only require making single change.

Meaning , Aliasing and Better Readability - Sometimes its easy to read the value by its meaning or alias ( 0 as ZERO or 0 as NEUTRAL_VALUE ).


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

   Like         Discuss         Correct / Improve     static final  final variable  variable constants


Frequently asked.
 Q24. 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


 Q25. Can you assign value to a static final type at runtime ?Core Java
Ans. No

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

   Like         Discuss         Correct / Improve     static  final  static final     Asked in 1 Companies


Very frequently asked. Favorite question in Walk in Drive of many Indian service companies.
  Q26. 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


Very frequently asked. Favorite question in Walk in Drive of many Indian service companies.
  Q27. 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


 Q28. 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.
 Q29. 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


 Q30. 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


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: