Java Interview Questions | Page 8


Question 71:

What is the use of values() method in enum?

The compiler automatically adds a static values method that returns an array containing all of the values of the enum in the order they are declared.

Question 72:

Define Serialization in Java.

Serialization is the process by which state of object can be converted to a byte stream to store in a file or memory buffer, or transmitted across a network connection link. This process of serializing an object is also called marshalling an object. To make a Java object serializable you need to implement the java.io.Serializable interface. This is a marker interface which tells the Java platform that the object is serializable.

Question 73:

Can you transfer a non-serialized object through network?

No, As transmission to network is done in form of bytes, we cannot transfer a non serialize object through network.

Question 74:

Define transient keyword and its uses in serialization.

The transient keyword is a modifier that is applied to instance variables in a class. It specifies that the variable is not part of the persistent state of the object and it is not saved during serialization.
                                      You can use the transient keyword to describe temporary variables, or variables that contain information which are related to execution not the state of any object such as a processing time.

Question 75:

Is Java serialization secured? If not, how will we secure it.

When we serialize object it is in binary format an fully documented and entirely reversible. In fact, just dumping the contents of the binary serialized stream to the console is sufficient to figure out what the class looks like and contains. This is a big security concern.
                                                  Using encryption techniques and signing an entire object we can secure it from the above issue.The simplest thing is to put it in a javax.crypto.SealedObject and/or java.security.SignedObject wrapper. Both are serializable, so wrapping your object in SealedObject creates a sort of "box" around the original object. You need a symmetric key to do the encryption, and the key must be managed independently. Likewise, you can use SignedObject for data verification, and again the symmetric key must be managed independently.Together, these two objects let you seal and sign serialized data without having to stress about the details of digital signature verification or encryption.


Question 76:

Describe Varargs. When do you use varargs in Java?

Java 5 allows you to declare methods that take a variable number of arguments of a specified type. Before varargs either we use overloaded method or take an array as the method parameter. If a programmer doesn't know how many argument any method will have, varargs is the better approach.
For example :-

 public void notify(int i, String... properties)
 {
     //Code Here
 }

Some of the best known methods accepting variable arguments in Java are:-
String.format() method and
Arrays.asList() method

Question 77:

How to convert an String to Enum in Java?

You can create Enum from String by using Enum.valueOf() method.
valueOf() is a static method which is added on every Enum class during compile time.
For example :-

public class EnumConversion
{
                      private enum BankCodes{ HDFC, RBS, SBI}
                      public static void main(String args...)
                      {
                            String SBICode = "SBI";

                             Enum bankCode = BankCodes.valueOf(SBICode);

                      }
}
Question 78:

Can we extend an Enum class?

No. As Enum class is final, We cannot extend enum class.

Question 79:

What are the implicit static methods for Enums?

There are two static methods that are created in any enum class at the time of compilation :-

  • static EnumTypeName[] values()
  • static EnumTypeName valueOf(String name)
Question 80:

Describe Autoboxing and Unboxing in Java.

In Java 1.5, autoboxing and unboxing is introduced to convert primitive type into its equivalent Wrapper type and vice versa automatically.

While autoboxing primitive type is converted to Wrapper type for example :- boolean to Boolean, byte to Byte, char to Character, float to Float whereas while unboxing Wrapper type is converted to primitive for example :- Float to float.
Code Snippet :-
                 //before autoboxing
                 Integer wrapperType = Integer.valueOf(100); //Extracode for Conversion
                 int primitiveType = wrapperType.intValue() //Extracode for Conversion

                //In java 1.5
                Integer wrapperType = 3; //Automatic Conversion
                int primitiveType = wrapperType; //Automatic Conversion

Share interview questions

Comments

Comments
comments powered by Disqus

Navigation

Social Media