Prev Next

Java / Methods

1. Is the main() method required for all java classes? 2. Can a main method be declared final? 3. Difference between arguments and parameters in Java. 4. What is the return type of main() method? 5. Why does the main() method declared static? 6. What is the the argument for main() method? 7. Can a main() method be overloaded? 8. When no command line arguments are passed, does the String array parameter of main() method be null? 9. Can we invoke a static method on a null object reference in Java? 10. Explain the Default method for interfaces in Java 8. 11. Advantages of default method in Interface feature in Java 8. 12. Explain the static interface methods feature in Java 8. 13. Advantages of using static interface method in Java 8. 14. Difference between default and static methods in Java interface. 15. Explain pass by reference and pass by value in Java? 16. Difference between System.exit(0), exit (1) and exit(-1) in Java. 17. What is virtual function/method in Java? 18. Can we define a static method in a Java interface? 19. What is fluent interface in Java? 20. Explain method chaining in Java. 21. What is synthetic method in Java? 22. What is bridge method in Java? 23. What is a method reference in Java 8? 24. Types of method references in Java 8. 25. Are Java static calls less expensive than non-static calls? 26. What is lambda expression in Java 8? 27. What is the target type of lambda expression? 28. Can you Serialize lamda expression in Java8? 29. What is System.out::println expression? 30. Other names for default methods in Java 8. 31. Is abstract modifier allowed in default method? 32. What is the scope of default methods in Java8? 33. How do I extend Interfaces that contain Default Methods? 34. Explain the scenario how Default Method can cause Multiple Inheritance Ambiguity Problem. 35. Why Java doesn't allow overriding of static methods? 36. Explain public static void main(String args[]) method signature. 37. How to call a default method of an interface in a class? 38. How to invoke a static method of an interface? 39. Explain System.out.println().

1. Is the main() method required for all java classes?

Posted on Apr 1, 2016 by Senthil Kumar.

No. main() method is defined only if the java class is an application and has to execute.

Read full answer

2. Can a main method be declared final?

Posted on Apr 1, 2016 by Senthil Kumar.

Yes, so that the subclass cannot override main() method.

Read full answer

3. Difference between arguments and parameters in Java.

A parameter is that appears in the definition of the method. An argument is the instance or value passed to the method during runtime when the method is invoked.

Read full answer

4. What is the return type of main() method?

Posted on Apr 1, 2016 by Senthil Kumar.

void. main() method does not return any value.

Read full answer

5. Why does the main() method declared static?

Posted on Apr 1, 2016 by Senthil Kumar.

JVM need to access the main() method even before the class instantiation so begin the execution. so it is declared static.

Read full answer

6. What is the the argument for main() method?

Posted on Apr 1, 2016 by Senthil Kumar.

There is only one argument that main() method accepts. main() method accepts a vararg/array of string object as an argument.

Read full answer

7. Can a main() method be overloaded?

Posted on Apr 1, 2016 by Senthil Kumar.

Yes.

Read full answer

8. When no command line arguments are passed, does the String array parameter of main() method be null?

It is an empty Array with zero elements and not null. public class MainMethodClass { public static void main (String [] args) { System.out.println( "Size of the Arg Array=" + args.length); } } Output: 0

Read full answer

9. Can we invoke a static method on a null object reference in Java?

Yes. The compiler optimizes the code to invoke the static method using null object reference since object instance is not required to invoke a static method....

Read full answer

10. Explain the Default method for interfaces in Java 8.

In Java 8, a new feature "default method implementation for interfaces" is introduced that facilitates backward compatibility for the old interfaces to leverage the lambda expression capability of Java 8 and also existing libraries implementing these interfaces need not have...

Read full answer

11. Advantages of default method in Interface feature in Java 8.

Using default methods for interfaces eliminates the need for utility classes , for example, the java.util.Collections utility class is not required when all of its methods are provided in the Collection interface itself as default methods. It helps in extending...

Read full answer

12. Explain the static interface methods feature in Java 8.

The interface static methods are concrete methods implemented in the interface that prevents the implementation classes from overriding to ensure the proper and uniform implementation being used across all the implementation classes. The static interface method has method body and...

Read full answer

13. Advantages of using static interface method in Java 8.

Java interface static methods are suitable for providing utility function and acts as utility methods, for example sorting a Collection, validations, reversing a collection etc. Java interface static method ensures quality and security by preventing the implementation classes to override...

Read full answer

14. Difference between default and static methods in Java interface.

Default method. Static method....

Read full answer

15. Explain pass by reference and pass by value in Java?

When an object is passed by value, this means that a copy of the object is passed. Thus, even if changes are made to that object, it doesn't affect the original value....

Read full answer

16. Difference between System.exit(0), exit (1) and exit(-1) in Java.

A method is a named block of code on a class or instance. Overload by parameter list; override an instance method in a subclass with the same signature.

Read full answer

17. What is virtual function/method in Java?

In Java, all non-static methods are considered as virtual functions . The Final methods cannot be overridden and the private methods cannot be inherited so it is considered as non-virtual.

Read full answer

18. Can we define a static method in a Java interface?

With Java 8, interfaces can have static methods . They can also have concrete instance methods, but not instance fields.

Read full answer

19. What is fluent interface in Java?

The fluent interface enables to apply multiple properties/setters on an object just by using dots(.) without having to use the actual object reference....

Read full answer

20. Explain method chaining in Java.

Method chaining is a mean by which fluent API could be used so that multiple setters on a method be invoked continuously by just using dot(.) that improves readability and less code....

Read full answer

21. What is synthetic method in Java?

Synthetic method is a compiler created method after the type erasure on the generic methods.

Read full answer

22. What is bridge method in Java?

Java compiler creates a synthetic method known as bridge method when compiling a class or interface that extends a parameterized class or interface as part of type erassure.

Read full answer

23. What is a method reference in Java 8?

A method reference is a Java 8 construct that can be used for referencing a method without invoking it. It is used for treating methods as Lambda Expressions....

Read full answer

24. Types of method references in Java 8.

A method reference to a static method. A method reference to an instance method of an object of a particular type.

Read full answer

25. Are Java static calls less expensive than non-static calls?

Static methods are usually faster (comparatively) as we don't need 'this' object reference for static calls. However, the performance is negligible when compared to the non-static method calls and no decision should be made based on performance when choosing static...

Read full answer

26. What is lambda expression in Java 8?

A lambda expression is an anonymous function that you can use to create delegates or expression tree types. By using lambda expressions, you can write local functions that can be passed as arguments or returned as the value of function...

Read full answer

27. What is the target type of lambda expression?

The data type that the methods expect is called the target type. To determine the type of a lambda expression, the Java compiler uses the target type of the context or situation in which the lambda expression was found.

Read full answer

28. Can you Serialize lamda expression in Java8?

Yes. You can serialize a lambda expression if its target type and its captured arguments are serializable.

Read full answer

29. What is System.out::println expression?

System.out::println method in Java 8 is a static method reference to println method of out object of System class.

Read full answer

30. Other names for default methods in Java 8.

Default method is also known as defender method or virtual extension method.

Read full answer

31. Is abstract modifier allowed in default method?

No, we encounter compile time error. default modifier is used for methods with implementation.

Read full answer

32. What is the scope of default methods in Java8?

All the method declarations in an interface, including default methods, are implicitly public , so you can omit the public modifier.

Read full answer

33. How do I extend Interfaces that contain Default Methods?

When you extend an interface that contains a default method, you may do one of the following: Not mention the default method at all, which lets your extended interface inherit the default method. Redeclare the default method to make it...

Read full answer

34. Explain the scenario how Default Method can cause Multiple Inheritance Ambiguity Problem.

Java class can implement multiple interfaces and each interface can define default method with same method signature, therefore, the inherited methods can conflict with each other. public interface Interface1 { default void myDefaultMethod (){ System.out.println( "Interface One default method" );

Read full answer

35. Why Java doesn't allow overriding of static methods?

Overriding depends on having an instance of an class and static method is not associated to instance of the class.

Read full answer

36. Explain public static void main(String args[]) method signature.

public access modifier specify who can access this method. Public will be accessible by any Class from any package....

Read full answer

37. How to call a default method of an interface in a class?

Use super keyword along with interface name to invoke a default method. public interface Animal { default void feed () { System.out.println( "Feed some food to animals" ); } } public class Dog implements Animal{ public void feed () {...

Read full answer

38. How to invoke a static method of an interface?

Invoke the static method using the name of the interface. public interface Animal { static void doSwim () { System.out.println( "Animal can swim" ); } } public class Dog implements Animal { public void canSwim () { Animal.doSwim(); } }

Read full answer

39. Explain System.out.println().

System.out is the JVM standard output PrintStream. println writes the value and a line separator; it is synchronized and slow in hot loops.

Read full answer

«
»

Comments & Discussions