In this blog, I am going to list most of the vital core java interview questions. The core java interview questions are categorized in the Basics of features, oops, string, Collections, Exceptions, etc.
Interview Questions and Answer
1). What is Java?- is a general-purpose, object-oriented programming language developed by Sun Microsystems of the USA in 1991.
- was designed for the development of software consumer electronic devices like TVs, VCRs, and other electronic machines.
- is a high-level programming language and is platform-independent
- Compiled and Interpreted
- Platform Independent and portable
- Object-oriented
- Robust and Secure
- Multithreaded and Interactive
- High Performance
- Yes, JAVA is an Object-Oriented based Programming language
- OOPs concepts are given below
- Object
- It is a basic unit of Object-Oriented Programming and represents real-life entities like a pen, fruit, table, computer, etc.
- For Instance, Graphics Designer may have objects such as 'Circle', 'Menu', etc.
- Another Instance, While doing shopping on an e-commerce website, might have some objects such as 'Cart', 'Product', 'Wallet', etc.
- Class
- A Class is a user-defined layout from which objects are created.
- Set of Properties or methods.
- Inheritance
- is the mechanism where everything is an object that interacts with one another.
- It helps to reuse the code and establish a relationship between different classes.
- Polymorphism
- It means many forms.
- 'Poly' = many and 'morphs' = forms.
- Two Types: Compile-time and Runtime
- Abstraction
- is a process of hiding actual implementation details and a user can access functionality only.
- For Instance, a man is driving a car. The man has known only the functionality of the car. So He is pushing the command only like increase the speed, apply the brake, etc. He really doesn't know about the inner mechanism of the car or the implementation of accelerators, brakes, etc in the car.
- Encapsulation
- is a process of wrapping code and data in a single unit.
- For Instance, a capsule is mixed with medicines.
- Java Bean class is the best instance of fully encapsulated.
| Java | C |
|---|---|
| Doesn't include a unique statement | It used a unique statement like sizeof, typeof |
| Doesn't contain data type like struct, union | It used struct, union |
| Doesn't define the type modifiers keywords auto, extern, register, signed, and unsigned | we can define the type modifiers keywords auto, extern, register, signed, and unsigned |
| Doesn't support an explicit pointer type | It does support. |
| Doesn't have a preprocessor like '#define', '#include' etc. | It requires to use of a preprocessor before compiling the program. |
5). Difference between Java and C++
| Comparison | C++ | Java |
|---|---|---|
| Platform-independent | No | Yes |
| Inheritance | Yes | Yes but Java doesn't support multiple inheritances. This is accomplished using a new feature called 'interface' |
| Thread Support | No, but it relies on third-party libraries | Yes |
| Global Variables | Yes | No |
| Pointers | Yes | No |
6). Difference Between Java and Python
| Comparison | JAVA | Python |
|---|---|---|
| Interpreted Language Support | Yes, It is a complied + interpreted language. | It is an interpreted language. |
| Code | It has long lines of code. | It has fewer lines of code |
| Inheritance | Yes but It doesn't support multiple Inheritance | Yes, Pythons supports both single and multiple Inheritance |
| Portable | It can run on any devices which can run with JVM | It needs an interpreter installed on the target machine to translate code. So Python is less portable. |
7). Write a 'Hello, World!' example in Java and explain it.
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
- public:
- The Keyword public is an access modifier that declares the main method as unprotected and therefore making it accessible to all other classes.
- static:
- It is a keyword in java that identifies it as class-based.
- main() is made static in Java so that it can be accessed without creating the instance of a Class.
- In case, main is not made static then the compiler will throw an error as main() is called by the JVM before any objects are made and only static methods can be directly invoked via the class.
- void:
- It is the return type of the method.
- Void is a keyword and defines the method which will not return any value.
- main:
- It is the name of the method which is searched by JVM as a starting point for an application with a particular signature only. It is the method where the main execution occurs.
- String args[]: It is the parameter passed to the main method.
- System.out.println('Hello, World!')
- It will print 'Hello, World!' to the console.
- 'System' is a class.
- 'out' is an object of PrintStream class.
- 'println()' is method of PrintSteam class.
8). Explain about 'final' keyword
- It is the keyword in java and is used as a non-access modifier.
- It can apply to a variable, method & class.
- Final Variable: Its value can be changed once assigned.
- Final Method: When a method is declared final then it can't be overridden by the inheriting class.
- Final Class: It can't be extended by another class.
9). Types of Constructors
- Three types of constructors
- Default
- No-args
- Parameterized
- any contractor can't be final.
- Default
- If you don't implement any constructor in your class, java compile inserts a default constructor.
- This constructor is known as the default constructor.
- It will be added during compile-time and exists in the .class file
- No-Args
- A constructor with no arguments is known as the No-args Constructor.
- The signature is the same as the default constructor but the body can have some code implementation, unlike the default Constructor where the body is empty.
- Parameterized Constructor
- Constructor with arguments is known as Parameterized Constructor.
- It is used to assign a user-wanted specific value to the instance variables of different objects.
- It is written explicitly by the developer.
10). Is Java a pure Object-oriented based programming language?
- No, Java isn't a pure Object-oriented based programming language because Java has predefined primitive data types such as boolean, byte, char, int, float, double, long, short which aren't objects.
- For primitive data types, Java provides Wrapper classes to convert into objects.
11). What are wrapper classes in Java?
- Wrapper classes convert the Java primitive data types into reference objects.
- Every Primitive data type has a class dedicated to it like 'boolean'-> Boolean class, 'int' -> Integer class, etc.
- String class is immutable in JAVA.So it's suitable to use in a multi-threaded environment.
- It represents a sequence of characters in UTF-16 format.
- When we initialize a new String, JVM looks for the string with the value in the JVM String Pool. If it finds a value, then it will return the reference otherwise will create a string object and place the object in the String pool.
13). Difference between String, String Buffer, String Builder
| Comparison | String | String Builder | String Buffer |
|---|---|---|---|
| Mutability | immutable | Mutable | Mutable |
| Thread-Safe | Yes | No | Yes |
| Performance | Fast | More Efficient | Less Efficient |
14). Access modifiers in Java
- Four access modifiers in Java
- private
- It is specified by using the private keyword.
- The methods or data members declared as private are accessible only within the class in which they are declared.
- default
- When no access modifier is specified for a class, a variable, a method - It is said to be having the default access modifier by default.
- It is accessible only within the same package.
- protected
- It is specified by using the protected keyword
- The methods or data members declared as private are accessible only within the same packages or subclasses in different packages.
- public
- It is specified by using the public keyword.
- Class, methods, data members as the public are accessible from everywhere in the program.
| Comparison | Default | Private | Protected | Public |
|---|---|---|---|---|
| Same Class | Yes | Yes | Yes | Yes |
| Same Package sub-class | Yes | No | Yes | Yes |
| Same Package non-sub-class | Yes | No | Yes | Yes |
| Different Package sub-class | No | No | Yes | Yes |
| Different Package non-sub-class | No | No | Yes | Yes |
15). What is Polymorphism? and explain the types of Polymorphism.
- It means many forms.
- 'Poly' = many and 'morphs' = forms.
- Two Types
- Compile-time Polymorphism
- Runtime Polymorphism
- Compile-time polymorphism is method overloading whereas Runtime Polymorphism is method overriding.
16). What do you mean by Compile-time Polymorphism?
- It is also known as static binding.
- It is a method of overloading where the same name but a different signature.
- We can implement two ways
- By changing the numbers of arguments
- By changing the return type.
17). What do you mean by Runtime Polymorphism?
- It is also known as Dynamic Method dispatch.
- It is method overriding which is implemented by using Interface and Inheritance.
- In this process, an overridden method is called through the reference variable of the superclass.
- The method must have the same name & same parameter list as in the parent class.
- There must be an IS-A relationship (inheritance).
18). What do you mean by Abstraction?
- It is a process of hiding the implementation details from the user and revealing only the functionality to them.
- 'abstract' is a keyword.
- A class that is declared as abstract is known as an abstract class.
- It can have abstract and non-abstract methods.
- It can have a constructor and static- method also.
- An interface can have methods and variables.
- "interface" is a keyword.
- An interface doesn't contain any constructor.
- All of the methods in an interface are abstract.
- An Interface can extend multiple interfaces.
- An Interface can not be extended by class, it is implemented by a class only.
20). What is the difference between abstract and interface?
| Abstract Class | Interface |
|---|---|
| An abstract class can have final, non-final, static, non-static variables. | The interface has only static and final variables. |
| Abstract class doesn't support multiple inheritances. | The Interface support multiple inheritances. |
| An abstract class can provide the implementation of the interface. | The interface can not provide the implementation of the abstract class. |
| An abstract class can extend another class and implement multiple JAVA interfaces. | An Interface can extend another JAVA interface only. |
| An abstract class can be extended using the keyword "extends". | An interface can be implemented using the keyword "implements". |
21). What is inheritance?
- It is a process to inherit the properties (Data member) and functionalities(methods) from one class to another class.
- It allows us to reuse a code, it improves reusability in your application.
- Parent Class:
- The class whose properties and functionalities are used by another class is known as a parent class, superclass, or base class.
- Child Class:
- The class that extends the features of another class is known as child class, subclass, or derived class.
- extends is the keyword used to inherit the properties of a class.
- If you don't want other classes to inherit from a class, use the
finalkeyword
22). What are Method Overloading and Method Overriding?
- Method overloading
- It allows different methods to have the same name but different signatures where signatures can differ by the number of input parameters or type of input parameters or both.
- It can be done by changing the number of arguments or by changing the data type of arguments.
- In addition, If two or more methods have the same name and the same parameter list but differ in the return type can not be overloaded.
- It is a compile-time polymorphism.
- Method overriding
- It is a process of overriding the base class method by a derived class with a more specific definition.
- It is performed between two classes inheritance relation.
- In this case, the methods of both classes must have the same name and same parameters.
- It is runtime polymorphism.
- private, static, and final methods can not be overridden.
23). What is the difference between Heap and Stack Memory in Java?
| Features | Stack | Heap |
|---|---|---|
| Application | It stores items that have a short life such as methods, variables, and reference variables of the objects. | It stores objects and Java Runtime Environment(JRE) classes. |
| Ordering | It follows the LIFO order. | It does not follow any order because it is a dynamic memory allocation and does not have any fixed pattern for allocation and deallocation of memory blocks. |
| Efficiency | It has faster access, allocation, and deallocation. | It has slower access, allocation, and deallocation. |
| Java Options Used | We can increase the stack size by using the JVM option -Xss. | We can increase or decrease the heap memory size by using the -Xmx and -Xms JVM options. |
| Exception Throws | JVM throws the java. lang.StackOverFlowError if the stack size is greater than the limit. To avoid this error, increase the stack size. | JVM throws the java. lang.OutOfMemoryError if the JVM is unable to create a new native method. |