CS360 Windows Programming

Midterm Review

Date: 10/11/2005

Topics Covered:

Sample Questions:

  1. How does the Common Language Runtime make programming in C# different from programming in a traditional language like C?
  2. In a C# class, you can write a method that uses the "destructor" syntax. When is such a method called? What is the reason for writing one?
  3. What is the difference between a value type and a reference type in C#?
  4. Given the following program in CIL (Common Intermediate Language), generate a plausible C# program that might have generated this program (this process is called "reverse engineering").

    .method private hidebysig static void   Main() cil managed
    {
     
    .entrypoint
     
    // Code size      28 (0x1c)
     
    .maxstack   4
     
    .locals init (
      [0] int32 x,           
      [1] int32 a,           
      [2] int32 e,           
      [3] int32 b,           
      [4] int32 z,           
      [5] int32 d)  
      IL_0000:   ldc.i4.3  
      IL_0001:   stloc.1  
      IL_0002:   ldc.i4.5  
      IL_0003:   stloc.2  
      IL_0004:   ldc.i4.s    71  
      IL_0006:   stloc.3  
      IL_0007:   ldc.i4.2  
      IL_0008:   stloc.s     z  
      IL_000a:   ldc.i4.6  
      IL_000b:   stloc.s     d  
      IL_000d:   ldloc.1  
      IL_000e:   ldloc.2  
      IL_000f:   mul  
      IL_0010:   ldloc.3  
      IL_0011:   ldloc.s     d  
      IL_0013:   ldloc.s     z  
      IL_0015:   mul  
      IL_0016:   ldc.i4.3  
      IL_0017:   div  
      IL_0018:   add  
      IL_0019:   mul  
      IL_001a:   stloc.0  
      IL_001b:   ret
    } // end of method class1::Main
  5. Consider the following code
    using System;
    struct Point
    {
            
      public int X;
            
      public int Y;         
      public Point (int x, int y)
      {                  
       this.X = x;                  
       this.Y = y;                  
      }
    }
    class Rectangle
    {         
      public Point CornerTopLeft = new Point();        
      public Point CornerBottomRight = new Point();         
      public Rectangle () {}
    }
    class Class1
    {         
      static void Main(string[] args)         
      {                  
       Point p1 = new Point(4,3);                  
       Point p2;                  
       p2 = p1;                  
       p1.X = 56;
       p1.Y = 85;                  
       Rectangle r1 = new Rectangle();                  
      
    Rectangle r2;                  
       r1.CornerTopLeft = p1;                  
       r1.CornerBottomRight = p2;                  
       r2 = r1;                  
       r2.CornerTopLeft = new Point(1,0);                  
       Console.WriteLine("({0},{1})",p2.X,p2.Y);                  
       Console.WriteLine("({0},{1})",p1.X,p1.Y);                  
       Console.WriteLine("({0},{1}) and ({2},{3})",                           
                          r1.CornerTopLeft.X,r1.CornerTopLeft.Y,
                          r1.CornerBottomRight.X,r1.CornerBottomRight.Y);              
       Console.WriteLine("({0},{1}) and ({2},{3})",                           
                          r2.CornerTopLeft.X,r2.CornerTopLeft.Y,
                          r2.CornerBottomRight.X,r2.CornerBottomRight.Y);         
      }
    }
    1. What will this program print?
    2. Explain why the program behaves this way.
  6. Suppose you have a plain text file containing "key=value" pairs one per line (e.g. Telephone=(503)352-2008). Write a program which will extract all the key/value pairs putting them into a Hashtable using a regular expression to extract the information from each line of the text file. Also use a regular expression to verify that the value has the proper format (that is area code in parentheses, a single dash ('-') before the last four digits. there can be any number of spaces after the right parenthesis - for example (503)     352-2008 is valid, (503) 3522008 and (503)flaboot are not valid). If the value does not have the proper format, print the message "does not have the correct format" after the value.