Java Interview Questions and Answers – Part 5


15. Explain Casting ?

When we receive an object of parent class, and if we need to cast it to correct sub class we use casting. For example

16.  What is an immutable class?

Immutable class are classes whose objects once initialized with a value, cannot be changed. e.g. String class. All wrapper classes given below are immutable. Any change in the existing immutable objects creates a new object.

17. What are the advantages of  immutable objects ?

Since immutable objects are thread safe, overhead caused by synchronization in multi threaded application can be avoided.

18. What are the different types of inner classes?

A class within a class is called as inner class. Sometimes it is also known as nested class.
There are 4 types of inner classes.

  • Member Inner Class : A class that is a member ( like methods, attributes ) is called as a member inner class.
  • Local Inner Class: A class which is defined in a block( without name) is known as local inner class.
  • Static Inner Class: A class with static modifier in its definition is known as static inner class. Like other static members, a static inner class member is to be referred by its class name.
  • Anonymous Inner Class: A class that has no name and exactly implements only one interface or extends one abstract class is known as anonymous inner class.
19. What are the Java wrapper Classes?

Wrapper classes convert numeric strings into numeric values. The way to store primitive data in an object. The valueOf() method is available in all wrapper classes except Character. All wrapper classes have typeValue() method. This method returns the value of the object as its primitive type.

20. How to use wrapper classes in java? Explain with an example.

Java uses primitive types and are part of any object hierarchy. These values are passed to methods by values.
Character class methods:
        isDigit() – to determine whether the character is digit.
        isLower() – to determine whether the character is lower case alphabet.
        is Letter() – to determine whether the character is an alphabet.
Ex:

Byte class methods:

byteValue() – returns the Byte value as byte value
Ex : byte bvalue = Byte.byteValue();
parseByte() – returns byte value from a byte string
Ex: byte bvaue = Byte.parseByte(“73”);

Integer class methods:

intValue() – returns the Integer value as int value
Ex: int bvalue = Integer.intValue();
parseInt() – returns int value from a int string
Ex: int bvaue = Integer.parseInt(“53”);

21. What are the methods of the Wrapper class which are used to manipulate the data?

Some of the data manipulation methods from Byte and Integer classes are:

Byte Class

  • static Byte decode(String nm): A string is decoded into Byte
  • double doubleValue(): Returns the value of Byte as double value
  • byte parseByte(String str): Returns byte value of the given byte string
  • static String toString(byte b): Returns String object representing the byte type
  • byte byteValue(): Returns byte value of the Byte object

Integer Class

  • byte byteValue(): Returns byte value of the Integer object
  • int intValue(): Returns int value of the Integer object
  • static int reverse(int ivalue): Returns the value after reversing the order of bits specified in two’s complement for an int value.
  • static String toBinaryString(int inum): Returns the String representation of int as an unsigned integer in base 2.
  • static String toHexString(int inum): Returns the String representation of int as an unsigned integer in base 16.
  • String toString(): Returns a string object that represents this Integer’s value.
22. What are wrapper classes for primitive types?
Primitive Data Types Wrapper class
byte Byte
short Short
int Integer
long Long
char Character
float Float
double Double
boolean Boolean
23. What is serializable Interface?

If we want to transfer data over a network then it needs to be serialized. Objects cannot be transferred as they are. Hence, we need to declare that a class implements serializable so that a compiler knows that the data needs to be serialized.

24. What are Generics ?

Collection Classes uses object type to permit different input and return types. We need to explicitly cast the object to retrieve the object we need. In order to avoid this issue, we use generics.

For example, ArrayList a = new ArrayList();

25. What is the concept of Comparator Interface?
  • Natural ordering is established by using Comparator interface. This interface can be used to sort the objects which does not have natural sorting order.
  • Comparator interface is passed to sort methods or to have precise control over the sort order. The order of certain data structures such as sorted sets, sorted maps which do not have a natural ordering.

About Siva

Senior Hadoop developer with 4 years of experience in designing and architecture solutions for the Big Data domain and has been involved with several complex engagements. Technical strengths include Hadoop, YARN, Mapreduce, Hive, Sqoop, Flume, Pig, HBase, Phoenix, Oozie, Falcon, Kafka, Storm, Spark, MySQL and Java.

Leave a comment

Your email address will not be published. Required fields are marked *