I am trying to write Stack code using the two techniques i.e Design by Contract vs Defensive Programming but I am not sure if I am doing right or not.I am not throwing any kind of exception or error in design by contract assuming all the inputs will be correct. 
Stack implementation by Design by Contract vs Defensive Programming
stack;defensive programming;design by contract
Design by contractIn your example 1,  no check is done.  The implementation assumes that the client respects the terms of the contract.  So yes, you could be tempted to claim that it's DBC.  However what's missing is the expression of your contract:  there's no clue of  pre-conditions, post-conditions and invariants in the code.  This is missing.  In C++  you would put some assert() at the begin and the end of each function.  These would do hard check (and abort in case of infrigement) during debugging, but would be skipped in production release. In Java, you would annotate your code with javadoc tags @pre, @post and @inv  in the comments for documentation, but also for processing as explained here with tools like jContracts  or similar tools/libraries.  Defensive programmingIn your second example,  you actively enforce the check if the expectations of the contract are met and take an alternate path of execution to handle the unexpected case (here with exceptions).  This seems to correspond to the anticipation expected from defensive programming.  