Friday, March 27, 2020

Technical Architect interview in Incedo - Part - II

Hi Friends,

In this post I'm sharing interview questions-answers from one more interview @ Incedo.

Also go through my other interesting interview posts:




Technical Architect interview in Incedo :


Question 1:

Explain when and why to use Factory Design Pattern.

Answer 1:

Factory Design Pattern is used in following scenarios:


  • When we need to separate  the logic of object creation from our other code or client code.
  • Whenever we need the ability to add more subclasses to the system and modified them without changing the client code.
  • We let subclasses to decide which object to instantiate by overriding the factory method.

UML diagram for Factory Design Pattern is :





Question 2:

Explain when and why to use Abstract Factory Design Pattern.

Answer 2:


Whenever we have to work with two or more objects  which work together forming a set/kit and there can be multiple sets or kits  that can be created by client code, then we will use Abstract Factory Design Pattern.

Lets take an example of a War Game:

Medieval:
  • Land Unit
  • Naval Unit
Industrial:
  • Land Unit
  • Naval Unit

Here , we have two types of objects Land Unit and Naval Unit under Medieval age.
And also two types of objects under Industrial age.

So, we can take two factories : Medieval Age Factory and Industrial Age Factory.

UML Diagram for above scenario is :







Another example we can take is :







Question 3:

When and why to use Generic class? Explain with example. Can we use generics with array?

Answer 3:

A generic type is a type with formal type parameters.

e.g.:

interface Collection<E>{

    public void add(E e);
    public Iterator<E> iterator();

}

A parameterized type is an instantiation of a generic type with actual type arguments.

Example of a parameterized type:

Collection<String> coll = new LinkedList<String>();


Generic class is used when we need to create similar types of object with similar features.

e.g.:

Suppose , we are developing a database library called Data Access Object (DAO) for a program that manages resources in a university.

We would write a DAO class for managing Students like:

public class StudentDAO{

    public void save(Student st){
          // some code here ....
    }

    public Student get(long id){

        // Some code here ...
    }

}

This looks fine. But if we need to persist Professor objects to database. Then we would write another DAO class as:

public class ProfessorDAO{

    public void save(Professor st){
          // some code here ....
    }

    public Professor get(long id){

        // Some code here ...
    }

}

Note: These two classes are same. And if we need to add more entity classes to the system like course, facility etc, then?

So, to avoid such situation ,we write a Generic class:

public class GeneralDAO<T>{

    public void save(T entity){

    }

    public T  get(long id){

    }

}

Here T is called type parameter of GeneralDAO class. T stands for any type. The following code illustrates how to use this generic class:

GeneralDAO<Student> studentDAO = new GeneralDAO<Student>();

Student student = new Student();

studentDAO.save(student);

Note: Type parameters are never added in names of methods and constructors. They are only added in names of classes and interfaces.




Question 4:

What is Type Erasure?

Answer 4:

Generics provide compile time type safety and ensures we only insert correct type in Collection and avoids ClassCastException.

Generics in java are implemented using Type Erasures.

It is a process of enforcing type constraints only at compile time and discarding the element type information at runtime.

There are two types of Type Erasures:


  • Class Type Erasure
  • Method Type Erasure 


e.g.:

public static <E> boolean  containsElement(E[] elements , E element){

    for(E e : elements){

        if(e.equals(element)){
            return true;
        }
    }
    return false;
}

When compiled, the unbound type E gets replaced with an actual type of Object as:

public static  boolean  containsElement(Object[] elements , Object element){

    for(Object e : elements){

        if(e.equals(element)){
            return true;
        }
    }
    return false;
}

The compiler ensures type safety of our code and prevents runtime errors.



Question 5:

Explain Exception handling in java.


Answer 5:

Types of Exceptions:


  • Checked Exceptions
  • Unchecked Exceptions
  • Error

Checked Exception: Exceptions that are directly inherit from Throwable class except RuntimeException and Error are known as checked exceptions.


Unchecked Exceptions: The classes which inherit RuntimeException are known as unchecked exception.








Question 6:

What is static import in java? How to use it?

Answer 6:

Static import allows developers to access static members of a class directly.There is no need to qualify it by the class name.

Example:

import static java.lang.System.*;

class StaticImport{

    public static void main(String[] args){

        out.println("Ok"); // No need to use System.out
        out.println("Java");
    }

}

O/P:

Ok
Java



That's all for this post.

Hope these interview questions help everybody.

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