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. |
|
| ||||
Core Java - Interview Questions and Answers for 'Static' - 63 question(s) found - Order By Rating | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
Ans. By using synchronized static method or synchronized block | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  threads  synchronization  static methods Asked in 2 Companies intermediate | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
Ans. It's weird that compiler doesn't complain if we declare transient with static variable because it makes no sense. At least a warning message saying "transient is useless in this situation" would have helped with code cleaning. Static variables are never serialized and transient is an indication that the specified variable shouldn't be serialized so its kind of double enforcement not to serialize. It could be that as it makes no different to the variable behavior and hence using both keywords with a variable are permitted. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  static  transient  serialization Asked in 1 Companies expert   rare | ||||
Try 1 Question(s) Test | ||||
| ||||
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 | ||||
| ||||
Ans. static keyword is used to specify that the respective programming construct ( method , variable ) belongs to the class and not to its instance and is supposed to be shared by all instances of the class. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  static keyword Asked in 1 Companies basic   frequent | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
Ans. 1. Static class is a class which cannot be instantiated and all its members are static whereas Singleton is the class that only permit creation of single object and then the object is reused. 2. As there is no object in Static class, it cannot participate in runtime Polymorphism. 3. As Static class doesnt allow creating objects and hence it cannot be serialized. 4. Static class body is initialized eagerly at application load time whereas Singleton object can be initiated eagerly using static blocks or lazily on first need. 5. Its not recommended to use pure static class as it fails to use many OOPs concepts. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  Static Class  Singleton  Static Class vs Singleton Asked in 3 Companies Intermediate   frequent | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
Ans. In first case we are trying to initialize Inner class object using the instance of Outer Class whereas in second case we are trying to initialize the Inner class object directly using the Outer class name. In second case , Inner class is "static inner class" as we cannot access "non static inner class" using Classname alone. In first case, the inner class could be either "static inner class" or "non static inner class". | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  inner classes  inner class   static inner class   new operator | ||||
| ||||
Ans. Generics , Enums , Autoboxing , Annotations and Static Import. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   java5   generics   enum   autoboxing   annotations   static import   rare | ||||
Try 1 Question(s) Test | ||||
| ||||
Ans. Though Static methods cannot access the instance variables directly, They can access them using instance handler. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  static   static method   java   oop   variables Asked in 1 Companies basic   frequent | ||||
| ||||
Ans. No. Static methods belong to the class and not the objects. They belong to the class and hence doesn't fit properly for the polymorphic behavior. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   static   static method   overriding intermediate | ||||
Try 3 Question(s) Test | ||||
| ||||
Ans. They are loaded at runtime when the respective Class is loaded. | ||||
Sample Code for static variable | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   static   static variable   memory intermediate | ||||
| ||||
Ans. No. Only Object and its members are serialized. Static variables are shared variables and doesn't correspond to a specific object. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  serialization   java   oops   static   static variables Asked in 1 Companies intermediate   rare | ||||
Try 2 Question(s) Test | ||||