Core Java - Interview Questions and Answers for 'Function' | 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

   



Core Java - Interview Questions and Answers for 'Function' - 25 question(s) found - Order By Newest

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


 Q2. Difference between parameters and arguments ?Core Java
Ans. Parameters are the variables that the method is expected to receive along with the method call. Arguments are the values which are passed on while calling the methods.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     parameter   argument   method   function   parameter vs argument      basic


 Q3. What is the difference between these two method declarations ?

private static void method(String[] arg)

and

private static void method(String... arg)
Core Java
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


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


  Q5. How are values passed in Java ? By value or reference ?Core Java
Ans. Java only provides pass by value.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     ebay   pass by value   pass by reference   method call   methods   functions   function call     Asked in 10 Companies      basic        frequent


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


 Q7. Do you prefer using var args ?Core Java
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


Not frequently asked as it was introduced with Java 8.
 Q8. What is the @FunctionalInterface annotation ?Core Java
Ans. This is an informative annotation that specify that the interface is a functional interface. A Function Interface has only one abstract method and many default methods. Compiler generates an error if the interface specified with the annotation doesn't abide by the specifications for functional interface.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   java8   java 8   functional interface   default methods


 Q9. What is the input to the Reduce function ?

a. One Key and One Value
b. Multiple Keys and Multiple associated Values
c. Multiple Keys and One associated values with each
d. One key and associated values.
Ans. One key and associated values.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hadoop   bigdata   big data   map-reduce   map reduce   reduce function


 Q10. What are instance methods ?
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


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


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


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


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


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


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


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


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


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


 Q20. How do you define a functional interface?Core Java
Ans. Create interface with the only one non-overriding abstract method and annotate it with @FunctionalInterface

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     functional interface  java 8     Asked in 1 Companies


 Q21. How can we schedule Lambda function to run daily ?Amazon Web Services (AWS)
Ans. We can schedule that through CloudWatch.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     lambda function  aws lambda  aws serverless computing  amazon cloud serverless computing  amazon lambda function  aws cloudwatch  amazon cloudwatch


 Q22. 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 methods


 Q23. What are inline functions ?Programming Language
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.

 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


 Q24. What is arrow function in Typescript ? TypeScript
 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     arraow functions in typescript   lambda expression vs arrow function


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



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: