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 Newest | ||||
| ||||
Ans. MethodHandle is a modern, more flexible, more typesafe way of doing reflection. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  MethodHandle  Reflection | ||||
| ||||
Ans. All methods defined in a class that are not marked static are actually instance methods. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  methods  functions | ||||
| ||||
Ans. Yes, But the method should have some return type as otherwise it will be treated as a constructor. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  constructor  methods  method names | ||||
Try 1 Question(s) Test | ||||
| ||||
Ans. No. Static methods cannot be overridden and hence make no sense to be declared abstract. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  main method   abstract | ||||
Try 1 Question(s) Test | ||||
| ||||
Ans. Request Body in case of Get Request has no meaning and hence it's not parsed when the request is received. Alternatively Request Parameters are passed as either Path Params or Query Params. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  rest  web services  http  http methods  get request | ||||
| ||||
Ans. No, Main method is the entry point into an application. An application usually contain multiple classes to perform a function. Lets take an example of a House, House usually have only one external Door and you may have internal doors to move around within a house. Internal Doors are methods of classes whereas External Door is a special method called main method. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  main method | ||||
| ||||
Ans. Default methods results in multiple inheritance of behavior and not of state. In case we try to implement multiple interfaces with default method having same name and signature, and don't override it in implementation class, it will throw an error. For example - interface MyInterface { public void default myMethod(){ } } interface MyInterface2 { public void default myMethod(){ } } class MyClass implements MyInterface,MyInterface2 { } This code will compilation error "Duplicate Default Method" if we specify the definition of myMethod() in myClass, compiler won't complain and there is no conflict and MyClass can use overridden definition. But if we don't override myMethod() in MyClass, Java would be in conflict as to what definition should be carried to MyClass and hence throws compilation error. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  default methods  java 8  multiple inheritance  object oriented programming (oops)  oops concepts  diamond problem   interfaces | ||||
| ||||
Ans. Yes, compiler won't complain but at runtime it will give an error saying "Error: Main method not found in class". Even though we can use this method as any other private method, it cannot be invocate by executing the class. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  main method   main method visibility basic | ||||
Try 1 Question(s) Test | ||||
| ||||
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 | ||||
| ||||
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. 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. 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. 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. 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. 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, 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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, 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. 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. 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 | ||||