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

   



Core Java - Interview Questions and Answers for 'Method' - 117 question(s) found - Order By Rating

next 30
 Q1. What is the difference between method and function in Typescript ?TypeScript
Ans. Method belongs to a class whereas function isn't. So the only difference is the scope in which they are defined.

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

   Like         Discuss         Correct / Improve     method vs function in typescript


 Q2. What is the use of defining equals , compareTo and hashcode methods in a class ? Where are they used ?Core Java
Ans. equals, compareTo and hashcode are of use when the objects are used within collections.

Equals helps with collections that helps maintaining only unique objects ( like Set )

compare and compareTo helps with collections that helps maintaining objects in order ( TreeSet, TreeMap etc )

hascode helps with collections that facilitates hash searching ( like hashSet, hashMap etc )

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

   Like         Discuss         Correct / Improve     equals    compareTo   hashcode method


 Q3. why is "".equals(str); safer than str.equals("")?Core Java
Ans. str.equals("") this statement will throw NullPointerException if str is null where as "".equals(str) works fine even if str is null

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

   Like         Discuss         Correct / Improve     equals method     Asked in 1 Companies


 Q4. What are inline functions ?Programming Language
Ans. Inline functions , just like C++ Macros is an optimized technique used by compiler to reduce the execution time. If the function is working on pre identified values ( which aren't resolved at runtime ), the function can execute the method and evaluate the outcome at compile time only instead of making a function call at runtime.

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

   Like         Discuss         Correct / Improve     inline functions  inline methods  C++     Asked in 1 Companies


 Q5. What are inline functions ? Do we have inline functions in Java ?Core Java
Ans. Inline functions , just like C++ Macros is an optimized technique used by compiler to reduce the execution time. If the function is working on pre identified values ( which aren't resolved at runtime ), the function can execute the method and evaluate the outcome at compile time only instead of making a function call at runtime.

In Java, the optimizations are usually done at the runtime or JVM level. At runtime, the JVM perform some analysis to determine which methods to inline. Java compiler would never inline any method and there is no way in java for the developer to explicitly define inlining of methods as it's take intrinsically care of during runtime only.

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

   Like         Discuss         Correct / Improve     inline functions  inline methods


 Q6. Can a method return more than one value at a time in Java ?Core Java
Ans. object / native type returned can only be one but an object can comprise of a array , collection or a group of different value types.

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

   Like         Discuss         Correct / Improve     java methods


 Q7. in the following class:
class A {
void methoda(Object o) {
Sysout("Object");
}

void methoda(String s) {
Sysout("String");
}

public static void main(String []args) {
A a = new A();
a.methoda(null);
}
}

what will be printed?
Core Java
Ans. String

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

   Like         Discuss         Correct / Improve     Code  Coding  method overloading     Asked in 1 Companies


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


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


 Q10. Which Java version supports Interface Default methods ?Core Java
Ans. Java 1.8 or Java 8

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

   Like         Discuss         Correct / Improve     default methods   interface default methods   java 8  java 8 features


 Q11. What is method hiding 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     method hiding


 Q12. Have you ever used java.util.Objects class ?Core Java
Ans. Yes, We are using requireNonNull method for validating if the object is null.

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

   Like         Discuss         Correct / Improve     objects  util methods        rare


 Q13. How many VTables are there for each class ? Core Java
Ans. There is one VTable for each class.

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

   Like         Discuss         Correct / Improve     virtual table method  vtable  runtime polymorphism  object oriented programming (oops)  oops concepts   method overriding      intermediate        rare


 Q14. How is the virtual method table implemented 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     virtual method table   method overriding  runtime polymorphism  object oriented programming (oops)  oops concepts      expert        rare


 Q15. What is virtual table with respect to method overriding 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     vtable  virtual method table   method overriding  runtime polymorphism  object oriented programming (oops)  oops concepts


 Q16. Are final methods faster than regular instance methods ?Core Java
Ans. Yes. As they cannot be overridden , there is no use of virtual table concept which is used for dynamic binding resolution.

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

   Like         Discuss         Correct / Improve     final methods   final keyword      expert


 Q17. What is the default execution method in Java?Core Java
Ans. public static void main(String[] args)

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

   Like         Discuss         Correct / Improve     main method


 Q18. Can we have a private default method ?Core Java
Ans. No. The whole idea of default method is to provide a default definition in case the implementing class intend to provide definition for only some of the methods. Making any of the interface method private would restrict it from being implemented by the implementing class. So default method is an option to provide implementation and not restricting a new definition.

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

   Like         Discuss         Correct / Improve     java 9  default methods


 Q19. What could be the reason for allowing private methods in java 9 ?Core Java
Ans. Java 8 allowed for method implementation using default methods in interfaces. As those default methods could contain complex logic and might need organizing the logic into multiple methods, they have allowed for private methods.

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

   Like         Discuss         Correct / Improve     java 9  default methods


 Q20. In the Following code which foo will get called.

foo(Integer i){
}

foo(String s){
}

public static void main(){
passing foo(null);
}
Core Java
Ans. ambiguity error

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

   Like         Discuss         Correct / Improve     code  method  function  data types     Asked in 1 Companies


 Q21. How can we ensure thread safety in static method ?Core Java
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


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


 Q23. What are the advantage of Abstract classes over interfaces with respect to Java 7 ? and What changed in Java 8 to help facilitate that in Java 8 ?Core Java
Ans. Abstract Classes provide default implementations of methods that are inherited by the classes that extend them, which was not the case for Interfaces. This changed in Java 8, where default implementations are provided for methods.

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

   Like         Discuss         Correct / Improve     abstract classes  interfaces  default method     Asked in 1 Companies      expert


 Q24. Is Runtime Polymorphism possible without Inheritance ?Core Java
Ans. Yes, Runtime Polymprohism requires either Class inheritance or Interface implementation.

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

   Like         Discuss         Correct / Improve     runtime polymorphism  object oriented programming (oops)  oops concepts  method overriding  inheritance  object oriented programming (oops)  oops concepts


 Q25. Why can't we have diamond problem with interfaces ?Core Java
Ans. Interfaces don't have member elements and method definitions that could cause diamond problem. With Java 8, Interfaces have default method definitions. This could have created diamond problem but Java introduced a compile time check for "duplicate default methods" in case same method is derived from multiple interfaces and no definition is overridden by the class.

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

   Like         Discuss         Correct / Improve     diamond problem  interfaces  java 8  default methods


 Q26. Can we synchronize the run() method 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     synchronization  multithreading  run method


 Q27. Why do we pass an array of strings to main method ?Core Java
Ans. Array of strings in the main method are the list of arguments or parameters which are sent to the application / program.

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

   Like         Discuss         Correct / Improve     main method   main method string array argument


 Q28. Can we compile and execute a Java class without main method ?Core Java
Ans. No without main method can not be executed it will throw an error illegal start of type. Main method is the entry point of application.

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

   Like         Discuss         Correct / Improve     jvm  compilation  main method      Basic


 Q29. Do you prefer using var args ?Core Java
Ans. Though var args are used rarely but they are pretty useful if a method is expected to receive variable number of arguments. For example - it's pretty useful for the main method as the caller has the flexibility to provide arguments in infinite ways.It provides a convenience and flexibility to the caller.

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

   Like         Discuss         Correct / Improve     var args  methods  method declarations  functions


 Q30. What is the difference between these two method declarations ?

private static void method(String[] arg)

and

private static void method(String... arg)
Core Java
Ans. First expects the argument as a string array whereas second expects variable number of string arguments or a string array.

So we can call both by providing string array as an argument but second can be called with 0 to n string arguments which cannot be done for first.

for example - We can call second method with any of following

method();
method("Hello");
method("Hello","World");
method(new String[4]);


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

   Like         Discuss         Correct / Improve     var args  methods  method declarations  functions


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