Java is a popular programming language used to create a wide range of applications, from mobile apps to large-scale enterprise systems. It’s known for being easy to learn, platform-independent, and reliable. Java is used by many companies and developers because it’s versatile and has a large community for support.
It is an essential part of the Java programming language needed to create basic programs. It includes fundamental concepts like basic Java syntax, data types, loops, conditionals, and object-oriented programming concepts. It also covers important features like exception handling errors(exception handling), working with collections (lists, sets, maps), and managing multiple tasks simultaneously (multithreading). Understanding Core Java is crucial for building simple applications and is the foundation for learning more advanced Java technologies and frameworks.
This is a resource designed to help new graduates and individuals who are preparing for their Java programming job interviews. It typically includes a collection of commonly asked questions that cover fundamental concepts of Java programming.
Table of Contents
A job interview is like a first date. If you show up in a suit and tie, it’s like showing up in a wedding dressπ.
1. What is Java?
Java is a high-level object-oriented programming language, it is famous for its different features like reliability, reusability, security, and platform independence (ability to work across various platforms). To make it famous java has the OOPS concept, Exception handling, multithreading, collection framework, and all.
The main thing is to mention those things whatever you know properly. As you mentioned “OOPS concept, Exception handling, multithreading, collection framework” in the previous question then the interviewer will go forward as per your mentioned questions like oh ok he/she knows these things, let’s ask questions on the mentioned topics. Or else If you do not mention the famous features of Java then the next question would be like,
2. What are the main features of Java?
Never be a storyteller, always make your answers straight and simple in the interview, until and unless they will not ask you questions with “explain” words. So the answers will be like,
It is a platform Independent, Object-Oriented, simple, familiar, robust, secure, multi-threaded, and vast library of classes and methods, it has a large and active community of developers.
3. What is the difference between JDK, JRE, and JVM?
This question is used to examine whether you, at least have an idea about the internal workings, and memory management of Java or not. So your answer will be like,
JVM is a Java virtual machine that runs Java bytecode on a specific platform which makes the Java platform independent. JRE is the Java runtime environment that includes JVM and libraries needed to run Java applications. JDK is a Java development kit that includes JRE, development tools, and libraries for developing Java applications.
JDK contains JRE and JRE contains JVM.
4. What is the JIT compiler?
JIT means Just-In-Time compiler, which is a component of a runtime environment that improves performance by compiling bytecode into native code or machine-readable code at runtime, allowing programs to run faster by optimizing code execution dynamically.
4. What is OOPS in Java?
OOPS stands for object-oriented programming structure. It is a main feature of Java which says that everything revolves around Java. It has different functionalities that make it famous such as class, object, Inheritance, polymorphism, encapsulation, and abstraction.
As you mentioned some topics here so, the next question would be,
6. What is Inheritance?
Never go for a direct definition, first understand the things then create your definition with a layman’s language, this will create a separate impression for you. So the answer would be like,
When inheritance is the topic then code reusability will be the attraction point. It is based on the parent-child relationship where we can not only access the parent class but also we can have access the parent class things.
If the interviewer asks you for an example then don’t go for those cars, bus, train examples. Try to give your example of whatever you understood from inheritance. So you may give this,
Recently iPhone 15 was launched, but the Apple company did not build the iPhone 15 from scratch. They used all the functionalities of iPhone 14(parent class) along with they had modified some existing features(child class can access the parent class functionalities) and they had added some new features(child class functionalities). And at last, introduced it as a brand new iPhone15.
7. What is polymorphism?
The question defines everything, poly means many and morphism means forms. When one object can play a different role in the same or different situation then that is known as polymorphism. It is two types, method overloading and method overriding.
Method overloading is also known as compile-time polymorphism or static binding.
Method overriding is also known as run-time polymorphism or dynamic binding.
8. What is Encapsulation?
We need encapsulation to enhance the security, by adding data members (variables) and member functions(getters and setters) in a single unit. Our every Java POJO class is encapsulated classes.
Example:
class encpasulationDemo{
// data members(variables)
private int rollNo;
private String branch;
private String name;
// member functions(setters and getters)
public void setRollNo(int rollNo){
this.rollNo = rollNo;
}
public int getRollNo(){
this.rollNo;
}
public void setBranch(String branch){
this.branch = branch;
}
public String getBranch(){
this.branch;
}
public void setName(String name){
this.name = name;
}
public String getName(){
this.name;
}
}
9. What is abstraction?
Abstraction is another way of representing security in Java. It is the concept of hiding complex implementation details and showing only the required features of an object. We would achieve it with the help of inheritance.
10. What is an interface in Java?
An interface is a reference type that defines a set of abstract methods that a class must implement. It allows different classes to be interchanged through a common interface without knowing their specific implementations.
So we can say that interface is another way of achieving abstraction in Java. Most importantly Java does not support multiple inheritance at the class level but with the help of interface, we can achieve multiple inheritance in Java.
11. What is the difference between an interface and an abstract class?
- Through the interface, we can achieve multiple inheritance but with the help of abstract class, we can’t achieve multiple inheritance in Java.
- In the case of the access specifier, in the interface, we can only use, public, static, and final but in the abstract class we can have instance variables and constants with different access levels as private, protected, and public.
- Interface can’t have a constructor but abstract class has. If we are not providing any constructor to an abstract class then JVM internally provides a default constructor.
- One more point that you may include or not like,
The abstract class has both abstract and concrete methods but the interface has only abstract methods while after Java 8 it also supports default and static methods.
12. What is static in Java?
First of all, static is a keyword in Java and we can use it with inner classes, variables, blocks, and methods. When static comes with variables then it acts as a global variable in Java, when it comes with a block then it executes before the main method, when it comes with method then it will break the overriding concept because it is resolved at compile time but overriding concept based on runtime. It also comes with an inner class then it will have access to all private data members of the outer class directly.
13. What is final in Java?
Final is a keyword in Java, it comes with classes, methods, and variables. When the final is a class then it stops inheriting a class. When the final is a method then we will not override that method but we can overhide that method. When the final is a variable then it acts as a constant in Java, which means once we assign the variable value then later we can’t change the value.
14. What is System.out.println();
It is used to print something with a line break, where the system is a predefined class in the lang package, out is constant in the system class, and println() is the overloaded method of the PrintStream class.
15. What are the access specifiers in Java?
There are 4 types of access specifiers in Java,
- Public: It is a global level access, we can access anywhere with the help of this.
- Private: It restricts the access limit within the class only.
- Protected: It restricts access within the package only.
- Default: It is also the same as protected but the only difference is in the case of protected, it is accessible if the subclass is in another class but it is not accessible in case of default.
16. What is public static void main(String args[])?
It is a method known as the main() method in Java, where the execution starts.
- Public is the access specifier which allows it to access globally.
- Static is used because no need to create the object of the class to access the method, we can access it directly by method name.
- Void means the method will only execute the things but will not return anything(no return type).
- Main is the method name which is predefined. You may have a question like will I make AnanyaPandy, ViratKohli, and EmmaWatson the method name? But sorry to say that James Gosling only knows the main() as a method name so those beautiful names will not execute. But if you want to try with a different name then no issues it’s your laptop your code, you can write but keep in mind at the end you have to face, “Main method not found error”.
- String args[] means the main method accepts an array of string types as an argument.
17. Why Java is not fully object-oriented?
Because of primitive data types, Java is not fully object-oriented.
18. What is wrapper class?
As we know primitive data types are not objects so to overcome that Java introduced the wrapper class that converts primitive data types to object types.
Primitive datatypes are, boolean, char, int, float, long, and double.
Wrapper classes are, Boolean, Character, Integer, Float, Long, and Double.
19. What is String in Java?
String is a predefined class in Java or we can say that a collection of characters are known as String. It has various methods which make it unique among along data types.
20. What is a String Literal Pool or String Constant Pool?
We can create a string in two ways, one is with the new keyword which will store the data in the heap memory area and another one is directly assigned the value which will be stored in the string literal pool. The advantage of a string literal pool is memory management, if a value is already present in the memory and we are again trying to create the same value with a different string then it will not create a separate memory space for this, instead, it will simply refer to the already existing one.
21. What is ‘this’ in Java?
- This is a keyword in Java which used to refer to the current class instance variable.
- It can be used to differentiate between instance variables and local variables with the same name.
- this() can call the current class constructor.
- this() can be used to call methods from the current class.
22. What is ‘super’ in Java?
- Super is a keyword in Java which used to refer to the current base class instance variable.
- super() can call the current base class constructor.
- super() can be used to call current base class methods.
23. What is Enum in Java?
When our requirement is like a group of constants whose value will never change we will go for an enum, because each variable in the enum class is by default public
, static
, and final
.
Example,
public enum TeamIndia {
ROHIT_SHARMA;
SIKHAR_DHAWAN;
VIRAT_KOHLI;
KL_RAHUL;
MS_DHONI_WK_C;
HARDIK_PANDYA;
RAVINDRA_JADEJA;
AXAR_PATEL;
KULDEEP_YADAV;
MOHOMMED_SHAMI;
JASPRIT_BUMRAH;
}
24. What is an Object class and what are the methods present in it?
The object is the base class of every predefined and user-defined class in Java. If a class does not extend any class then it is a direct child class of the object class or if a class extends any class then it will be the indirect child class of the object class. The methods present in the object class are,
- toString()
- equals()
- hashcode()
- getClass()
- finalize()
- clone()
- wait()
- notify()
- notifyAll()