Core Java Interview Questions

Below are the Top 30 Core Java Interview Questions based on the candidate's experiences and the company's interview pattern.


 

1. What do you understand by Java virtual machine?

Java Virtual Machine is a virtual machine that enables the computer to run the Java program. JVM acts like a run-time engine which calls the main method present in the Java code. JVM is the specification that must be implemented in the computer system. The Java code is compiled by JVM to be a Bytecode that is machine-independent and close to the native code.

 

2. What is the difference between JDK, JRE, and JVM?

JVM:

JVM is an acronym for Java Virtual Machine; it is an abstract machine that provides the runtime environment in which Java bytecode can be executed. It is a specification that specifies the working of Java Virtual Machine. Its implementation has been provided by Oracle and other companies. Its implementation is known as JRE.

 

JVMs are available for many hardware and software platforms (so JVM is platform-dependent). It is a runtime instance that is created when we run the Java class. There are three notions of the JVM: specification, implementation, and instance.

 

JRE:

JRE stands for Java Runtime Environment. It is the implementation of JVM. The Java Runtime Environment is a set of software tools that are used for developing Java applications. It is used to provide the runtime environment. It is the implementation of JVM. It physically exists. It contains a set of libraries + other files that JVM uses at runtime.

 

JDK:

JDK is an acronym for Java Development Kit. It is a software development environment that is used to develop Java applications and applets. It physically exists. It contains JRE + development tools. JDK is an implementation of any one of the below given Java Platforms released by Oracle Corporation:

 

1)Standard Edition Java Platform

2)Enterprise Edition Java Platform

3)Micro Edition Java Platform

 

3. How many types of memory areas are allocated by JVM?

1) Class(Method) Area: Class Area stores per-class structures such as the runtime constant pool, field, method data, and the code for methods.

2) Heap: It is the runtime data area in which the memory is allocated to the objects

3) Stack: Java Stack stores frames. It holds local variables and partial results, and plays a part in method invocation and return. Each thread has a private JVM stack, created at the same time as the thread. A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.

4) Program Counter Register: The PC (program counter) register contains the address of the Java virtual machine instruction currently being executed.

5) Native Method Stack: It contains all the native methods used in the application.

 

4. What is the platform?

A platform is the hardware or software environment in which a piece of software is executed. There are two types of platforms, software-based and hardware-based. Java provides a software-based platform.

 

5. What are the main features of Java?

1) Object-Oriented programming language

2) Simple

3) Distributed

4) Multithread

5) Platform Independent

6) Secured

7) Portable

8) Robust

9) Architectural Neutral

 

6. Is it possible to declare an Array without Array size?

It is not possible to declare an Array without Array size. If you try to do so then you will get a compile-time error.

 

7. What do you understand by the term Object-Oriented Programming language? 

The object-oriented programming language is a language that uses objects in programming.

 

8. What are the basic principles of the OOPs concept?

1) Objects

2) Classes

3) Abstraction

4) Polymorphism

5) Encapsulation

6) Inheritance

 

9. What do you mean by inheritance in java?

Inheritance is the main feature of java. Inheritance means a java class or interface can inherit the properties and behavior from another class or interface. Inheritance can be gained by implementing interfaces or extending classes.

 

10. What is the abstraction in java?

Abstraction means hiding detailed information from the user. Abstraction can be achieved by interfaces and abstract classes.

 

11. What are the main differences between the Java platform and other platforms?

There are the following differences between the Java platform and other platforms.

 

1) Java is the software-based platform whereas other platforms may be hardware platforms or software-based platforms.

2) Java is executed on top of other hardware platforms whereas other platforms can only have the hardware components.

 

12. What gives Java its write once and runs anywhere in nature?

The bytecode. Java compiler converts the Java programs into the class file (Byte Code) which is the intermediate language between source code and machine code. This bytecode is not platform-specific and can be executed on any computer.

 

13. What is a classloader?

Classloader is a subsystem of JVM which is used to load class files. Whenever we run the java program, it is loaded first by the classloader. There are three built-in classloaders in Java.

 

1) Bootstrap ClassLoader: This is the first classloader which is the superclass of the Extension classloader. It loads the rt.jar file which contains all class files of Java Standard Edition like java.lang package classes, java.net package classes, java.util package classes, java.io package classes, java.sql package classes, etc.

 

2) Extension ClassLoader: This is the child classloader of Bootstrap and parent classloader of System classloader. It loads the jar files located inside $JAVA_HOME/jre/lib/ext directory.

 

3) System/Application ClassLoader: This is the child classloader of the Extension classloader. It loads the class files from the classpath. By default, the classpath is set to the current directory. You can change the classpath using "-cp" or "-classpath" switch. It is also known as the Application class loader.

 

14. What if I write static public void instead of public static void?

The program compiles and runs correctly because the order of specifiers don't matter in Java.

 

15. What is the default value of the local variables?

The local variables are not initialized to any default value, neither primitives nor object references.

 

16. What are the various access specifiers in Java?

In Java, access specifiers are the keywords that are used to define the access scope of the method, class, or variable. In Java, there are four access specifiers given below.

 

1) public:  The classes, methods, or variables which are defined as public, can be accessed by any class or method.

2) protected: protected can be accessed by the class of the same package, or by the sub-class of this class, or within the same class.

3) default: default are accessible within the package only. By default, all the classes, methods, and variables are of default scope.

4) private: The private class, methods, or variables defined as private can be accessed within the class only.

 

17. What do you mean by the Object in java?

The object is the instance of a class. A class contains the basic properties and behavior of a real-world object. We can instantiate this class with a new keyword to make an object.

For example, Car.java has a class named Car. Car car= new Car() is an object or instance of this class.

 

18. What is a constructor?

Java provides the facility to declare a special member of the class with the same name, no return type, and with zero or many parameters is called a constructor.

 

19. What is polymorphism in java?

Poly means many and Morph means forms of a method or constructor or operator. So finally, Polymorphism means many forms of the same method or constructor, or operators. 

 

20. What do you mean by the method overloading?

The method with the same name but the number of arguments are different is called method overloading. Methods return type should be the same for all methods.

 

21. Does java support multiple inheritance?

Java classes do not support multiple inheritance but can gain multiple inheritance by using the interfaces.

 

22. What is the difference between constructor and method in java?

The constructor is the special member of the class with the same name as the class and no return type. But the method is the ordinary member of a class used to describe the behavior of some object or class.

 

23. Is it possible to overload the main() method?

Yes, the main method can be overloaded. But we should declare the original one like public static void main(String args[]){} because JVM will look for this when starting execution.

 

24. How many types of constructors are used in Java?

There are two types of contructor in java:

1) Default Constructor: default constructor is the one that does not accept any value. The default constructor is mainly used to initialize the instance variable with the default values. It can also be used for performing some useful tasks on object creation. A default constructor is invoked implicitly by the compiler if there is no constructor defined in the class.

 

2) Parameterized Constructor: The parameterized constructor is the one that can initialize the instance variables with the given values. In other words, we can say that the constructors which can accept the arguments are called parameterized constructors.

 

25. Does the constructor return any value?

yes, The constructor implicitly returns the current instance of the class

 

26. Can you make a constructor final?

No, the constructor can't be final.

 

27. What is the static variable?

The static variable is used to refer to the common property of all objects (that is not unique for each object), e.g., The company name of employees, college name of students, etc. The static variable gets memory only once in the class area at the time of class loading. Using a static variable makes your program more memory efficient (it saves memory). The static variable belongs to the class rather than the object.

 

28. What is the static method?

1) A static method belongs to the class rather than the object.

2) There is no need to create the object to call the static methods.

3) A static method can access and change the value of the static variable.

 

29. Why is the main method static?

Because the object is not required to call the static method. If we make the main method non-static, JVM will have to create its object first and then call main() method which will lead to the extra memory allocation.

 

30. What if the static modifier is removed from the signature of the main method?

Program compiles. However, at runtime, It throws an error "NoSuchMethodError."