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.
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 :
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 :
LikeDiscussCorrect / Improve  static class   static vs singleton   java   oops   objects  Runtime Polymorphism
Q43. How is static and dynamic polymorphism achieved in 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 :
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 :
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 :
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 :
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 :
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 ?
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
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 ?
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 :
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 :
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);
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 :
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 :