Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

April 29, 2010

Singleton Class (very Good one)

public class Singleton {
private static Singleton instance;
//public static int i =1 ;

private Singleton() {
}

public static Singleton getInstance() { // Using Double Checked Locking method
if (instance == null) {
synchronized (instance) {
if (instance == null) {
instance = new Singleton();
}
}
}
//i++;
return instance;
}

protected Object clone() throws CloneNotSupportedException
{
throw new CloneNotSupportedException();
}
}

http://www.roseindia.net/javatutorials/J2EE_singleton_pattern.shtml


We need to override clone() method as well so no one should create a new Instance using clone.

After this even, one can create multiple instances of a Sing class in a Clustered Enviroonment coz in a clustered enviro, we have a heap for each node. So, even if we create an instance, a new instance can be created on other node.
How to avoid this : we'll talk in next post :)

Depth First Polymorphism - Java

public class DepthFirstPoly {

public void Test(Object obj){
System.out.println("Obj");
}
public void Test(String obj){
System.out.println("String");
}
/*public void Test(StringBuffer obj){
System.out.println("String");
}*/
public static void main(String[] args) {
//System.out.println();
DepthFirstPoly dfs = new DepthFirstPoly();
dfs.Test(null);

}

}


Output :
String

April 23, 2010

Primitive Assignments - V. Important

The equal ( = ) sign is used for assigning a value to a variable, and it's cleverly named the assignment operator. There are actually 12 assignment operators, but only the five most commonly used

You can assign a primitive variable using a literal or the result of an expression.

Take a look at the following:

int x = 7; // literal assignment
int y = x + 2; // assignment with an expression
// (including a literal)
int z = x * y; // assignment with an expression

The most important point to remember is that a literal integer (such as 7) is always implicitly an int and an int is a 32-bit value. No big deal if you're assigning a value to an int or a long variable, but what if you're assigning to a byte variable? After all, a byte-sized holder can't hold as many bits as an int-sized holder. Here's where it gets weird. The following is legal,

byte b = 27;

but only because the compiler automatically narrows the literal value to a byte. In other words, the compiler puts in the cast. The preceding code is identical to the following:

byte b = (byte) 27; // Explicitly cast the int literal to a byte

It looks as though the compiler gives you a break, and lets you take a shortcut with assignments to integer variables smaller than an int. (Everything we're saying about byte applies equally to char and short, both of which are smaller than an int.)

We know that a literal integer is always an int, but more importantly, the result of an expression involving anything int-sized or smaller is always an int. In other words, add two bytes together and you'll get an int—even if those two bytes are tiny. Multiply an int and a short and you'll get an int. Divide a short by a byte and you'll get…an int. OK, now we're at the weird part. Check this out:

byte b = 3; //No problem, 3 fits in a byte
byte c = 8; // No problem, 8 fits in a byte
byte d = b + c; // Should be no problem, sum of the two bytes
// fits in a byte

The last line won't compile! You'll get an error something like this:

TestBytes.java:5: possible loss of precision
found : int
required: byte
byte c = a + b;
^

We tried to assign the sum of two bytes to a byte variable, the result of which (11) was definitely small enough to fit into a byte, but the compiler didn't care. It knew the rule about int-or-smaller expressions always resulting in an int. It would have compiled if we'd done the explicit cast:

byte c = (byte) (a + b);

April 11, 2010

Futures vs Forwards

FUTURES are simply Fixed Date Forwards

Forward and Futures Contracts are essentially the same, the differences being largely in conventions regarding the quotation of prices. Since we are familiar with Forward Contracts, the similarities/ differences of the Futures contracts are set off against the features of Forward Contracts, as below:

Forwards

Futures

1. Traded in the OTC (over the counter) or Interbank market, with buyer/ seller bearing mutual counterparty risk.

1. Traded on Exchanges, with the Exchange bearing counterparty risk with respect to the buyer/ seller.

2. Can have any maturity date, customized to the requirement of the Buyer.

2. Has Fixed maturity dates. In the case of Dollar-Rupee Futures, the maturity dates will be the last working day of a calendar month, going out to 12 months.

3. Can be for any amount, customized to the requirement of the Buyer.

3. Lot Size is fixed. Transactions can be in multiples of the fixed Lot Size, not in broken/ odd amounts.

4. The Forward Rate = Spot Rate + Forward Difference, where Forward Difference <> 0.

4. The Futures Rate = Spot Rate + Forward Difference, where Forward Difference <> 0

5. In case of USD-INR, the Forward Rate is quoted as X INR per 1 USD. For example, the current Forward Rate for 31-Aug-08 is 42.3350 INR per USD 1.

5. Some clarity is needed on this, but our understanding, so far, is that for Dollar-Rupee futures, the Futures Rate will also be quoted in the same manner as the Forward Rate.

6. No margin requirement

6. Margin required

7. Avowedly non-speculative for Corporates in the Indian context, because trades (hedges) can be done only against and to the extent of actual exposures.

7. Fully speculative, because the contracts will be cash-settled on expiry. No delivery of Dollars can either be taken or given.

8. Account for, by far, the lion's share of the market volume. Estimates range from 80-95%.

8. Accounts for a small part of the total market volume, with estimates ranging from 5-20%.

9. Client (corporate) trades/ hedges with a bank.

9. Client will trade/ hedge with a broker who is a member of the exchange.

In the proposed Futures market, any resident Indian can buy/ sell Dollar-Rupee Futures (lot size USD 1000) upto a maximum of USD 5 million with a broker, at rates similar to the currently quoted Forward Rates. The Contract may be squared off at or before maturity, the difference in rates being settled in cash.


http://www.kshitij.com/inr/forwards&futures.shtml