Thursday, June 2, 2016

Simple Design Pattern Interview Questions

Problem-1: Can you explain about access specifiers in java?

Answer: In java , classes,variables,methods and constructor can have access specifiers.There are 4 types of access specifiers.

  1. private
  2. protected
  3. public
  4. default or No specifier or none
There is a point to be noted about constructors in java are treated differently than methods because constructor are not considered a class member.

Class Members are made of two things:
  • Class's variable
  • Class' methods
Problem-2 : Given a database application,Can you think of classes to generate unique sequence numbers?

Answer: Here we can use Singleton Pattern to create a counter to provide unique sequential numbers.This is can be used as primary keys in database.

public class SequenceNumber
{
  private static SequenceNumber instance;
  private static int count;
  private SequenceNumber(){
    count=0;
 }
public static synchronized SequenceNumber getInstance()
{
   if(instance==null)
 {
  instance =new SequenceNumber();
 }
  return instance;
}

public synchronized int getNext()
{
  return ++count;
}

}

No comments: