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 Rating | ||||
| ||||
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 | ||||
| ||||
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 | ||||
| ||||
Ans. It will compile fine and the method call will bind to method with specific argument and not with var arg i.e private static void method(String arg) It will print "Hello" upon execution | ||||
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 | ||||
| ||||
Ans. This will compile fine as we can provide 0 arguments for a var arg but will print nothing upon execution. | ||||
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 | ||||
| ||||
Ans. No, It will compile and execute perfectly fine. As we are using method with var args , we can call it with 0 to n arguments. | ||||
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 | ||||
| ||||
Ans. Though the call an be bind to either of these but in this case, 1st method will get priority and hence will be called. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  var args  method  function  overloading  scjp  ocjp | ||||
| ||||
Ans. No. Because java internally treats var args as arrays and hence both method declarations will generate the same byte code and hence would result in ambiguity while determining call binding. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  var args  methods  functions   method declaration  scjp  ocjp | ||||
| ||||
Ans. Because var args are not only allowed with the last argument in the method declaration. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  var args  methods  functions   method declaration  ocjp  scjp | ||||
| ||||
Ans. 1st is a valid and standard declaration. 2nd results in compilation error as only 2 dots are there. 3rd results in compilation error as three dots are not consecutive and broken. 4 through 6 may not be standard and ideal way of declarations but they are valid and will compile and work fine. 7 is valid declaration. 8 and 9 will result in compilation error as var args can only be provided to last argument. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  var args  methods  functions   method declaration  scjp  ocjp | ||||
| ||||
Ans. Var args is the feature of Java that allows a method to have variable number of arguments. Var args are used when we are not sure about the number of arguments a method may need. Internally java treats them as array. Declarations of methods with var args void method(int... x){}; void method(int x, int... y){}; | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  var args  methods  functions   method declaration Asked in 2 Companies | ||||
| ||||
Ans. BuggyBread method without any return type is the constructor which get's called upon object creation whereas BuggyBread method with return type of void is just another method that needs to be called explicitly for it's invocation. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  method  return type  constructor | ||||
| ||||
Ans. All methods are not expected to return something but Yes, all methods are expected to have a return type. If a method returns nothing, it can be declared with the return type void. Constructors are not expected to have any return types , not even void. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  return type   method  constructor | ||||
| ||||
Ans. http://www.devinline.com/2015/10/Lock-Vs-synchronized-in-java.html | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java.util.concurrent.locks  synchronization  synchronized methods | ||||
| ||||
Ans. Public constructor is simple and easy as it's the default way of object creation. So there are no additional coding overheads as compiler provides the default constructor if none is provided by coder. With static final methods, it facilitates loose coupling by segregating the responsibility of object creation to a separate method. Validation can be done on the constructor arguments before calling it. Moreover if any adaption on the arguments is required that can achieved easily with factory method.On the flip side, there is coding overhead and additional method call. | ||||
Sample Code for constructor Sample Code for factory | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  constructor  factory design pattern  factory method | ||||
| ||||
Ans. it depends on the implementation of equals method of the respective class. If no definition is provided and it uses the default definition of the object class, two references are equal only if they point to the same object. We can have such an equality defined for a particular class objects if we provide appropriate implementation of equals method comparing all those fields. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  equals  equals method | ||||
| ||||
Ans. No. It won't be null but an empty array with size 0 | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  main method | ||||
| ||||
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 : | ||||
Like Discuss Correct / Improve  override static methods | ||||
| ||||
Ans. Likewise classes, methods also provide a way for code reuse and abstraction. Code is reused, clean and easy to understand if classified properly within classes and methods. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  methods  functions  advantages of methods in java | ||||
| ||||
Ans. Mutator is another name for setter methods, i.e the method allows for mutating the property of an object and eventually the state of the object. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  setter method  mutator | ||||
| ||||
Ans. Yes, but in that case the derived class itself should be abstract. We cannot have an object of a class without definition for the method which has been declared abstract in the parent. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  overriding  overriding abstract methods  abstract methods | ||||
| ||||
Ans. Abstract methods are the methods that need to be overridden in the derived class ( either as implementing method or again abstract method ) so it's not only allowed but its required to override abstract method in the derived class. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  overriding  abstract methods  overriding abstract methods | ||||
| ||||
Ans. Yes | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  overloading  abstract methods  overloading abstract methods | ||||
| ||||
Ans. Introduced with java 8 , Method References help us to point to methods by their name. A method references is described using :: symbol | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  method reference  java 8  lambda expressions | ||||
| ||||
Ans. wait and notify are declared final in object class and hence cannot be overridden. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  object class methods  wait  notify | ||||
| ||||
Ans. After a thread is started using a call to start method, JVM invokes the thread’s run method when the thread is initially executed. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  threads  thread run method Asked in 1 Companies | ||||
| ||||
Ans. We can overload the main method by specifying different argument types. For example - 2 main methods with different arguments is perfectly legal public static void main(); public static void main(String[] args); The following are not legal as compiler will complain of duplicate methods public static void main(String[] args); public static void main(String[] args); Even The following are not legal as we cannot overload on return types public static String main(String[] args); 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 Asked in 2 Companies | ||||
| ||||
Ans. Yes we can have a main method with string[] argument in every class of an application. When we execute an app we specify the starting point i.e the class that will get the control first and hence main method of that class gets executed. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  main method Asked in 3 Companies | ||||
| ||||
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 : | ||||
Like Discuss Correct / Improve  main method  static Asked in 4 Companies Basic | ||||
| ||||
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 : | ||||
Like Discuss Correct / Improve  static  static method  static variables | ||||
| ||||
Ans. No, If both Parent and Derived are outer classes. public class Vehicle { private static String manufacturingDate = "2016"; } public class Car extends Vehicle{ public static void main(String args[]){ System.out.println(manufacturingDate); // error - The field Vehicle.manufacturingDate is not visible } } Yes, If derived is the inner class of Parent. public class Vehicle { private static String manufacturingDate = "2016"; static public class Car extends Vehicle{ public static void main(String args[]){ System.out.println(manufacturingDate); // no problem } } } | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  private members   private methods   private variables   inheritance  object oriented programming (oops)  oops concepts   members visibility   inner classes  nexted classes basic   frequent | ||||