Java Interview questions – Part 3

Interview questions topjavatutorial

 

Java Interview Questions:

  1. What is Anonymous Inner class ?
  2. What is Static nested class ?
  3. What is Transient Variable ?
  4. What is Volatile Variable ?
  5. How to create object instance without using new operator ?
  6. Can we write our own Marker Interface, if yes, How? and how can we use that?
  7. How can we implement interface without using implements keyword?
  8. When to use abstract class or interface? 
  9. Difference between compareTo vs compare methods
  10. What are the differences and similarities between an abstract class and an interface?
  11. What is the difference between Abstraction and Encapsulation?
  12. Why is the main() function defined as static?
  13. What’s the difference between .equals() and “==”?
  14. What is Type Erasure?
  15. Why JAVA uses  HEAP for creating objects?

 

1.What is Anonymous Inner class ?

There are 2 flavors of it:

Flavor one creates an anonymous subclass of a class type where as flavor two creates an anonymous implementer of specified interface type.

Flavor 1:

Class PopCorn{

Public void pop(){}

}

Class B{

PopCorn p = new PopCorn (){

Public void pop(){}

}

}

Flavor 2:
Interface Cookable{

Public void cook();

}

Class Food{

Cokkable c = new Cookable(){

Public void cook(){}

}

}

  • Anonymous interface implementers can implement only one interface
  • An Anonymous inner class can’t even extend a class and implement an interface at the same time
  • An argument is declared, defined and automatically instantiated as part of the method invocation. The class is being defined within a method argument, so the syntax will end with });

2.What is Static nested class ?

A static nested class is simply a class that’s a static member of the enclosing class

class Outer{

Static class inner{

}

}

Static nested class can be invoked from outside the outer class, in following way :
Outer.inner innerClass = new Outer.inner();

3.What is Transient Variable ?

Transient variable does not get serialized

4.What are Volatile variables ?

The value of a volatile field becomes visible to all readers (other threads in particular) after a write operation completes on it. Without volatile, readers could see some non-updated value.

5.How to create object instance without using new operator ?

There are only three standard ways to instantiate a class without the use of the new operator, and they are as follows:

1) newInstance()

Option 1:

Using newInstance method of Class class

Class clas = Class.forName("NewClass");
NewClass obj = (NewClass) clas.newInstance();

Option 2:

Using ClassLoader

getClass().getClassLoader().loadClass("NewClass").newInstance();

Option 3:

Using Reflection

Eg: constructor.newInstance() and class.newInstance()

2) clone()
3) De-serialization

6.Can we write our own Marker Interface, if yes, How? and how can we use that?

Marker interface can be created by simply writing an interface with no body and informing compiler that another class will provide needed decoration. One specific instance of this is when you create a sub-interface that extends from a super-class interface containing all abstract methods

The use of annotations have become a replacement for marker interfaces. Therefore, there is not much of a need for this such as declaring an interface for an EJB that is to be implemented by a concrete class.

Read more on marker interface.

7.How can we implement interface without using implements keyword?

Anonymous inner class can be used for this purpose. Following code is an example of this :

Collections.sort(someList, new Comparator(){
public int compare(Object obj1, Object obj2)
{
return obj1,equals(obj2);
}
});

8.When to use abstract class or interface? 

Use Abstract class :
-If various implementation are of the same kind and use common behaviour or status Then better to use abstract .
-If you plan on updating the base class throughout the life of program.

Use Interface :

  • In general, this is done because java does not support extending multiple classes.
  • Interface should be used when we need to define common behaviour which can have different variations of implementation.
  • You see that something in your design will change frequently . then use interface.

9.Difference between compareTo() and compare() methods

 

compareTo() is a method of the interface Comparable, so it is used to compare this instance to another one. You will want to implement Comparable<T> in your class if you want to use it with Collections.sort() or Arrays.sort() methods.

compare() is a method of the interface Comparator, so it is used to compare two different instances of another class with each other.

10.What are the differences and similarities between an abstract class and an interface?

Interfaces allow a form of multiple inheritance. A class can only extend one other class. Pre-Java8 interfaces are limited to public methods and constants with no implementation, while abstract classes can have a partial implementation, protected parts, static methods, etc. Java 8 onwards interfaces can have default method implementations.

Also, a class may implement several interfaces, but a class may extend only one abstract class. Interfaces tend to be slow as there is extra indirection required to find the corresponding method in the actual class, while abstract classes are faster. As for the similarities, neither abstract classes nor interfaces can be instantiated.

11.What’s the difference between abstraction and encapsulation?

Abstraction hides the implementation details, whereas encapsulation hides the data. Abstraction lets you focus on what the object does instead of how it does it.

12.Why is the main() function defined as static?

So it can be called without needing to create an instance.

13.What’s the difference between .equals() and “==”?

“==” operator is used to see if two objects are the same object. For example, to see if it occupies the same area in memory while equals compares values.

“==” comparison is done by comparing references while .equals compares the contents. So two different strings with the same value would give true for “.equals()” but false for “==”.

14.What is Type Erasure?

Type erasure means the runtime has no knowledge of the types of generic objects.

15.Why JAVA uses  HEAP for creating objects?

The memory of a process is divided in some memory regions like heap, stack, data and code. The heap area/segment is growable. That’s why all run-time created objects goes there (to theoretically allow N number of object creations).
 
 

© 2015 – 2018, https:. All rights reserved. On republishing this post, you must provide link to original post

One comment

  1. […] Java Interview Questions – Part 3 […]

Leave a Reply.. code can be added in <code> </code> tags

%d bloggers like this: