Basics – 3

      Question

      1. What is the interface? Why you use it if you cannot write anything concrete on it?
        It tells about the contract your classes will follow. It also supports abstraction because a client can use interface method to leverage multiple implementations e.g. by using List interface you can take advantage of random access of ArrayList as well as flexible insertion and deletion of LinkedList. The interface doesn’t allow you to write code to keep things abstract but from Java 8 you can declare static and default methods inside interface which are concrete.Answer
      2. The difference between abstract class and interface in Java?
        The most important one is Java’s restriction on allowing a class to extend just one class but allows it to implement multiple interfaces.
        An abstract class is a class which can have state, code and implementation, but an interface is a contract which is totally abstract.
        An abstract class is good to define default behavior for a family of class, but the interface is good to define Type which is later used to leverage Polymorphism.
        Answer
      3. The difference between sleep and wait in Java?
        Though both are used to pause currently running thread, sleep() is actually meant for short pause because it doesn’t release lock, while wait() is meant for conditional wait and that’s why it release lock which can then be acquired by another thread to change the condition on which it is waiting. Answer
      4. What is an immutable object? How do you create an Immutable object in Java?
        Immutable objects are those whose state cannot be changed once created. Any modification will result in a new object e.g. String, Integer, and other wrapper class. Answer
      5. Can we create an Immutable object, which contains a mutable object?
        Yes, its possible to create an Immutable object which may contain a mutable object, you just need to be a little bit careful not to share the reference of the mutable component, instead, you should return a copy of it if you have to. Most common example is an Object which contain the reference of java.util.Date object. Answer
      6. What is the right data type to represent a price in Java?
        BigDecimal if memory is not a concern and Performance is not critical, otherwise double with predefined precision. Answer
      7. How do you convert bytes to String?
        you can convert bytes to the string using string constructor which accepts byte[], just make sure that right character encoding otherwise platform’s default character encoding will be used which may or may not be same. Answer
      8. Can we cast an int value into byte variable? what will happen if the value of int is larger than byte?
        Yes, we can cast but int is 32 bit long in java. while byte is 8 bit long in java. so when you cast an int to byte higher 24 bits are lost. Answer
      9. Which class contains clone method? Cloneable or Object?
        java.lang.Cloneable is marker interface and doesn’t contain any method clone method, it is defined in the object class. Answer
      10. Is ++ operator is thread-safe in Java?
        No, it’s not a thread safe operator because its involve multiple instructions like reading a value, incriminating it and storing it back into memory which can be overlapped between multiple threads. Answer






      <Previous> < Next >