Section A.3. Chapter 3: C Language Fundamentals


A.3. Chapter 3: C# Language Fundamentals

A.3.1. Quiz



Solution to Question 31 .

True or False.



Solution to Question 32 .

int is the C# alias for the .NET Int32 type. They can be used interchangeably.



Solution to Question 33 .

All except the last, which requires an explicit cast:

 int newInt = (int) myLong; 



Solution to Question 34 .

A uint is an unsigned int and can hold only positive numbers , but it can hold a positive value twice as big as an int (4 million+ rather than 2 million+).



Solution to Question 35 .

A float takes four bytes and a double takes eight bytes, and thus a double can represent much larger values with greater precision.

Figure A-3. Exercise 2-3


Solution to Question 36 .

In C, if you wish to use a variable of any type (pass it as a parameter to a method), you must assign it a value.



Solution to Question 37 .

You refer to the constant like this:

 Temperatures.LightJacketWeather 

Its value is 33.



Solution to Question 38 .

Any statement that evaluates to a value.

A.3.2. Exercises



Solution to Exercise 3-1 .

Write a short program creating and initializing each of the following types of variables : int , float , double , char , and then outputting the values to the console.

 namespace fundamentals {    class exercise    {       static void Main(  )       {          int myInt = 3;          float myFloat = 4.25f;          double myDouble = 123456789.9867;          char myChar = 'A';          System.Console.WriteLine("myInt: {0}, myFloat: {1}, myDouble: {2},             myChar: {3}.", myInt, myFloat, myDouble, myChar);       }    } } 

The output should look like this:

 myInt: 3, myFloat: 4.25, myDouble: 123456789.9876, myChar: A. 



Solution to Exercise 3-2 .

Modify the program in Exercise 3-1 to change the values of variables and output the values to the console a second time:

 namespace fundamentals {    class exercise    {       static void Main(  )       {          // round one          int myInt = 3;          float myFloat = 4.25f;          double myDouble = 123456789.9867;          char myChar = 'A';          System.Console.WriteLine("Round 1: myInt: {0}, myFloat: {1},             myDouble: {2}, myChar: {3}.", myInt, myFloat, myDouble,             myChar);          // round two          myInt = 5;          float myFloat = 25.267f;          myDouble = 987654321.1234;          myChar = 'Z';          System.Console.WriteLine("Round 2: myInt: {0}, myFloat: {1},             myDouble: {2}, myChar: {3}.", myInt, myFloat, myDouble,             myChar);       }    } } 

The output should look like this:

 Round 1: myInt: 3, myFloat: 4.25, myDouble: 123456789.9876, myChar: A. Round 2: myInt: 5, myFloat: 25.267, myDouble: 987654321.1234, myChar: Z. 



Solution to Exercise 3-3 .

Modify the program in Exercise 3-2 to declare a constant float Pi equal to 3.14159. Then assign a new value to pi (3.1) and output its value with the other variables statement. What happens when you try to compile this program?

 namespace fundamentals {    class exercise    {       static void Main()       {          const float pi = 3.14159f;          int myInt = 3;          float myFloat = 4.25f;          double myDouble = 123456789.9867;          char myChar = 'A';          pi = 3.1f;          System.Console.WriteLine("Round 1: myInt: {0}, myFloat: {1},             myDouble: {2}, myChar: {3}.  Pi: {4}", myInt, myFloat,             myDouble, myChar, pi);       }    } } 

This program won't compile, because you're trying to assign a value to a constant. Instead, you receive a compiler error that reads, "The left-hand side of an assignment must be a variable, property or indexer."



Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net