Core Java - Interview Questions and Answers for 'Utility class' - 3 question(s) found - Order By Newest Q1. In which cases , moving methods to utility class could be useful ? Core Java
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 Related Questions Name few Java util classes that were introduced with Java 7 ? Name few Java Util classes introduced with Java 8 ? What are Util Classes ? What are its advantages ? Q2. What will the following code do ? String dateStr = "2011 11 19"; DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date date = dateFormat.parse(dateStr); System.out.println(date);
Ans. It will throw the ParseException as the date format doesn't abide with the format of the specified date. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  date   utility classes   dateformat   exception   parseexceptionRelated Questions What are the ways to avoid LazyInitializationException ? How compiler handles the exceptions in overriding ? What things should be kept in mind while creating your own exceptions in Java? Difference between Checked and Unchecked exceptions ? Explain java.lang.OutOfMemoryError ? When do you get ClassCastException?
or
What is ClassCastException ? If you are given a choice to implement the code to either Insert a Record or Update if already exist, Which approach will you follow ? Explain throw, throws , try and catch in Java ? What is ConcurrentModificationException ? What will happen if we don't have termination statement in recursion ? Q3. Can we have interface as a replacement for utility class ? Core Java
Ans. Yes, With Java 8 we can use Interfaces as collection of utility methods through the use of default methods. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java 8  interfaces  utility class   utility classesRelated Questions Differences between abstract class and interface ? What are the different uses of Interfaces in Java ? Why can't we have diamond problem with interfaces ? Why do we need Thread class even in case we execute thread using runnable interface ? What is the difference between List, Set and Map ?
or
What are the different Java Collections Interfaces ? What are Marker Interfaces ? Name few Java marker interfaces ? Can we declare interface methods as private ? What is the use of runnable interface if we can always create a new thread using Thread class ? Which of the following doesn't extend Collection interface ? If an Abstract class has only abstract methods, What's the difference between such a class and an interface ?