You are on page 1of 4

Sunday, 29 January 2023

Core Java Questions & Answers | Set – 1


By : Gayatri Mishra
https://www.linkedin.com/in/gayatri-mishra-freelancecoach/

Question - 1: Why main method is always static in java program ?

In any Java program, the main() method is the starting point from where
compiler starts executing the code. So, the compiler needs to call the main()
method.
If the main() is allowed to be non-static, then while calling the main() method
JVM has to instantiate its class.
While instantiating it has to call the constructor of that class, There will be
ambiguity if the constructor of that class takes an argument.
Static method of a class can be called by using the class name only without
creating an object of a class.

1
https://www.linkedin.com/in/gayatri-mishra-freelancecoach/

Question -2 : What are the new features added in Java 8 w.r.t Interface ?

| Changes in Interface | Default & Static method

Java 8 interface allows static and default methods .


By using both we can define the method inside interface.
We can call the default method using the object of the class which
implemented from the same interface.
We can call the static method by using the interfaceName.methname();
Concept of abstract method is same as earlier.

Please check the below sample code:

2
https://www.linkedin.com/in/gayatri-mishra-freelancecoach/

Question – 3: What is functional interface in Java ?

The interface which is having only single abstract method is known as


functional interface.
Annotation :
@FunctionalInterface :
This is an annotation used to make sure that interface can not have more than
one abstarct method.
At the same time functional interface can have multiple default and static
methods.
Example of functional interface :
Runnable ----> This interface contains only run method.{Reference -
Threadclass implements Runnable, having only one abstract method i.e run
method}

Please check the below sample code :

3
https://www.linkedin.com/in/gayatri-mishra-freelancecoach/

Question -4: Can we convert different primitive data type values to String in
Java ?
What is valueof method ?

 Yes we can convert any primitive data type values to String using
String.valueof()
valueOf() :
 valueof() method is used to convert any primitive data types values to
string.
 By using valueof method we can convert the int , long , char , double ,
boolean to String
 Syntax :
String val = String.valueof(var)

You might also like