Sunday, March 8, 2020

JAVA Programming - 1.2 Introduction to basic interview questions

1. What are the basic terminology of Simple JAVA program?
  • class keyword is an user defined datatype.
  • public keyword is an access modifier which represents visibility
  • static is a keyword, if we declare any method as static, it is known as static method. The core advantage of static method is that there is no need to create object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create object to invoke the main method. So it saves memory.
  • void is the return type of the method.
  • main represents the starting point of the program.
  • String[] args is used for command line argument.
  • System.out.println() is used print statement.

2. What are all the different ways to declare the main() method?
  • static public void main(String args[])
  • public static void main(String[] args)
  • public static void main(String []args)
  • public static void main(String args[])
  • public static void main(String... args)
class A{
static public void main(String... args){
System.out.println("Learn with Durga");
}
};

Note: Invalid java main method signature 
public void main(String[] args)
static void main(String[] args)
public void static main(String[] args)
abstract public static void main(String[] args)

3. What will happened to Java file during Compilation time and Run time?

During compilation time, java file is compiled by Java Compiler that does not interact with OS and converts the java code into byte code.

Image result for during run time of java file execution


During run-time, 
  • Class-loader: subsystem of JVM that is used to load class files.
  • Bytecode Verifier: Checks the code fragments for illegal code that can violate access right to objects.
  • Interpreter: reads bytecode stream then execute the instructions.


4. Can Java file name and class name be different?

Yes, if the class is not a public.

For example, the java code save as Tutorial_1.java with the class name of India where public keyword not used.

 class India{
public static void main(String arg[]){
System.out.println("Welcome to learn");
}

Output:
C:\Users\Durga>javac Tutorial_1.java
C:\Users\Durga>java India
Welcome to learn

The same java code with public keyword,
public class India{
public static void main(String arg[]){
System.out.println("Welcome to learn");
}
}
Output:
C:\Users\Durga>javac Tutorial_1.java
Tutorial_1.java:1: error: class India is public, should be declared in a file named India.java
public class India{
       ^
1 error

In order to overcome this, if the class is declared as public, then the file name and class name should be same.

5. How many public classes can a Java code have?

As we have seen in the previous question that whenever public keyword is used for the class, then file name should also be same as class name. So, if multiple classes are declared as public keyword, then there will be a confusion while assigning the file name.

6. How many main() methods can a Java code have?

Java program may have zero or as many as the number classes of main() method. If there is no main() method, program gets compiled successfully, during run-time it shows an error called main() method not found. If program has multiple main() methods, the required main method can be called with respective class name.

For example, the below Java program has three classes and main() methods.
class China{
public static void main(String arg[]){
System.out.println("Welcome to China");
}
}
class US{}
class India{
public static void main(String arg[]){
System.out.println("Welcome to India");
}
}

Output:
C:\Users\Durga>javac India.java

C:\Users\Durga>java India
Welcome to India

C:\Users\Durga>java China
Welcome to China

C:\Users\Durga>java US
Error: Main method not found in class US, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application


7. Difference between JDK, JRE and JVM?


JVM: 
JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed. JVMs are available for many hardware and software platforms. JVM, JRE and JDK are platform dependent because configuration of each OS differs. But, Java is platform independent. There are three notions of the JVM: specification, implementation, and instance.

The JVM performs following main tasks:
Loads code
Verifies code 
Executes code
Provides runtime environment

JRE:
JRE is an acronym for Java Runtime Environment. It is used to provide runtime environment. It is the implementation of JVM. It physically exists. It contains set of libraries + other files that JVM uses at runtime.

JDK :
JDK is an acronym for Java Development Kit. It physically exists. It contains JRE + development tools.



9. How many types of variable in JAVA?
1) Local Variable: A variable declared inside the method is called local variable.
2) Instance Variable: A variable declared inside the class but outside the method, is called instance variable . It is not declared as static.
3) Static variable: A variable which is declared as static is called static variable. It cannot be local.

For Example:
class Learn{
int data=50;//instance variable 
static int m=100;//static variable 
void method(){
int n=90;//local variable
}
}

PREVIOUS         NEXT

No comments:

Post a Comment