Hi friends,
In this post, I am sharing interview questions and answers on Singleton, Serialization and enum.
Question 1:
Which classes are candidates of Singleton?
Answer:
Any class which we want to be available to the whole application and only one instance should be available is a candidate for singleton.
e.g.
Popup utility class in GUI application. If we want to show popup with message , we can have only one instance of this class and we just call show() method with message.
Question 2:
What is lazy and early loading in Singleton?
Answer:
When we create singleton instance using static final at the time of class loading, it is called early loading. And when singleton instance is created when the client requests , it is called lazy loading.
Question 3:
Give any Singleton example from Java development kit?
Answer:
- java.lang.Runtime
- java.awt.Desktop
Question 4:
How to customize java serialization of an object?
Answer:
We can use readObject(ObjectInputStream ois) and writeObject(ObjectOutputStream oos) methods of java.io.ObjectInputStream and java.io.ObjectOutputStream classes.
readObject(ObjectInputStream) throws ClassNotFoundException and IOException.
writeObject(ObjectOutputStream) throws IOException.
We can use writeUTF(), writeInt(), writeLong() and readUTF(), readInt() and readLong() methods defined in ObjectOutputStream and ObjectInputSteam classes.
In this, next question becomes:
Why readObject(ObjectInputStream ois) throws ClassNotFoundException?
Answer:
Because readObject(ObjectInputStream) is used on deserialization and there may be the case that class is not found. e.g. On client side, we have a class Message in client package and on server side , we have same class in server package, then while deserializing, class will not be found because package client is not found.
But, it is not the case with writeObject(ObjectOutputStream). That is why this method does not throw ClassNotFoundException.
Question 5:
We use serialVersionUID in serialized classes. Why this ID is made static?
Answer:
During deserialization, it is checked that whether both sender and receiver of serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of senders class, then InvalidClassException is thrown. That check is made before creating the object and by comparing serialVersionUID. As no object is present at that time, that is why it is static.
Code Scenario:
public class Account implements Serializable{
String
strUserName = "Anish";
transient
String pwd = "abcd";
transient String onlyTest = "test";
private void writeObject(ObjectOutputStream oos)
throws Exception{
oos.defaultWriteObject();
String
epwd = "12345"+ pwd;
String
etest = "54321" + onlyTest;
oos.writeUTF(epwd);
oos.writeUTF(etest);
}
private void readObject(ObjectInputStream ois) throws Exception{
ois.defaultReadObject();
Account acc = null;
String
epwd = (String) ois.readUTF();
String
etest = (String) ois.readUTF();
pwd
= epwd;
onlyTest = etest;
}
}
public static void main(String[] args){
Account account = new Account();
try
{
FileOutputStreamfos
= new FileOutputStream("C:\\Users\\Lenovo\\TestFiles\\a.txt");
ObjectOutputStream oos
= new ObjectOutputStream(fos);
oos.writeObject(account);
FileInputStream fis
= new FileInputStream("C:\\Users\\Lenovo\\TestFiles\\a.txt");
ObjectInputStream ois
= new ObjectInputStream(fis);
Account account1 = (Account)ois.readObject();
oos.close();
fos.close();
ois.close();
fis.close();
}
catch(Exception
e) {
}
}
Above example code is for customized serialization and deserialization.
Customized serialization is required when we have to send some secure information over the network in encrypted form. So,in this example we are sending transient pwd in serialized form. And then deserializing it.
Points to note down:
1.
We have to
write readObject() and writeObject() methods.
2.
Whatever ,
we need to make secure , we need to write it in ObjectOutputStream
specifically. Other variables will be written by a call to
defaultWriteObject(). Same thing applies to when reading object using
readObject() method. If we don’t call defaultWriteObject() , then other
variables will not be written.
3.
Order of
writing and reading the variables must be same.
Question 6:
Is it mandatory to declare enum constants with UPPERCASE letters?
Answer:
No,but using uppercase is a good coding practice.
Question 7:
Can enum types have fields, method and constructors?
Answer:
Yes.
Example:
enum EnumClass{
A, B, C; // Enum contants
int i; // Enum can have fields
private EnumClass(){
// Enums can have constructors
}
void method(){
//Enum can have methods
}
}
Question 8:
What is the access type of enum constructor?
Answer:
Enum constructors are private by default. Only private constructors are allowed in enum types. That's why we cannot instantiate enum types using new operator.
Question 9:
How enum constants work?
Answer:
Enum constants are created only once for the whole execution. All enum constants are created when you refer any enum constant first time in your code. While creating each enum constant, corresponding constructor is called.
enum Enums
{
A, B(10), C("ccc", 20);
//No-arg
private constructor
private Enums()
{
System.out.println(1);
}
//Private
constructor taking one argument
private Enums(inti)
{
System.out.println(2);
}
//Private constructor taking two arguments
private Enums(String
s, intj)
{
System.out.println(3);
}
}
public class MainClass
{
public static void main(String[]
args)
{
Enums en
= Enums.B; //All enum constants are created while executing this
statement.
//While
creating each enum constant, corresponding constructor is called
Enums en2 = Enums.C; //No enum constant is created here.
Enums
en3 = Enums.A; //No enum constant is created here.
}
}
Question 10:
Can we declare enum constants anywhere?
Answer:
No. These must be declared first ahead of fields, constructors and methods(if any).
enum Enums{
int i;
A, B, C; // Compile time error, enum constants must be declared first.
}
That''s all for this post.
Thanks for reading!!
No comments:
Post a Comment