Java Interview Questions
Java Interview Questions
JVM (Java Virtual Machine): JVM(Java Virtual Machine) acts as a run-time engine to run
Java applications. JVM is the one that actually calls the main method present in a Java code.
JVM is a part of JRE(Java Runtime Environment).
JRE (Java Runtime Environment): JRE refers to a runtime environment in which
Java bytecode can be executed. It implements the JVM (Java Virtual Machine) and provides all
the class libraries and other support files that JVM uses at runtime. So JRE is a software package
that contains what is required to run a Java program. Basically, it’s an implementation of the
JVM which physically exists.
JDK(Java Development Kit): It is the tool necessary to compile, document and package Java
programs. The JDK completely includes JRE which contains tools for Java programmers. The
Java Development Kit is provided free of charge. Along with JRE, it includes an
interpreter/loader, a compiler (javac), an archiver (jar), a documentation generator (javadoc) and
other tools needed in Java development. In short, it contains JRE + development tools.
Q5. Define class and object. Explain them with an example using java.
Class: A class is a user defined blueprint or prototype from which objects are created. It
represents the set of properties or methods that are common to all objects of one type. In general,
class declarations can include these components, in order:
Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword
extends. A class can only extend (subclass) one parent.
Interfaces: A comma-separated list of interfaces implemented by the class, if any, preceded by
the keyword implements. A class can implement more than one interface.
Object: It is a basic unit of Object Oriented Programming and represents the real life entities. A
typical Java program creates many objects, which as you know, interact by invoking methods.
An object consist of :
State : It is represented by attributes of an object. It also reflect the properties of an object.
Behavior : It is represented by methods of an object. It also reflects the response of an object
with other objects.
Identity : It gives a unique name to an object and enables one object to interact with other
objects.
For Example: Employee is an example of a class
A specific employee with unique identification is an example of an object.
class Employee
{
// instance variables declaration
// Methods definition
}
An object of employee is a specific employee
Employee empObj = new Employee();
One of the objects of Employee is referred by ‘empObj’
Q6.What is a method? Provide several signatures of the methods
A Java method is a set of statements to perform a task. A method is placed in a class.
Signatures of methods: The name of the method, return type and the number of parameters
comprise the method signature.
A method can have the following elements in it signature:
– Access specifier – public, private, protected etc. (Not mandatory)
– Access modifier – static, synchronized etc. (Not mandatory)
– Return type – void, int, String etc. (Mandatory)
– Method name – show() (Mandatory)
– With or without parameters – (int number, String name); (parenthesis are mandatory)
Example:
class Test {
void fun1() {}
public double fun2(double x) {}
public static void fun3() {}
public static void fun4(String x) {}
}
For Example:
public class StaticMethod {
public static void printMe()
{
System.out.println("Static Method access directly by class name!");
}
}
public class MainClass {
public static void main(String args[])
{
StaticMethod.printMe();
}
}
Q12. Why static methods cannot access non static variables or methods?
Ans) A static method cannot access non static variables or methods because static methods can
be accessed without instantiating the class, so if the class is not instantiated the variables are not
initialized and thus cannot be accessed from a static method.