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

   



Interview Questions and Answers - Order By Rating

   next 30
 Q31. 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


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


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


 Q34. Do you see any problem with this code

public class BuggyBread {

   public static void main(String[] args) {
      method();
   }

   private static void method(String... args){
      for(String arg:args){
         System.out.println(arg);
      }
   }
}
Core Java
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


 Q35. Do you see any problem with this code

public class BuggyBread {

   public static void main(String[] args) {
      method("Hello","World");
   }

   private static void method(String... args){
      for(String arg:args){
         System.out.println(arg);
      }
   }
}
Core Java
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


 Q36. Which method will get called if we call it as method(1)

void method(int x ){};
void method(int... x){};
Core Java
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


 Q37. Can we overload method as following ?

void method(int... x){};
void method(int[] x){};
Core Java
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


 Q38. Why following method declarations are not valid ?

void method(int... x, int y){};
void method(int... x,int... y){};
Core Java
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


 Q39. Which of the following are valid declarations

1. void method(int... x){};
2. void method(int.. x){};
3. void method(int.. .x){};
4. void method(int ...x){};
5. void method(int... x){};
6. void method(int ... x){};
7. void method(int x, int... y){};
8. void method(int... x, int y){};
9. void method(int... x,int... y){};
Core Java
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


 Q40. What are var args ?

or

What is the use of var args ?
Core Java
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


 Q41. What is the difference between the 2 methods in Java ?

Class BuggyBread {

BuggyBread(){
}
void BuggyBread(){
}
}
Core Java
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


 Q42. Does all methods need to have a return type ? Is it applicable to constructors too ?Core Java
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


 Q43. What are the advantages and disadvantages of using lock classes from java.util.concurrent.locks vs. synchronized methods ?Core Java
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


 Q44. What are the trade offs between public constructor and static final method ?Core Java
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


 Q45. In Java, if object A contains same the same data of object B, are they equal?Core Java
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


 Q46. If we don't provide any argument to the executed program, What arguments will be passed to the main method ? Will the String array argument be null ?Core Java
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


 Q47. Is it a good practice to override static methods ?Design
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


 Q48. What are the advantages of using methods in Java ?Core Java
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


 Q49. What is a mutator in Java?Core 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


 Q50. Can we override an abstract method with abstract method in the derived class ?Core Java
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


 Q51. Can we override abstract methods ?Core Java
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


 Q52. Can we overload abstract methods ?Core Java
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


 Q53. What is a method reference in Java ?Core Java
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


 Q54. Can we override wait() or notify() methods?Core Java
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


 Q55. Who invokes a thread's run method ?Core Java
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


 Q56. Can we have multiple main methods in a single class ?Core Java
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


 Q57. Can an application have multiple main methods within different classes ? If yes, How will the app decide which one to be executed ?Core Java
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


 Q58. Why main method is declared static ?Core Java
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


 Q59. What could be the problems due to the usage of static methods and elements ? Core Java
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


 Q60. Can we access private members of the parent class ? i.e Are private members of the class visible in the derived class ?Core Java
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


previous 30   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: