Verizon Interview Question

You have to write a jar file (plain java code) In that you will supply a list(ArrayList) object that will be used by client. Client can do anything they add elements delete elements. You have to put a constraint on the list object 1. Not allow Null 2. Size of list is not more than 5 If it does not satisfy the condition then throw a custom error.Write the code

Interview Answer

Anonymous

Oct 31, 2016

public class MyList extends ArrayList{ @Override public boolean add(Object e) { boolean flag=false; try{ if (e!=null && size()=5) throw new RuntimeException ("MyList-Exception : List Size exceeded. Only 5 elements allowed !"); //System.out.println("Error: List Size excceded or Trying to Insert null"); } } catch(Exception e1){ e1.printStackTrace(); System.exit(0); } return flag; } } catch(Exception e3){ e3.printStackTrace(); System.exit(0); } }

2