Interview Questions and Answers - Order By Newest Q91. Do you see any problem with this code
public class BuggyBread {
public static void main(String[] args) {
method("Hello");
}
private static void method(String... args){
for(String arg:args){
System.out.println(arg);
}
}
private static void method(String[] arg){
System.out.println(arg);
}
} Core Java
Ans. Yes, it will give compilation error and java will complain about duplicate method. Java treat var args internally as arrays and hence would result in same byte code for both method's syntax. 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 Q92. Will this code work
public class BuggyBread {
public static void main(String[] args) {
method("Hello","World");
}
private static void method(String[] arg){
System.out.println(arg);
}
}
What all possible changes we can make to method signature to make it work ? Core Java
Ans. It won't work as java won't find the method definition for method with 2 arguments.
We can either declare the method as
private static void method(String arg,String arg2)
or
private static void method(String... arg) 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 Q93. 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 Q94. 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 Q95. 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 Q96. 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 Q97. 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 Q98. 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 Q99. 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 Q100. 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 Q101. 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 Q102. 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 Q103. 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 Q104. 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 Q105. 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 Q106. 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 Q107. 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 Q108. 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 Q109. 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 Q110. 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 Q111. 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 Q112. 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 Q113. 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 methodsAns. 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 Q115. 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 Q116. 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 Q117. 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 Q118. Which of the following is false ? Core Java
a. A Class cannot override both hashcode and equals method. b. A class can override both hashcode and equals method. c. A Class must override hashCode method if its overridding equal method. d. A Class can override hashCode even if its not overridding equals method.Ans.a. A Class cannot override both hashcode and equals method.
a. Abstract Class is only meant to be sub classed and not supposed to be instantiated. b. Abstract class handlers can be used to handle derived class objects. c. We can't have an abstract class without abstract methods. d. Abstract class has member elements. Ans.c. We can't have an abstract class without abstract methods.
Q120. Which of the following keyword is expected with main method declaration ? Core Java
a. Final b. Static c. Volatile d. TransientAns.b. Static