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. |
|
| ||||
Interview Questions and Answers - Order By Newest | ||||
| ||||
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 | ||||
| ||||
Ans. We can do so through a static public member method or static block. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   static   constructor   static method   static block  private Asked in 1 Companies | ||||
| ||||
Ans. Both belong to the class as a whole and not to the individual objects. Static methods are explicitly called for execution whereas Static block gets executed when the Class gets loaded by the JVM. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   static   static method   static block   difference between   similarity basic   frequent | ||||
Try 1 Question(s) Test | ||||
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 | ||||
| ||||
Ans. It could be worthy to move a method to util class if the method needs to be shared, doesn't require polymorphic behavior and need not be overridden in special cases. Don't belong to one group through is-a relationship ( You can share through parent class method ) Don't implement a specific interface ( java 8 default methods ) Doesn't involve complex computing as you will be loosing the benefit of object state with just static method. Doesn't require polymorphic behavior as static methods don't participate in runtime polymorphism. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  utility classes   util classes   static methods   application design   rare | ||||
| ||||
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. A static method is not associated with any instance of a class so the concept of overriding for runtime polymorphism using static methods is not applicable. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   static   static methods   java keywords   yes-no intermediate   frequent | ||||
| ||||
Ans. By static import , we can access the static members of a class directly without prefixing it with the class name. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   java5   static import   static   java keyword   static import Asked in 4 Companies basic   frequent | ||||
| ||||
Ans. Inner class is a type of nested class. Inner classes are non static nested classes i.e the one that are associated with the instance of outer class. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   static   nested classes   inner classes   nested vs inner class basic   frequent | ||||
Try 1 Question(s) Test | ||||
| ||||
Ans. static loading - Classes are statically loaded with Java new operator. dynamic class loading - Dynamic loading is a technique for programmatically invoking the functions of a class loader at run time. Class.forName (Test className); | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   jvm   memory management   class loaders  classloaders   static class loading   static loading   dynamic class loading   dynamic loading Asked in 2 Companies expert   frequent | ||||
| ||||
Ans. Yes, it is possible by using a static block in the Java program. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   main   static block   compile   yes-no | ||||
| ||||
Ans. Yes. static method is not accessed by the instance of class. Either you call it by the class name or the reference. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   static   static method   code | ||||
| ||||
Ans. import static java.lang.System.*; | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   static import   generics   import   OCJP   SCJP   Oracle certified java developer Asked in 1 Companies | ||||
| ||||
Ans. Yes, Effective Java 8. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   java8   static interface methods   yes-no Asked in 1 Companies | ||||
| ||||
Ans. Static methods can be called using instance references wherein this would have made sense but static method can also be called using Class name wherein this would mean nothing and hence forbidden. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   static methods   this keyword | ||||
Try 2 Question(s) Test | ||||
Ans. Constructors , Classes ( Outer ) , Interfaces , Local variables , Inner Class methods and instance variables. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  oops   java   static   nested classes   static nested classes | ||||
Try 2 Question(s) Test | ||||
| ||||
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. 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 | ||||
| ||||
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. A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the static keyword. Here is an example: static { // whatever code is needed for initialization goes here } A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   static   static block Asked in 16 Companies basic   frequent | ||||
| ||||
Ans. The accessibility (public, protected, etc.) of the static nested class is defined by the outer class. A static nested class is not an inner class, it's a top-level nested class. The name of the static nested class is expressed with OuterClassName.NestedClassName syntax. When you define an inner nested class (or interface) inside an interface, the nested class is declared implicitly public and static. Static nested classes can be declared abstract or final. Static nested classes can extend another class or it can be used as a base class. Static nested classes can have static members. Static nested classes can access the members of the outer class (only static members, obviously). The outer class can also access the members (even private members) of the nested class through an object of nested class. If you don't declare an instance of the nested class, the outer class cannot access nested class elements directly. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   nested classes   static nested classes   inner classes | ||||
| ||||
Ans. No. Even though "this" would mean a reference to current object id the method gets called using object reference but "this" would mean an ambiguity if the same static method gets called using Class name. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   static   this keyword   this   yes no   why questions intermediate | ||||
| ||||
Ans. Program throws "NoSuchMethodError" error at runtime . | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   main method   main   static   static method   nosuchmethoderror | ||||
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 | ||||
| ||||
Ans. No, only using Interface Name. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   java8   static interface methods | ||||
| ||||
Ans. Yes , we can do that as static methods are not accessible using references and hence cannot lead to conflict. We cannot do inverse as Default methods cannot be overridden with the static methods in derived interface. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   java8   default methods   static interface methods | ||||
| ||||
Ans. Still No in case we are making use of inheritance. we may have problem wherein we have program flow moving across common inherited method and specific methods of the derived class. call made to another static method in the parent class will only access the static class of the Parent class irrespective of the call from any of the derived class. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  static class   static vs singleton   inheritance | ||||
| ||||
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 | ||||
| ||||
Ans. Static block is like static method that gets executed upon class loading. The way static method allows accessing member variables ( using object references only ), the same static block can also access and initialize member variables. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   static block | ||||
| ||||
Ans. Static methods are resolved during compile time and hence they cannot participate in runtime polymorphism. Instance methods are resolved during runtime. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   static methods   methods   runtime polymorphism intermediate | ||||