Wednesday, June 2, 2010

SPF Review 2 with Answers


1    For the Console application below, what is the C# code to output "Hello World!" ?    
    using System;   
    class Program   
    {   
      static void Main(string[] args)   
      {   
        // Code to output "Hello World!"   
  Console.WriteLine("Hello World!");   
      }   
    }   
2    For the Windows Form application below, what is the C# code to output a message box that displays "Hello again!"?    
    using System.Windows.Forms;
:   
    private void button1_Click(object sender, EventArgs e)   
    {   
      // Code to output message   
  MessageBox.Show("Hello again!");   
    }   
    :   
3    (Explicit casting)  Write down the value of z for each case below:   
    int x = 9, y =2;   
a)    double z = x / y;  Value of z is 4.0    
b)    double z = (double) (x / y);  Value of z is 4.0   
c)    double z = (double) x / (double) y;  Value of z is 4.5  
d)    double z = x / (double) y;   Value of z is 4.5  
e)    double z = (double) x / y;  Value of z is 4.5  
4    Write down the correct code to replace the code with errors below:   
    int i = 55;   
    string x;   
    // code with error, to be replaced   
    x = i;   
    // correct code to replace the above code   
x = i.ToString();   
5    Write down the correct code to replace the code with errors below:   
    int i;   
    double k;   
    string x = "52";   
    // 2 lines of code with error, to be replaced   
    i = x;
k = x;   
    // correct code to replace the above code   
i = int.Parse(x);   
k = double.Parse(x);
   
6    Write the conditions in the if statement to correct check the following cases   
    string name = textBox1.Text;   
    string nric = textBox2.Text;   
    if ( /* conditions to be coded  */ )   
    {  // Successful   
      :   
    }   
    else   
    {  // Failure   
      :   
    }   
a)   
Conditions    name: john AND nric: S1111111Z
Codes    if (      (name == "john")   &&   (nric == "S1111111Z")     )
b)   
Conditions    (name: john  AND nric: S1111111Z )   OR
(name: jenny AND nric: S2222222Y ) 
Codes    if (     

( (name == "john")   &&   (nric == "S1111111Z") )  ||                                             ( (name == "jenny")   &&   (nric == "S2222222Y") )                                                  )
7    In the following loop, use the table below to work out the values of n through the loop and derive the final result of n after the loop :   
    int m=0, n = 0, p =3;   
    for (m=0; m < 5; m++)   
    {   
       n += m;
   n += p;   
    }   
    Final value of n is 25  

Working:

image

0 comments:

Post a Comment