Tuesday, February 4, 2020

Java Interview @ Gemini Solutions

Hi Friends,

In this post, I'm sharing Java Interview questions for the role of Java Technical Lead in Gemini Solutions.


You may also interested in going through my other interview questions in many I.T. companies:




Question 1:

Write algorithm for Infix expression evaluation.

expression is : 3*(5-1)/3 + 1


Answer:

Algorithm:

Approach: Use two Stacks


  • operand stack
  • operator stack
Process:
  1. Pop out two values from operand stack, let's say it is X and Y.
  2. Pop out operator from operator stack. Let's say it is '+'
  3. Do X + Y and push the result on the operand stack.

Iterate through given expression, one character at a time.

  1. If a character is an operand, push it on the operand stack. 
  2. If the character is an operator,
    • If the operator stack is empty, push it onto the operator stack.
    • Else if the operator stack is not empty,
      • If the character's precedence is greater than or equal to the precedence of the stack top of the operator stack, then push the character to the operator stack.
      • If the character's precedence is less than the precedence of the stack top of the operator stack , then do Process [As described above] until character's precedence is less or stack is not empty.
  3. If the character is "(", then push it onto the operator stack.
  4. If the character is ")",  then do Process [As described above]  until the corresponding "(" is encountered in operator stack. Now just pop out the "(".


Question 2:

What is the difference between Lambda and anonymous class?


Answer:

There are multiple differences between lambda and anonymous class:


  • Usage of 'this' keyword: IN anonymous class, this keyword resolves to anonymous class itself. Whereas for Lambda, this keyword resolves to enclosing class where lambda expression is written.
  • Compilation: Java compiler compiles lambda expression and convert them into static private method of the class. It used invokedynamic bytecode instruction that was added in java 7to bind this method dynamically. While anonymous class compiles to a class. The compiler generates a class file for each anonymous inner class.
  • Lambdas implement a functional interface, so implements only 1 method. Anonymous class can extend a class or implement multiple interfaces with any number of methods.



Question 3:

Design Email [Inbox, Outbox, attachment]. Write all technologies that will be used. Create Class diagram.

Answer:

I designed as per experience.



Question 4:

What is the use of volatile keyword? How does it help in synchronization? Does it provide full synchronization?

Answer:

Volatile is used to avoid the local copy of a variable inside  a thread. All threads will access the volatile variable from main memory , updates it and immediately put it back in main memory.
All, reads and writes would be made directly to main memory instead of to registers or local processor cache.

volatile variable is not thread safe. If multiple threads try to write on a volatile variable, then Race condition occurs.

Each thread has a separate memory space known as working memory. This holds the value of different variables for performing operations. After performing an operation, thread copies the updated value of the variable to main memory and from there other threads can read the latest value.

But if the variable is non-volatile, then new value may not be flushed immediately to main memory. And other threads may not read the updated value.



Question 5:

Why to use transient variable? What will be the value of transient variable after deserialization?
Where should we use transient variable?


Answer:

transient variable is used when we don't want value of that variable to be serialized.
So, if a variable is declared as transient, then it's value will not be serialized.

On deserialization, we get the default value of transient variable. e.g. :

transient int i ;

When the instance of this class is serialized, then upon deserialization, we get 0 in i , as 0 is the default value for an int in java.

Even if we declare i as transient int i = 10;
In this case, as well we get value of i as 0 after deserialization.

Use case  [Where should we use it]:

When we have a variable whose value can be calculated from other variables, the we should make it transient, as it should be calculated from other variables every time.



Question 6:

How will you write an immutable class?


Answer:

There are two ways to write an immutable class:


  • Make the class final and all it's instance variables as final, so that they can be initialized in constructor only.
  • Make all instance variable of the class as private and don't modify them inside in method. Means don't provide any setters methods. And also make all getters methods as final so that they can't be overridden and subclasses cannot override them and return some other values from overridden forms.



Question 7:

Write Lambda expression for printing even numbers from a list.

Answer:

Suppose , we have a list of integers.

int[] array = {1,2,3,4,5};

List<Integer> list = Arrays.asList(array);

list.stream().filter(i-> i%2 == 0).collect().forEach(System.out :: println);



Question 8:

Difference between default method and regular method in java 8.


Answer:

Differences between default method and regular method are as follows:


  • default method has default keyword in it's method signature. While regular method has no such keyword.
  • default cannot change the state of the object as it works only inside interface and ca only access it's local variables. While a regular method ca change the state of the object by modifying method arguments as well as fields of the class.




That's all for this interview.

Hope, this interview help everybody in clearing java technical rounds.

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