Java interview question

Basic java interview question

What is the most important feature of Java?.
Ans: Java is a platform independent language.

What is a JVM?
Ans: JVM is Java Virtual Machine which is a run time environment for the compiled java class files.

What is the difference between a JDK and a JVM?
Ans: JDK is Java Development Kit which is for development purpose and it includes execution environment also. But JVM is purely a run time environment and hence you will not be able to compile your source files using a JVM.

Is Java platform independent?.

Ans:  Yes. Java is a platform independent language. We can write java code on one platform and run it on another platform. For e.g. we can write and compile the code on windows and can run it on Linux or any other supported platform.

What do you mean by platform independence? 
Ans : Platform independence means that we can write and compile the java code in one platform (eg: Windows) and can execute the class in any other supported platform eg: (Linux,     Solaris, etc).

How many types of memory areas are allocated by JVM?

Ans: There are five types of memory areas inside JVM
1.Class(Method)Area
2.Heap,
3.Stack,
4.Program Counter Register and
5.Native Method Stack

What is classloader?

Ans: The classloader is a subsystem of JVM that is used to load classes and interfaces.There are many types of classloaders e.g. Bootstrap classloader, Extension classloader, System classloader, Plugin classloader etc.

What is class?

Ans : Class is nothing but a template that describes the data and behavior associated with instances of that class

Can a .java file contain more than one java classes?
Ans : Yes.A.java file contain more than one java classes, provided at the most one of them is a public class.

What is the parent class of all classes in java?
Ans: Object class.this is present inside java.lang package.

Does Java support multiple inheritance?
Ans: Java doesn’t support multiple inheritancein case of class but it support in case of interface.

What is constructor?

Ans: Constructor is a special types of  method that is used to initialize the state of an object. It is invoked at the time of object creation.

Is Java a pure object oriented language? 
Ans: Not because Java uses primitive data types and hence is not a pure object oriented language.

What is Casting?

Ans : Casting is used to convert the value of one type to another.

What is difference between Path and Classpath? 
Ans:  Path is used to specify the location of the executables(.exe) files and classpath is used to specify the location of .class files.

What is local variables in java? 
Ans: Local varaiables are those which are declared within a methods. Local variables should be initialised before accessing them. local variables are not automatically initialized to their default values.

What is the purpose of default constructor?

Ans: The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class.

What is instance variables in java? 
Ans: Instance variables are those which are defined at the class level. Instance variables need not be initialized before using them as they are automatically initialized to their default values.

What is default and parameterized constructors?

Ans: Default: Constructors with no arguments are known as default constructors
Parameterized: Constructor with arguments are known as parameterized constructors.

Should a main method be compulsorily declared in all java classes?
Ans: No not required. main method should be defined only if the source class is a java application.

What is the return type of the main method?
Ans: Main method doesn’t return anything hence declared void.

What is  Heap Memory 

Ans: Heap memory is used by java runtime to allocate memory to Objects and JRE classes. Whenever we create any object, it’s always created in the Heap space. Any object created in the heap space has global access and can be referenced from anywhere of the application.

Java Stack Memory

Ans: Java Stack memory is used for execution of a thread. They contain method specific values that are short-lived and references to other objects in the heap that are getting referred from the method. Stack memory is always referenced in LIFO (Last-In-First-Out) order.

Why is the main method declared static?
Ans: main method is called by the JVM even before the instantiation of the class hence it is declared as static.

What is the arguement of main method?
Ans: main method accepts an array of String object as arguement.

Does constructor return any value?

Ans: yes, that is current instance (You cannot use return type yet it returns a value)

What is Type casting in Java?

Ans: To create a conversion between two incompatible types, we must use a cast. There are two types of casting in java: automatic casting (done automatically) and explicit casting (done by programmer).

Can you make a constructor final?

Ans: No, constructor can’t be final.

Is constructor inherited?

Ans: No, constructor is not inherited.

Can a main method be overloaded? 
Ans: You can have any number of main methods with different method signature and implementation in the class.

Can a main method be declared final? 
Ans: Any inheriting class will not be able to have it’s own default main method.

Does the order of public and static declaration matter in main method?
Ans: No it doesn’t matter but void should always come before main().

Why main method is static?

Ans: because object is not required to call static method if It were non-static method,jvm creats object first then call main() method that will lead to the problem of extra memory allocation.

What is a package?
Ans: Package is a collection of related classes and interfaces. package declaration should be first statement in a java class.

What is Function Overriding and Overloading in Java ?

Ans: Method overloading in Java occurs when two or more methods in the same class have the exact same name, but different parameters. On the other hand, method overriding is defined  when a parent class have the same method define in child class. Overridden methods must have the same name, argument , and return type.

Can a class be declared as protected?
Ans: A class can’t be declared as protected. only methods can be declared as protected.

What is static and dynamic binding?

Ans: A binding that happens at compile time is known as static binding while binding that happens  at runtime is known as dynamic binding.

What is the purpose of declaring a variable as final? 
 Ans: A final variable’s value can’t be changed. final variables should be initialized before using them.

Leave a comment