Core Java - Interview Questions and Answers for 'Static' | 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

   



Interview Questions and Answers - Order By Newest

   
 Q41. Which of the following is not the difference between Singleton and Static class ( Class with static members only ) ?

a. Only one object can be created for Singleton class whereas No objects are created for static class.
b. Singleton class instance is initiated using new keyword whereas static class instance is created using static method.
c. Singleton class can be serialized whereas Static class cannot be.
d. Singleton Class can participate in runtime Polymorphism whereas Static class cannot.
Ans. Singleton class instance is initiated using new keyword whereas static class instance is created using static method.

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

   Like         Discuss         Correct / Improve     java   oops   singleton   design pattern   static class


 Q42. Which of the following do you think is the primary reason you would never use a static class even the application doesn't need multiple requests or threads ?

a. Serialization
b. Runtime Polymorphism
c. Lazy Loading
d. Memory
Ans. Runtime Polymorphism

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

   Like         Discuss         Correct / Improve     static class   static vs singleton   java   oops   objects  Runtime Polymorphism


 Q43. How is static and dynamic polymorphism achieved in Java ?Core Java
Ans. Static polymorphism is the polymorphic resolution identified at compile time and is achieved through function overloading whereas dynamic polymorphism is the polymorphic resolution identified at runtime and is achieved through method overriding.

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

   Like         Discuss         Correct / Improve     static polymorphism  object oriented programming (oops)  oops concepts   dynamic polymorphism  object oriented programming (oops)  oops concepts   polymorphism  object oriented programming (oops)  oops concepts   overloading   overriding   broadridg     Asked in 1 Companies      basic        frequent


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


 Q45. What is a static variable in Java ?Core Java
Ans. It is a class level variable that is shared among the objects of that class.

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

   Like         Discuss         Correct / Improve     static   static variables   class level variables     Asked in 1 Companies      basic        frequent

Try 1 Question(s) Test


 Q46. What could be the problems due to the usage of static methods and elements ? Core Java
Ans. static methods and static elements are shared by all objects of a class and hence could create problem.

Static methods are not synchronized by default and hence could create problem when accessed through multiple threads. Static elements are shareable by class and hence state of one object could be altered by another object.

Some limitations with Unit testing as not all mocking framework facilitate mocking them. Power mock allows but Mockito doesn't

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

   Like         Discuss         Correct / Improve     static  static method  static variables


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


 Q48. Why main method is declared static ?Core Java
Ans. static is the keyword that makes it accessible even without creating any object and using class name only. Making it non static would like creation of object upfront before calling the method.

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

   Like         Discuss         Correct / Improve     main method  static     Asked in 4 Companies      Basic


 Q49. Is it a good practice to override static methods ?Design
Ans. Though it's useful but it's not as useful as overriding member or object methods. We cannot achieve polymorphic behavior with static methods by overriding their definition in derived class.

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

   Like         Discuss         Correct / Improve     override static methods


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


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


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


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


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


 Q55. Can we mock static methods ?Testing
Ans. Yes we can use PowerMock. With Mockito , we cannot mock static methods.

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

   Like         Discuss         Correct / Improve     mocking fraeworks  mocking static methods  mockito


 Q56. Why can we have a static inner class but not a static Top level class in Java ?Core Java
 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     static class  inner classes   nested classes      Expert        rare


 Q57. What is the default data type of state variables in Clonable interface and Serializable interface ?Core Java
Ans. Cloneable and Serializable are Marker Interfaces, So these are empty interfaces with no variables.

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

   Like         Discuss         Correct / Improve     cloneable  serializable  static variables     Asked in 1 Companies


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


 Q59. Difference between static and dynamic binding ?Design
Ans. Static binding happens at the compile time whereas dynamic binding happens at runtime.

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

   Like         Discuss         Correct / Improve     static vs dynamic binding  overriding     Asked in 1 Companies      basic        frequent


 Q60. Can we call a static method using reference currently pointing to null.Core Java
Ans. yes, we can.
exa: public class Java{
public static void main(String... args) {
JAva j= null;
j.greeting(); // call with null reference
}
public static void greeting() {
System.out.println("Hello World");
}
} // output: Hello World

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

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


 Q61. How can you verify if there were interactions with a static method using PowerMock ?PowerMock
Ans. We can use PowerMockito.verifyStatic for this purpose

PowerMockito.verifyStatic(VerificationModeFactory.times(1));
<Class_Name>.<static_method_name>;

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

   Like         Discuss         Correct / Improve     mocking static methods


 Q62. Can a constructor be declared static ? Why ?Core Java
Ans. No.

When we declare a method static, it means that "this belongs to class as whole and not particular instance". The whole purpose of constructor is to initialize a object and hence there is no sense having static constructor.

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

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


 Q63. Can the value of a static variable be changed ? Core Java
Ans. Yes the value of a static variable can be changed. It's the final keyword that restrict the modification of value not static.


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

   Like         Discuss         Correct / Improve     static variables


 Q64. What will be the output of executing following class ?

public class BuggyBread {

static {
System.out.println("Static Block");
}

{
System.out.println("Initialization Block");
}

BuggyBread(){
System.out.println("Constructor");
}

public static void main(String[] args){
System.out.println("Main Method");
}
}
Core Java
a. Static Block
Main Method
b. Static Block
Instance Initialization Block
Main Method
c. Static Block
Constructor
Main Method
d. Static Block
Instance Initialization Block
Constructor
Main Method

Ans.a. Static Block
Main Method

 Q65. What will be the output upon executing following class ?

public class BuggyBread {

static {
System.out.println("Static Block");
}

{
System.out.println("Instance Initialization Block");
}

BuggyBread(){
System.out.println("Constructor");
}

public static void main(String[] args){
System.out.println("Main Method");
new BuggyBread();
}
}
Core Java
a. Instance Initialization Block
Constructor
Static Block
Main Method
b. Static Block
Instance Initialization Block
Constructor
Main Method
c. Main Method
Static Block
Instance Initialization Block
Constructor
d. Static Block
Main Method
Instance Initialization Block
Constructor

Ans.d. Static Block
Main Method
Instance Initialization Block
Constructor

 Q66. Which of the following is not true for static keyword ?Core Java
a. Static members are shared by all objects of the class.
b. We can override static methods.
c. Static methods operate on static variables only.
d. Static Elements are accessed using class name.

Ans.b. We can override static methods.

 Q67. Static Polymorphic in Java is achieved through .. Core Java
a. Method Overloading
b. Method Overriding
c. Variable Overloading
d. Variable Overriding

Ans.a. Method Overloading

previous 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: