Java Basic Interview Questions And Answers

java basic interview questions and answers   Top 42 Java Basic Interview Questions And Answers

1) In Java, can an Interface be declared as final?

No

This is not possible i.e. Interface can't be stated as final. This will result in a compilation error.


2) What is the most significant feature of Java?

Java is a platform independent language.


3) In Java, what is platform independence?

Platform independence means that code can be written and compiled in one platform and can be executed in any other supported platform.

Basically after compilation we get the class file which can be executed in any other environment.


4) What is JVM in Java?

JVM stands for Java Virtual Machine, it's a run time environment for the compiled java class files.


5) Is JVM platform independent in Java?

JVMs are not platform independent. JVMs are platform specific run time implementation provided by the vendor.


6) What is the difference between JDK and JVM?

JDK is Java Development Kit, it is used for development purpose and it includes execution environment. But JVM is purely a run time environment so we won't be able to compile source files using JVM.


7) What are the pointers and do Java support these pointers?

Pointers are the reference handle to a memory location. Improper handling of pointers may lead to memory leaks and reliability issues. Unlike C++ and other programming languages Java doesn't support the pointers.


8) In Java, which is the base class of all the classes?

Object class is the base class of all the classes.

This superclass is the root of the class hierarchy and is under package java.lang. This is the default package so all the classes of this package will be automatically included without specifying them explicitly.


9) In Java, why java.lang in the default package?

Default package means all the classes in the package will be automatically imported and we don't need to add them explicitly. Java.lang is default package as it consist of very core classes which in needed in every Java program.

Java.lang contains fundamental classes. Some of the examples are Object, Class, different wrapper classes, Runtime, Compiler, Thread, System, String etc.


10) In Java, do we have multiple inheritance?

No, Java does not support multiple inheritance.

The reason Java not support multiple inheritance is the diamond problem which can lead to complex issues.


11) In Java, what are the local variables?

Local variables are the variables which are declared within the methods i.e. block of code.

Local variables should be initialized before we can access them.


12) In Java, what are the instance variables?

Instance variables are defined at class level.

We don't need to initialize instance variables as they are automatically initialized.


13) In Java, what are the static variables?

In Java static variables are class level variables where all the objects of the class refer to the same variable.

If one static object changes the value then the change will be reflected in all the objects.


14) What do you think, is Java a pure object oriented language?

No

Java have primitive data types like int, byte, long etc. which don't have objects, so it is not pure Object Oriented language.


15) In Java, what are the primitive data types?

Primitive means basic, In Java there are 8 primitive data types.

Following are the primitive data types in Java programming language: boolean , byte , char , int , short , long , float and double.


16) In Java, are arrays primitive data types?

No

Arrays are the collection of objects in Java, so these are not primitive data type.


17) In Java, what is the basic difference between Path and Classpath?

Path is use to define where the system can find different executables files whereas classpath is use to specify the location of .class files.


18) In Java, how we can make variable constant?

By declaring the variable as static and final we can make variable as constant.

Example:

final static int var=20;


19) What will be the output of below Java code snippet?
public class A{
public static void main(){}
}

public class B {
public static void main(){}
}

This piece of code snippet will throw error.

We can have multiple classes in single source file but only one can have main() method.


20) In Java, can we declare multiple classes in single .java source file?

Yes, we can have multiple classes in single java file.

But only one class can have main() method.


21) In Java, what is the return type of main() method?

The main method don't have any return type so its declared as void.


22) In Java, why main() method of the class is declared as static?

Static methods called by JVM even before instantiation of class so that is the reason main() method is declared as static.


23) In Java, what are the arguments of main() method?

The arguments of main() method is array of strings.


24) In Java, can we overload the main() method?

Yes, main method can be overloaded in Java.


25) In Java, can we declare main() method as final?

Yes, we can declare main() method as final.


26) In Java, is there any specific order of public and static for main() method?

No, there is no specific order of public and static. Only void should come before the main() method.


27) What is a package in Java?

It is the collection of related classes and interfaces.

Package declaration should be the first statement in the source file. Example: Package com.xy.abc;


28) In Java, which is the default package?

Java.lang is the default package which we don't need to import explicitly.


29) In Java, what are the access specifiers?

In Java access specifiers are use to specify the variables, methods and classes to other classes. Following are the access specifiers:
● Public
● Private
● Protected
● Default

They are used to set the visibility of the classes, methods and variables.


30) In Java, can we declare the class as private?

No, we cant declare the class as private.


31) In Java, can we declare the class as protected?

No, we cant declare the class as protected.

It will throw an error message.


32) In Java, what is the access scope of protected methods?

Protected methods can be access by the classes within the same package and by the subclasses of the classes in any of the package.


33) In Java, why we declare variable as final?

Final variable value can't be changed. These variables should be initialized before using them.


34) In Java, what will happen if we declare the method as final?

If the method is declared as final then we can't override that method in Java.


35) In Java, can we declare inner class as static?

Yes, in Java we can declare inner class as static class.


36) In Java, can we declare class as static class?

No, we cant declare class as static class in java.

Though we can have static inner class in Java.


37) In Java, what will be the output of the below code snippet?

public class Test{

Test(){
final static int var;
 var=20;
 System.out.println(""Value or var is >>""+var);
  }
}

There will be syntax error while processing this code snippet.

The reason is that we need to define the value for var variable as it is constant variable.


38) When can we define method as static in Java?

Whenever the particular method of a class need to be called before the creation of object of a class, then we can define that method as static.


39) In Java, can we use 'this' operator in static method?

No, we can't use 'this' operator in static method.


40) In Java, suppose I want to print the details or variable before the main() method call, how I can achieve that?

This can be achieved by using static block.

Static block executes at the time of class load and before the creation of an object. So anything that we want to print before main() method call can be put in this block.


41) Which are the class variables in Java?

In Java static variables are class level variables where all the objects of the class refer to the same variable.


42) In Java, can we declare static variables inside the method?

No, we cant declare static variables inside the method as these are class variables.

Compilation error occurs if we try to declare static variables inside method.