Arduino DUE samples per second with the DAC
According to the datasheet:


  
  2-channel 12-bit 1 msps DAC
  


So it's possible to generate 1 million samples per second. Of course that kind of speed would need DMA to drive it smoothly.

Assuming you want to generate a sinewave at 40KHz you have to first ask yourself what resolution (time-wise) you want from that sinewave, since that's the frequency that you need to generate samples for. For instance, if you want 100 samples in your sine wave you'd need to generate those at 4Msps, which you can't. At 10 samples per cycle you'd have 400Ksps which is more manageable, but the quality of the sinewave would be pretty poor.

Or, if you don't need a sinewave, if a square wave would do, just use a normal digital IO pin. You can generate MHz then.