Java’s popularity in application development fuels high demand for skilled professionals. IT companies actively seek qualified Java developers, creating a competitive job market. While Core Java knowledge is essential, strong interview preparation is crucial to standing out.
Every candidate goes through the interview process, so being well-prepared is key. The best way to achieve this is by anticipating potential Core Java interview questions and practicing your responses.
Finding these questions can be challenging, especially for new graduates. Here’s where we can help! We’ve compiled a list of commonly asked Core Java interview questions to get you started.
Contents
Also check- Jquery interview questions / Java 8 interview questions
Core java interview questions
Q1.What is Java?
Ans-
Java is the high-level, object-oriented, robust, secure programming language, platform-independent, high performance, Multithreaded, and portable programming language. It was developed by James Gosling in June 1991. It can also be known as the platform as it provides its own JRE and API.Q2.What do you understand by Java virtual machine?
Ans-
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 which must be implemented in the computer system. The Java code is compiled by JVM to be a Bytecode which is machine independent and close to the native code.Q3.What is the difference between JDK, JRE, and JVM?
Ans-
JVM:
JVM is an acronym for Java Virtual Machine; it is an abstract machine which provides the runtime environment in which Java bytecode can be executed. It is a specification which 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 which 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 which 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 which 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:
–
Standard Edition Java Platform
Enterprise Edition Java Platform
Micro Edition Java PlatformQ4.What is JIT compiler?
Ans-
Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles parts of the bytecode that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.Q5.What are the main differences between the Java platform and other platforms?
Ans-
There are the following differences between the Java platform and other platforms.
–
Java is the software-based platform whereas other platforms may be the hardware platforms or software-based platforms.
Java is executed on the top of other hardware platforms whereas other platforms can only have the hardware components.Q6.What gives Java its ‘write once and run anywhere’ nature?
Ans-
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.Q7.What is classloader?
Ans-
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.
–
Bootstrap ClassLoader: This is the first classloader which is the superclass of 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.
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.
System/Application ClassLoader: This is the child classloader of 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 Application classloader.Q8.What is the platform?
Ans-
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 the software-based platform.Q9.Is delete, next, main, exit or null keyword in java?
Ans-
No.Q10.If I don’t provide any arguments on the command line, then what will the value stored in the String array passed into the main() method, empty or NULL?
Ans-
It is empty, but not null.Q11.What if I write static public void instead of public static void?
Ans-
The program compiles and runs correctly because the order of specifiers doesn’t matter in Java.Q12.What is the default value of the local variables?
Ans-
The local variables are not initialized to any default value, neither primitives nor object references.Q13.What are the various access specifiers in Java?
Ans-
In Java, access specifiers are the keywords which are used to define the access scope of the method, class, or a variable. In Java, there are four access specifiers given below.
–
Public The classes, methods, or variables which are defined as public, can be accessed by any class or method.
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.
Default Default are accessible within the package only. By default, all the classes, methods, and variables are of default scope.
Private The private class, methods, or variables defined as private can be accessed within the class only.Q14.What is the purpose of static methods and variables?
Ans-
The methods or variables defined as static are shared among all the objects of the class. The static is the part of the class and not of the object. The static variables are stored in the class area, and we do not need to create the object to access such variables. Therefore, static is used in the case, where we need to define variables or methods which are common to all the objects of the class.
–
For example, In the class simulating the collection of the students in a college, the name of the college is the common attribute to all the students. Therefore, the college name will be defined as static.Q15.What is object-oriented paradigm?
Ans-
It is a programming paradigm based on objects having data and methods defined in the class to which it belongs. Object-oriented paradigm aims to incorporate the advantages of modularity and reusability. Objects are the instances of classes which interacts with one another to design applications and programs. There are the following features of the object-oriented paradigm.
–
Follows the bottom-up approach in program design.
Focus on data with methods to operate upon the object’s data
Includes the concept like Encapsulation and abstraction which hides the complexities from the user and show only functionality.
Implements the real-time approach like inheritance, abstraction, etc.
The examples of the object-oriented paradigm are C++, Simula, Smalltalk, Python, C#, etc.Q16.What is an object?
Ans-
The Object is the real-time entity having some state and behavior. In Java, Object is an instance of the class having the instance variables as the state of the object and the methods as the behavior of the object. The object of a class can be created by using the new keyword.Q17.What is the difference between an object-oriented programming language and object-based programming language?
Ans-
There are the following basic differences between the object-oriented language and object-based language.
–
Object-oriented languages follow all the concepts of OOPs whereas, the object-based language doesn’t follow all the concepts of OOPs like inheritance and polymorphism.
Object-oriented languages do not have the inbuilt objects whereas Object-based languages have the inbuilt objects, for example, JavaScript has window object.
Examples of object-oriented programming are Java, C#, Smalltalk, etc. whereas the examples of object-based languages are JavaScript, VBScript, etc.Q18.What will be the initial value of an object reference which is defined as an instance variable?
Ans-
All object references are initialized to null in Java.Q19.What is the constructor?
Ans-
The constructor can be defined as the special type of method that is used to initialize the state of an object. It is invoked when the class is instantiated, and the memory is allocated for the object. Every time, an object is created using the new keyword, the default constructor of the class is called. The name of the constructor must be similar to the class name. The constructor must not have an explicit return type.Q20.Does constructor return any value?
Ans-
Ans: yes, The constructor implicitly returns the current instance of the class (You can’t use an explicit return type with the constructor).Related: Chime Software Engineer Interview / Salesforce Technical Architect Interview
Advanced core java interview questions
Q21.Is constructor inherited?
Ans-
No, The constructor is not inherited.Q22.Can you make a constructor final?
Ans-
No, the constructor can’t be final.Q23.What are the advantages of Packages in Java?
Ans-
There are various advantages of defining packages in Java.
–
Packages avoid the name clashes.
The Package provides easier access control.
We can also have the hidden classes that are not visible outside and used by the package.
It is easier to locate the related classes.Q24.Can we make constructors static?
Ans-
As we know that the static context (method, block, or variable) belongs to the class, not the object. Since Constructors are invoked only when the object is created, there is no sense to make the constructors static. However, if you try to do so, the compiler will show the compiler error.Q25.Can we make the abstract methods static in Java?
Ans-
In Java, if we make the abstract methods static, It will become the part of the class, and we can directly call it which is unnecessary. Calling an undefined method is completely useless therefore it is not allowed.Q26.Why is the main method static?
Ans-
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.Q27.Can we override the static methods?
Ans-
No, we can’t override static methods.Q28.Can we declare a constructor as final?
Ans-
The constructor can never be declared as final because it is never inherited. Constructors are not ordinary methods; therefore, there is no sense to declare constructors as final. However, if you try to do so, The compiler will throw an error.Q29.Can we declare an interface as final?
Ans-
No, we cannot declare an interface as final because the interface must be implemented by some class to provide its definition. Therefore, there is no sense to make an interface final. However, if you try to do so, the compiler will show an error.Q30.Can there be an abstract method without an abstract class?
Ans-
No, if there is an abstract method in a class, that class must be abstract.Related: Waymo Data Scientist Interview / Massage Envy Interview
Core java interview questions for freshers
Q31.Can you use abstract and final both with a method?
Ans-
No, because we need to override the abstract method to provide its implementation, whereas we can’t override the final method.Q32.Is it possible to instantiate the abstract class?
Ans-
No, the abstract class can never be instantiated even if it contains a constructor and all of its methods are implemented.Q33.What is the interface?
Ans-
The interface is a blueprint for a class that has static constants and abstract methods. It can be used to achieve full abstraction and multiple inheritance. It is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java. In other words, you can say that interfaces can have abstract methods and variables. Java Interface also represents the IS-A relationship. It cannot be instantiated just like the abstract class. However, we need to implement it to define its methods. Since Java 8, we can have the default, static, and private methods in an interface.Q34.Can you declare an interface method static?
Ans-
No, because methods of an interface are abstract by default, and we can not use static and abstract together.Q35.Can the Interface be final?
Ans-
No, because an interface needs to be implemented by the other class and if it is final, it can’t be implemented by any class.Q36.Can we define private and protected modifiers for the members in interfaces?
Ans-
No, they are implicitly public.Q37.When can an object reference be cast to an interface reference?
Ans-
An object reference can be cast to an interface reference when the object implements the referenced interface.Q38.What is the difference between the final method and abstract method?
Ans-
The main difference between the final method and abstract method is that the abstract method cannot be final as we need to override them in the subclass to give its definition.Q39.How can we access some class in another class in Java?
Ans-
There are two ways to access a class in another class.
–
By using the fully qualified name: To access a class in a different package, either we must use the fully qualified name of that class, or we must import the package containing that class.
By using the relative path, We can use the path of the class that is related to the package that contains our class. It can be the same or subpackage.Q40.Do I need to import java.lang package any time? Why?
Ans-
No. It is by default loaded internally by the JVM.
Core java interview questions and answers for experienced
Q41.Can I import same package/class twice? Will the JVM load the package twice at runtime?
Ans-
One can import the same package or the same class multiple times. Neither compiler nor JVM complains about it. However, the JVM will internally load the class only once no matter how many times you import the same class.Q42.What is the static import?
Ans-
By static import, we can access the static members of a class directly, and there is no to qualify it with the class name.Q43.What is the purpose of using javap?
Ans-
The javap command disassembles a class file. The javap command displays information about the fields, constructors and methods present in a class file.
Syntax
javap fully_class_nameQ44.Can you access the private method from outside the class?
Ans-
Yes, by changing the runtime behavior of a class if the class is not secured.Q45.Can an exception be rethrown?
Ans-
Yes.Q46.Can subclass overriding method declare an exception if parent class method doesn’t throw an exception?
Ans-
Yes but only unchecked exception not checked.Q47.How the metacharacters are different from the ordinary characters?
Ans-
Metacharacters have the special meaning to the regular expression engine. The metacharacters are ^, $, ., *, +, etc. The regular expression engine does not consider them as the regular characters. To enable the regular expression engine treating the metacharacters as ordinary characters, we need to escape the metacharacters with the backslash.Q48.Write a regular expression to validate a password. A password must start with an alphabet and followed by alphanumeric characters; Its length must be in between 8 to 20.
Ans-
The regular expression for the above criteria will be: ^[a-zA-Z][a-zA-Z0-9]{8,19} where ^ represents the start of the regex, [a-zA-Z] represents that the first character must be an alphabet, [a-zA-Z0-9] represents the alphanumeric character, {8,19} represents that the length of the password must be in between 8 and 20.Q49.What are the advantages of defining packages in Java?
Ans-
By defining packages, we can avoid the name conflicts between the same class names defined in different packages. Packages also enable the developer to organize the similar classes more effectively. For example, one can clearly understand that the classes present in java.io package are used to perform io related operations.Q50.What is the purpose of the strictfp keyword?
Ans-
Java strictfp keyword ensures that you will get the same result on every platform if you perform operations in the floating-point variable. The precision may differ from platform to platform that is why java programming language has provided the strictfp keyword so that you get the same result on every platform. So, now you have better control over the floating-point arithmetic.
Conclusion –
This list compiles some of the most frequently encountered Core Java interview questions. We’ve curated these questions with the help of industry professionals dedicated to supporting both recent graduates and experienced developers.
While these questions may not represent the entirety of a Core Java interview, they provide a solid foundation for your preparation. By familiarizing yourself with these common topics, you’ll be well-equipped to showcase your Java skills and increase your confidence in the interview setting.