Page 1
Java Latest Questions and Correct
Answers
If you overwrite clone(), which 3 rules must this method
obey?
Ans: 1) the new object should be new: memory address
should differ
2) Both should be an object of the same class
3) Both should be in the same state:
a.clone().equals(a) == true
Why do we need to implement the Clonable interface?
Ans: At runtime it would throw the
CloneNotSupportedException if we don't implement the
Cloneable interface. A class implements the Cloneable
interface to indicate to the Object.clone() method that
it is legal for that method to make a field-for-field copy
of instances of that class.
What is a marker interface?
Ans: A Marker interface, has no method.
Serializable, Clonable are marker interfaces.
How do you clone an object?
Ans: MyClone a = (MyClone) c.clone()
, Page 2
- TypeCast is nessesary.
- handle CloneNotSupportedException
Why can Annotations replace marker interfaces?
Ans: Annotations can convey metadata about the class
to its consumers without creating a separate type for it.
Annotations let you pass information to classes that
"consume" it.
What is Double-Checked Locking?
Ans: To reduce the overhead of acquiring a lock by first
testing the locking criterion without actually acquiring
the lock.
How do you make an object serializable?
Ans: The class must implement the java.io.Serializable
interface
Java object serialization is performed using which classes?
Ans: Java object serialization (writing) is done with the
ObjectOutputStream and deserialization (reading) is
done with the ObjectInputStream.
What is serialVersionUID?
, Page 3
Ans: In addition to implementing the Serializable
interface, a class intended for serialization should also
contain a private static final long variable named
serialVersionUID.
The serialVersionUID variable is used by Java's object
serialization API to determine if a deserialized object
was serialized (written) with the same version of the
class, as it is now attempting to deserialize it into.
If you make changes to the class that affect
serialization, you should also change its serialVersionUID
value.
What is escape analysis?
Ans: escape analysis is a method for determining the
dynamic scope of pointers - where in the program a
pointer can be accessed.
What is the size of byte?
Ans: 8 bits
What is the size of char?
Ans: 16 bits
What is the size of short?
Ans: 16 bits
, Page 4
What is the size of int?
Ans: 32 bits
what is the size of long?
Ans: 64 bits
what is the size of float?
Ans: 32 bits
what is the size of double?
Ans: 64 bits
What is the range of byte
Ans: -128 to 127
What is the range of short?
Ans: -32,768 to 32,767
What is the range of int?
Ans: -2,1 billion to 2,1billion
Java Latest Questions and Correct
Answers
If you overwrite clone(), which 3 rules must this method
obey?
Ans: 1) the new object should be new: memory address
should differ
2) Both should be an object of the same class
3) Both should be in the same state:
a.clone().equals(a) == true
Why do we need to implement the Clonable interface?
Ans: At runtime it would throw the
CloneNotSupportedException if we don't implement the
Cloneable interface. A class implements the Cloneable
interface to indicate to the Object.clone() method that
it is legal for that method to make a field-for-field copy
of instances of that class.
What is a marker interface?
Ans: A Marker interface, has no method.
Serializable, Clonable are marker interfaces.
How do you clone an object?
Ans: MyClone a = (MyClone) c.clone()
, Page 2
- TypeCast is nessesary.
- handle CloneNotSupportedException
Why can Annotations replace marker interfaces?
Ans: Annotations can convey metadata about the class
to its consumers without creating a separate type for it.
Annotations let you pass information to classes that
"consume" it.
What is Double-Checked Locking?
Ans: To reduce the overhead of acquiring a lock by first
testing the locking criterion without actually acquiring
the lock.
How do you make an object serializable?
Ans: The class must implement the java.io.Serializable
interface
Java object serialization is performed using which classes?
Ans: Java object serialization (writing) is done with the
ObjectOutputStream and deserialization (reading) is
done with the ObjectInputStream.
What is serialVersionUID?
, Page 3
Ans: In addition to implementing the Serializable
interface, a class intended for serialization should also
contain a private static final long variable named
serialVersionUID.
The serialVersionUID variable is used by Java's object
serialization API to determine if a deserialized object
was serialized (written) with the same version of the
class, as it is now attempting to deserialize it into.
If you make changes to the class that affect
serialization, you should also change its serialVersionUID
value.
What is escape analysis?
Ans: escape analysis is a method for determining the
dynamic scope of pointers - where in the program a
pointer can be accessed.
What is the size of byte?
Ans: 8 bits
What is the size of char?
Ans: 16 bits
What is the size of short?
Ans: 16 bits
, Page 4
What is the size of int?
Ans: 32 bits
what is the size of long?
Ans: 64 bits
what is the size of float?
Ans: 32 bits
what is the size of double?
Ans: 64 bits
What is the range of byte
Ans: -128 to 127
What is the range of short?
Ans: -32,768 to 32,767
What is the range of int?
Ans: -2,1 billion to 2,1billion