Wednesday, May 27, 2020

Java Technical Lead interview @ Accenture

Hi friends,

In this post I'm sharing java interview questions asked in Accenture in Java Technical Lead interview.

You can also go through my other Java Interview posts at multiple levels:


Question 1:

What is the difference between SSL and TLS?


Answer:

There are multiple differences between SSL and TLS:

Alert Messages:

    SSL has "No Certificate" alert message. TLS protocol removes the alert message and replaces it with several other alert messages. 



Record Protocol:

    SSL uses MAC [Message Authentication Code] after encrypting each message while TLS     on the other hand uses HMAC - a hash based message authentication code after each      message encryption. 


Handshake Process:

    In SSL, the hash calculation also comprises the master secret and pad while in TLS, the hashes are calculated over handshake message.



Question 2:

What changes are introduced in Java 10?


Answer:

There are multiple changes that have been done in java 10. Explaining all the changes below:

  • var keyword
  • Unmodifiable Collection enhancements
  • G1GC performance improvement
  • New JIT compiler [Created in pure java]
  • Alternate memory devices for allocating heap
  • Application class data sharing


var keyword:

It improves readability. It is not a reserved word, means it can be used as a class name or variable name. What the var keyword does is , turn local variable assignments:

HashMap<String, String> hm = new HashMap<>();  

into

var hm = new HashMap<String,String>();


Unmodifiable Collection enhancements:

var vegetables = new ArrayList<>(Lists.of("Brocolli", "Celery", "Carrot"));

var unmodifiable = Collections.unmodifiableList(vegetables);
vegetables.set(0, "Radish");
var v = unmodifiable.get(0); //  var v contains Radish.

It is so because, unmodifiableList() returns an unmodifiable view collection.
An unmodifiable view collection is a collection that is unmodifiable and that is also a view onto a backing collection.

Note that changes in the backing collection is still possible , and if they occur, they are visible through the unmodifiable view.  

So, if we need completely unmodifiable collection then, java 10 has added two new API's.

var unmodifiable = List.copyOf(vegetables); 

The second API adds three new methods to the Collector class in the  Stream package. 

We can now stream directly into an unmodifiable collection using toUnmodifiableList, toUnmodifiableSet and toUnmodifiableMap.

  


Question 3:

What is the improvement in G1GC in java 10?

Answer:

Java 9 made the Garbage-First garbage collector (G1GC) by default , replacing the concurrent Mark-sweep garbage collector (CMS).  

Java 10 introduces performance improvements to G1GC.

In java 10, G1GC is getting a performance boost with the introduction of full parallel processing during a full GC.



Question 4:

What is Application class-data sharing?

Answer:

Java 5 introduced Class-Data sharing (CDS) to improve startup times of small java applications.

The general idea was that when the JVM first launched , anything loaded by the bootstrap classloader was serialized and stored in a file on disk that could be reloaded on future launches of the JVM. 
This means that multiple instance of the JVM shared the class metadata so it wouldn't have to load them all every time.


Question 5:

How to write thread safe code in java?

Answer:

Example of Non thread safe code in java:

public class Counter{

    private int count;

// This method is not thread safe because ++ is not an atomic operation. 
    public int getCount(){
        
        return count++;
    }

}

How to make code thread safe in java:

There are multiple ways to make this code thread safe:

1). Use synchronized keyword in java and lock the getCount() method so that only one thread can execute it at a time.

2). Use atomic integer , which makes this ++ operation atomic and since atomic operations are thread-safe and saves cost of external synchronization.

Below is the thread-safe version of Counter class in java:

public class Counter{

    private int count;

    AtomicInteger atomicCount = new AtomicInteger(0);

    //This method is thread safe now because of locking and synchronization

    public synchronized int getCount(){

        return count++;
    }

    //This method is thread safe because count is incremented atomically.

    public int getCountAtomically(){

        return atomicCount.incrementAndGet();
    }
}


Question 6:

How does AtomicInteger work?

Answer:

Java 5 introduced java.util.concurrent.atomic package with a motive to provide a small kit of classes that support lock-free thread-safe programming on single variables. 

AtomicInteger uses combination of volatile and CAS [Compare and Swap] to achieve thread safety for Integer Counter.

It is non blocking in nature.

Compare-and-Swap:

CAS is an atomic instruction used in multi-threading to achieve synchronization.
It compares the contents of a memory location with a given  value and only if they are same, modifies the contents of that memory location to a given new value.  
This is done as a single atomic operation.

The atomicity guarantees that the new value is calculated based on up-to-date information. If the value has been updated by another thread in the meantime, the write would fail.



That's all for this post.

Thanks for reading!!

2 comments:

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...