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

Search Java Interview Questions


 2137 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Constants' - 11 question(s) found - Order By Newest

 Q1. What are the conventions regarding constant variables or static final variables ?Core Java
admin
info@buggybread.com
Ans. They should be named all in upper case with underscore separating words.

Moreover the name should be such that it identifies the value. For example, if we are using constant for employee Id 123, it should be named something like EMPLOYEE_ID_123 and not EMPLOYEE_ID

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

   Like      Discuss      Correct / Improve     constants   variable names   java conventions


 Q2. How do you define a class of constants in Java?
admin
info@buggybread.com
Ans. Make the class as final.
Disable the object construction by making constructor private.
Keep only Static Final Variables declared and Defined at the same time.

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

   Like      Discuss      Correct / Improve     java constants   constant class   java


 Q3. Why is a constant defined as a static final in Java?Core Java
Admin
info@buggybread.com
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


 Q4. What is the advantage of using static final or constant variables in Java ? Core Java2016-12-20 10:52:26

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


 Q5. What will be result of following code and why

Integer int1 = 1;
Integer int2 = 1;
String str1 = new String("str");
String str2 = new String("str");
String str3 = "str";
String str4 = "str";

      
System.out.println(int1 == int2);
System.out.println(str1 == str2);
System.out.println(str3 == str4);
Core Java2017-05-08 10:15:00

Ans. true
false
true

Just like Strings, java maintains an integer constant pool too. So 1 will be maintained in integer constant pool and hence reference int2 will point to same integer in pool.

String pool is only for string literals ( defined by "" ) and not for newly created objects and hence str1 == str2 will return false as they are separate objects in memory.

String pool is used for storing string literals so that they can be reused and str3 and str4 will point to same string literal in string pool.


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

   Like      Discuss      Correct / Improve     string pool  integer constant pool  string comparison  integer comparison


 Q6. 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 Java2017-05-09 19:33:04

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


 Q7. 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 Java2017-05-09 19:36:14

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


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

Core Java2017-05-09 19:40:03

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


 Q9. 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 Java2017-05-18 08:23:16

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



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q10. How do we define constant variables in Java ?Core Java2017-06-23 08:11:29

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


 Q11. Do we have Integer constant pool of primitive int or Integer objects ? Core Java2017-08-24 21:44:34

Ans. It's of Integer objects and not primitive int.

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

   Like      Discuss      Correct / Improve     integer constant pool




Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

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:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: