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. |
|
| ||||
Core Java - Interview Questions and Answers for 'Method declaration' - 11 question(s) found - Order By Newest | ||||
| ||||
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. 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 | ||||
| ||||
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. 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 | ||||
| ||||
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. 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 | ||||
| ||||
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 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 | ||||