Tuesday, May 25, 2010

SPF Review 1

1

What is MSIL and what advantage does it bring to programming?

 
 

MSIL is Microsoft Intermediate Language (now it has been renamed as CIL (Common Intermediate Language).

Advantage: allow different .NET languages code to work together

 
     

2

What is the main purpose of the keyword namespace?

 
 

A namespace is a way to group related code/classes together

 
     

3a

Declare an Integer type variable with appropriate name to indicate that it is to be used to store number of student in a class. Initialise it with a value of 27:

 
 

int numberOfStudent = 27;

 
     

3b

Declare an accurate Decimal type variable with appropriate name to indicate that it is to be used to store the size of the spaceship exit height. Initialise it with a value of 150.0:

 
 

double sizeSpaceshipExitHeight = 150.0;

 
     

3c

Declare a true/false type variable with appropriate name to indicate whether it is raining now. Initialise it with a value of true:

 
 

bool raining = true;

 
     

3d

Declare a Character type variable with appropriate name to indicate that it is the NRIC type. Initialise it with a value of T:

 
 

char NricType = 'T';

 
     

3e

Declare a String type variable with appropriate name to indicate that it is the email of the user. Initialise it with a value of noname@example.com:

 
 

string userEmail =  “noname@example.com”;

 
     

4

You may use the Visual Studio to try out the following console program:

 
     
 

In the main method:

int x, y, result;

float floatresult;

x = 7;

y = 5;

result = x+y;

Console.WriteLine("x+y: {0}", result);

result = x-y;

Console.WriteLine("x-y: {0}", result);

result = x*y;

Console.WriteLine("x*y: {0}", result);

result = x/y;

Console.WriteLine("x/y: {0}", result);

floatresult = (float)x/(float)y; // ** Line A **

Console.WriteLine("x/y: {0}", floatresult);

result = x%y;

Console.WriteLine("x%y: {0}", result);

result += x;

Console.WriteLine("result+=x: {0}", result);

 

a)

Write down the program output below :

 
 

x+y: 12

 
 

x-y: 2

 
 

x*y: 35

 
 

x/y: 1

 
 

x/y: 1.4

 
 

x%y: 2

 
 

result+=x: 9

 
     

b)

Explain the “(float)” used in Line A. What happen if it is not used?

 
 

Explicit casting => convert x from integer to float, convert y from integer to float.  If it is not used, then x/y will give the result in integer and the decimal portion will be lost.

 

0 comments:

Post a Comment