It is far more vital to grasp the Java Core Fundamentals in order to succeed in any interview. This post discusses the most commonly asked and highly rated Core Java Interview Questions for both newcomers and seasoned experts.
1. Classes/Interfaces
– Collection of objects, methods, variable declarations, and definitions. Classes are the behavior of the program.
Interfaces :
- Interfaces can have methods and variables
- Methods should be abstract; only method declarations are allowed.
- Interfaces inform the class about what to do and don’t.
- Interfaces are the blueprints of classes.
What Is the Difference Between Serializable and Cloneable Interfaces in Java?
Serializable :
- Under the IO Package, Serializable Interface is located.
- This interface’s goal is to serialize the object, which involves saving the object’s state to a file. This object can be de-serialized whenever needed.
- It is the process of transforming an object into a serializable byte stream.
- This technique is platform-agnostic, which means we can deserialize the object on any platform once we’ve converted it to a stream.
- The writeObject() method in the ObjectOutputStream class writes the object to a file, whereas readObject() in the ObjectInputStream class reads the item from the file.
Cloneable Interface :
- This interface is part of java.lang package.
- The purpose of this Interface is to clone the current object
- The Class Object uses the clone() method to close the current object. The class must implement the Clonable interface. Otherwise, the runtime exception will be thrown as CloneNotSupportedException.
What is the transient Keyword?
This keyword is to make a variable transient, this means whichever variable holds this keyword, could not be serialized.
What are all the primitive data types?
- char
- int
- float
- boolean
- long
- double
- short
- byte
What are all the Non-Primitive Data Types?
- String
- Arrays
- Class
- Interface
What is the difference between primitive data types and non-primitive data types?
Primitive Data Type :
- The Primitive Data Type has a value.
- Primitive data types are always started with lower caps.
- It is predefined in Java.
- It does not support null values.
Non-Primitive Data Type :
- The non-primitive data type has an object, This can have methods and is used to invoke methods and other objects.
- Non-Primitive Data Types always start with a capital letter.
- It is not predefined; it is defined by the programmer.
- It can have null values.
What is the Marker Interface?
- It is a plain empty interface.
- It has no method to it
- The purpose of the marker interface is that the compiler should treat the class differently if the class implements the marker interface.
- Serializable, Clonable, and Remote are the marker interfaces in Java.
What is an assertion in Java?
In Java, the assertion is used as a pre-check parameter. To ensure that the variable has the desired property, we can assign the assertion to it. If the property fails to meet the expectations, an assertion exception is thrown.
When there is a logical situation that cannot be checked directly but must be checked to avoid a collision, the assertion is used.
Syntax :
assert expr1: expr2;
Example:
nt itemValue = 20;
assert itemValue >= 50: "Item Limit is not enough.";
The above statement throws an assertion error because the item value is less than 50.
Assertions are disabled by default in disabled mode. This has to be enabled by configuring at run time.
java –enableassertions <Program Name>
What is Singleton Design Pattern?
Reusability is the most common term for Singleton. It does not always create object references. It only builds the object the first time; the second time, it makes the same object as the first.
Multi-threaded applications, logging, and the cache section all use singleton objects.
- The following actions are required for Singleton Classes: Constructor must be configured as Private; instance method must be created using Single Object Creation Setup.
Example:
private SingletonObject() { }
static SingletonObject singletonObject = null;
//Instance Method
public static SingletonObject getInstance( ) {
if(singletonObject == null)
singletonObject = new SingletonObject();
return singletonObject;
}
Making the constructor private ensures that the function Object() { [native code] } is hidden from view and that the only method to build the object is to use getInstance(), which always returns existing objects. This pattern is known as “Singleton.”