Monday, February 3, 2020

Java Interview @ OrangeMantra

Hi Firends,

In this post, I'm sharing interview questions asked in OrangeMantra.

Also read Questions-Answers asked in other interviews:





Question 1:

List all types of HTTP Error code categories

Answer:

There are mainly 5 categories in HTTP Response codes:


  • 1XX : Informational
    • 100 : Continue
  • 2XX : Success
    • 200 : OK
    • 201 : Created
    • 202: Accepted
    • 204: No Content
  • 3XX : Redirection
  • 4XX : Client Side Error
    • 400 : Bad Request
    • 401 : UnAuthorized
    • 402 : Payment Required
    • 403 : Forbidden
    • 404 : Not Found
  • 5XX : Server Side Error 
    • 500 : Internal Server error
    • 501: Not Implemented
    • 502: Bad Gateway
    • 503: Web service not available


Question 2:

List all the features in Java 7

Answer:

There are multiple features which have been added in java 7:

  • String in switch
  • try with resources
  • Binary Literals
  • multiple Exception types in catch block


Question 3 :

Explain about one very typical designing/Coding situation which you have solved.

Answer:

I answered as per my experience.



Question 4:

Explain all differences between Stack and Heap?


Answer:

  • Heap is very large in size while Stack is small in size as compared to Heap.
  • Every thread has it's own Stack while Heap is shared among all threads.
  • When Heap gets full, java.lang.OutOfMemoryException is thrown. While in case of Stack, java.lang.StackOverflowException is thrown.
  • Heap is mainly used to store the objects, while Stack is used for storing local data, method body , method return values etc.
  • Heap is tuned by using -Xmx and -Xms options. While Stack size is tuned by -Xss.
  • In Heap, data is stored in random order. While in  Stack , data is stored in LIFO [Last-In-First-Out] order.


Question 5:

What is the contract between natural ordering and equals() method in java?

Answer:

Contract between natural ordering or ordering provided by comparator interface and equals() method is that the output returned by natural ordering / comparator interface should be consistent with value returned by equals() method.

If these values are not consistent, then it will lead unexpected results in TreeMap and TreeSet Collections.



Question 6:

What is the use case of Marker interface ? How to write custom marker interface?

Answer:

Marker interface is used to indicate something special to JVM/Compiler so that it can perform necessary actions on the class implementing marker interface.

Examples of marker interfaces are : Serializable, Cloneable, Remote

Custom marker interface:

public interface Ingestible{
    //nothing
}

public interface Edible extends Ingestible{


}


public interface Drinkable extends Ingestible{

}


public class Food implements Edible{

}


public class Drink implements Drinkable{

}


public class Person{

    public void ingest(Ingestible something){
        
         if(something instanceof Edible){

              System.out.println("something can be eaten");

         }
    }
}

In above example, we are telling JVM that the class implementing Ingestible interface can be ingested.

Use case:

A marker interface is used for Identification purpose.



Question 7:

Why to use BigDecimal over Float or Double?


Answer:

Float and Double cannot be used for precise values: e.g.:

double d1 = 131.44;
double d2 = 130.34;

System.out.println("d1 - d2 = "+ (d1-d2));

Output:  1.10000000000013

That's why in financial applications, where while doing calculations , scale and rounding mode for the numbers is an important aspect . So it's a better choice to go with BigDecimal.


Using BigDecimal:
BigDecimal bd1 = new BigDecimal("131.44");
BigDecimal bd2 = new BigDecimal("130.44");

System.out.println(bd1-bd2);  gives 1.10







That's all for this interview post.

Hope this post helps everybody in their java interviews.

Thanks for reading !!


No comments:

Post a Comment

CAP Theorem and external configuration in microservices

 Hi friends, In this post, I will explain about CAP Theorem and setting external configurations in microservices. Question 1: What is CAP Th...