Top 40 Important Java Interview Questions and Answers
Here we will discuss about Top 40 Important Java Interview Questions and answers that beginners needs to know. Java is an object oriented programming language and very useful. So there lots and questions that are ask in various interviews.
So dear readers here we have design various question and answer to solve your problem. So remove your interview fear and learn java interviews question in a easy manner.
Note-To view the answers please click on the questions.
2) What are the differences between C++ and Java?
3) List the features of Java Programming language.
4) What do you understand by Java virtual machine?
5) Comparison between JVM, JRE, and JDK?
6) How many types of memory areas are allocated by JVM?
9) Where is the source code of java written?
Basic Requirements to Run a Java Program
Following are the basic requirements that are necessary to run a java program. so before you start making a java program you have to check that you have JDK (Java Development Kit) in your system. To download the JDK just click on the following-
To learn more Important Java Interview Questions and answers you first you have to install JDK in you system.
Steps to Installing JDK (Java Development Kit)
JDK (Java Development Kit) allows programmers to run their java codes. Programmers can install different version on JDK on the same machine but we advise install only one version (latest version) of JDK.
Steps to Installing JDK 8 for Windows
To install the JDK 32bit and 64bit you have to follow the following instructions:
Step 1) first opens the link. Then download the JDK 8.
Step 2) then Accept License Agreement and Download the Java 8 JDK (for version 32 bit and 64 bit).
Step 3) Now click on the installation link. Then select the “I reviewed and accept the Oracle Technology Network License Agreement for Oracle Java SE” this will redirect you to the login page. If user has the oracle account then he/she will direct download the file else you have to sign up.
Step 4) Run the exe file of JDK once the installation process is complete.
Step 5) Then select the PATH where you want to install the JDK in the Windows or you can leave it default. Then click on the next button.
Step 6) Now follow the screen instruction to complete the installation process.
Step 7) Click on the close button once the installation process is complete.
After successful installation of the JDK now you are able to run a java program. so following are the few more important Java Interview Questions and answers-
1) How to check java version?
To check java version you need to execute the following command on the command prompt or terminal-
Java – version
To see the answer click on the question.
To see the answer click on the question.
To see the answer click on the question.
5) Explain static variable?
Static variable is also known as class variable. To declare a class variable in a class static keyword is used. Constructor or a block, in case of outside of a method.
6) Discuss the Class Variable?
These are variables declared with in a class, outside any method, with the static keyword.
Class variable are static variable that are declared inside a class but outside of method. Class variables are declared using ‘static’ keyword
7) Discuss the variables that a class can consist of?
Following are the variable that a class consists-
- Local variable,
- instance variables and
class variables.
8) What is class?
Basically class is a blueprint from where objects are created. A class has:
- Fields,
- Method that explain the behavior of the object
9) List few Java keywords?
Following are the few keywords-
- import,
- try, catch, finally
- super, etc.
10) Discuss the access specifiers in Java and their types.
To see the answer click on the question.
11) What is the default value of local variables?
No default value (neither object reference nor primitives) is assign to a local variable.
12) What happen if you write static public void instead of public static void?
Here order of specifiers does not consider and program compiles and execute successfully.
13) If you do not provide any arguments on the command line, then what value stored in the String array passed into the main() method, discuss?
It is not null but empty.
14) Is delete, next, main, exit or null keyword in java?
NO, there is no delete, next, main, exit or null keywords are in java.
15) What is data encapsulation in java?
It helps to follow the concept of modularity. Data encapsulation means binding the attributes of data & their behavior in single unit. So it helps programmers to achieve data hiding.
16) What is instance variable and a local variable?
When a variable is accessible to all their methods then these variables are known as instance variables.
For example, consider the following code segment,
class Gpstest
{
public String companyName; // instance variable
public int companyAge; // instance variable
}
Variables that are define under a method, block, or constructor and accessible only inside them are known as local variables
For example, consider the following code segment,
class Gpstest
{
public void gmethod()
{
public String companyName; // local variable
public int companyAge; // local variable
}
}
17) discuss the reason why is Java not a pure object oriented language?
Java is not a pure object oriented programming language because java supports primitive data types (i.e., byte, boolean, char, short, int, float, long, and double).
18) Discuss the method in java?
A block of code that is accessible only when it is called. One can pass the parameter in the methods to perform various tasks.
19) api in java?
List of various classes (pre-written) is known as java API (java Application Programming Interface). These list of classes are the part of JDK and consists all java–
- Classes,
- Interfaces,
- Packages
with their fields, constructors, & methods.
20) Discuss the concept of collection in java?
To group multiple items in a single unit java uses containers. These containers are known as collections. For example, list of names, etc.
Following are the few collection classes available in java –
- Stack
- Vector
- Hashtable
- Array, etc.
21) Explain the inheritance in Java?
The concept of re-usability of code is achieved through inheritance in java. So it means one class can acquire the properties and methods of other class.
The benefit of inheritance is that programmer written the code once and use multiple times. In java ‘extends’ keyword is used to inherit the features and methods of other class.
22) Explain the enumeration in Java?
List of named constant is known as enumeration. It defines a class type. To create it ‘enum’ keyword is used. By default each enumeration is static, public and final. Enumeration may contain methods, constructors & instance variable and is not initiate using ‘new’ keyword.
23) With suitable example discuss the concept of taking input in Java?
To take the input from keyboard in java Scanner class is used. This class is found in the java.util package.
Note-There is also various other ways that are used to take input from keyboard.
Scanner class implements the Iterator and closeable interface and extends the object class.
Consider the following code,
import java.util.*;
public class Gps {
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print(“Enter company name: “);
String name = in.nextLine();
System.out.println(“Company Name is: ” + name);
in.close();
}
}
24) Why multiple inheritance is not supported in java?
Java does not support multiple inheritance because it leads to a deadly diamond problem. Java support multiple inheritance concept using interface but interface only has method signature no other code. So, logically implementing interface is not multiple inheritance. In java you cannot extend more than one class.
25) What is array in java?
In java container objects that have fixed number of values of same type is known as array. You can define the array length during the creation of arrays. Once array is created its length is fixed.
In an array of 10 boxes the counting is start from index of 0 and end to 9.
26) What is garbage collection in java?
An automatic process to delete the objects is known as garbage collection. In java, programmers do not need to mark object for delete explicitly. It is done by the garbage collection when required. It means once the requirements meets to the JVM specification then garbage collection is automatically implemented by the JVM.
27) What is Overloading? discuss.
To see the answer click on the question.
28) Discuss the concept of Interface in java?
To see the answer click on the question.
29) Discuss the concept of Abstract class?
To see the answer click on the question.
30) What are all the Classes and Interfaces that are available in the collections?
To see the answer click on the question.
31) Difference between Abstract class and Interface.
Abstract Class | Interface |
Have default constructor and called when the concrete subclass is instantiated. | Have no constructor so cannot be instantiated.
|
Have both abstract and non-abstract methods. | Only abstract methods are declared. |
It has instance variables. | Interface only have constants. |
Must Read – Overloading in Java