Search


Showing posts with label interview questions. Show all posts
Showing posts with label interview questions. Show all posts

Monday, August 9, 2010

is finally block called after return command in try block ?

Technorati Tags: ,
   1:  static void Main(string[] args)
   2:  {
   3:         try
   4:         {
   5:               Console.WriteLine("line1");
   6:                return;
   7:         }
   8:         finally
   9:         {
  10:                Console.WriteLine("line2");
  11:         }
  12:  }

output:

   1:  line1
   2:  line2

Well in fact, believe it or not, the finally block will always occur no matter what happened in the try block or any optional catch block.

reference: http://chaoticjava.com/posts/finally-finally-block/

Monday, January 18, 2010

CountWords C# using Regular Expressions (Regex)

   1: protected int WordsCount()
   2: {
   3:     Regex r = new Regex(@"\b\w+\b");
   4:     return r.Matches(textBox1.Text).Count;
   5: }

don’t forget to include using System.Text.RegularExpressions;

Angle between the hr hand and min hand in the clock

 

C# Function that would tell the angle between the hr hand and min hand in the clock

   1: // assume we always have valid numbers for hour and minute
   2: private double CalculateAngle(int h, int m)
   3: {
   4:    // position for minutes
   5:    double totalHourPerDay = 12;
   6:    double totalMinutesPerHour = 60;
   7:    double totalMinutesPerDay = 360;
   8:    double m_pos = m / totalMinutesPerHour * totalMinutesPerDay;
   9:    double h_pos = (h / totalHourPerDay * totalMinutesPerDay) + (m / totalMinutesPerHour * totalMinutesPerDay / totalHourPerDay);
  10:  
  11:    double gap = Math.Abs(m_pos - h_pos);
  12:    
  13:  
  14:    return gap;
  15: }

Sunday, January 17, 2010

Calculate Age code in C#

   1: public static int CalculateAge(DateTime BirthDate)
   2: {
   3:     int YearsPassed = DateTime.Now.Year - BirthDate.Year;
   4:     // Are we before the birth date this year? If so subtract one year from the mix
   5:     if (DateTime.Now.Month < BirthDate.Month || (DateTime.Now.Month == BirthDate.Month && DateTime.Now.Day < BirthDate.Day))
   6:     {
   7:         YearsPassed--;
   8:     }
   9:     return YearsPassed;
  10: }