Practice and explore

Test your knowledge and understanding by answering some questions, get some hands-on practice, and explore with deeper research into this chapter's topics.

Exercise 3.1 - test your knowledge

Answer the following questions:

  1. Where would you look for help about a C# keyword?
  2. Where would you look for solutions to common programming problems?
  3. What happens when you divide an int variable by 0?
  4. What happens when you divide a double variable by 0?
  5. What happens when you overflow an int variable, that is, set it to a value beyond its range?
  6. What is the difference between x = y++; and x = ++y;?
  7. What is the difference between break, continue, and return when used inside a loop statement?
  8. What are the three parts of a for statement and which of them are required?
  9. What is the difference between the = and == operators?
  10. Does the following statement compile? for ( ; true; ) ;

Exercise 3.2 - explore loops and overflow

What will happen if this code executes?

    int max = 500; 
    for (byte i = 0; i < max; i++) 
    { 
      WriteLine(i); 
    } 

Add a new console application named Ch03_Exercise02 and enter the preceding code. Run the console application and view the output. What happens?

What code could you add (don't change any of the preceding code) to warn us about the problem?

Exercise 3.3 - practice loops and operators

FizzBuzz is a group word game for children to teach them about division. Players take turns to count incrementally, replacing any number divisible by three with the word "fizz", any number divisible by five with the word "buzz", and any number divisible by both with "fizzbuzz".

Some interviewers give applicants simple FizzBuzz-style problems to solve during interviews. Most good programmers should be able to write out on paper or whiteboard a program to output a simulated FizzBuzz game in under a couple of minutes.

Want to know something worrisome? Many computer science graduates can't. You can even find senior programmers who take more than 10-15 minutes to write a solution.

Reginald Braithwaite

"199 out of 200 applicants for every programming job can't write code at all. I repeat: they can't write any code whatsoever."

This quote is taken from http://blog.codinghorror.com/why-cant-programmers-program/.

Refer to the following link for more information:

http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/

Create a console application named Ch03_Exercise03 that outputs a simulated FizzBuzz game counting up to 100. The output should look something like this:

1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14,  FizzBuzz, 16, 17,  
    Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz,  28, 29, FizzBuzz, 31, 32, 
    Fizz, 34, Buzz, Fizz, 37, 38, Fizz, Buzz,  41, Fizz, 43, 44, FizzBuzz, 46, 47, 
    Fizz, 49, Buzz, Fizz, 52, 53,  Fizz, Buzz, 56, Fizz, 58, 59, FizzBuzz, 61, 62,
    Fizz, 64, Buzz, Fizz,  67, 68, Fizz, Buzz, 71, Fizz, 73, 74, FizzBuzz, 76, 77,
    Fizz, 79,  Buzz, Fizz, 82, 83, Fizz, Buzz, 86, Fizz, 88, 89, FizzBuzz, 91, 92, 
    Fizz, 94, Buzz, Fizz, 97, 98, Fizz, Buzz

Exercise 3.4 - practice exception handling

Create a console application named Ch03_Exercise04 that asks the user for two numbers in the range 0-255 and then divides the first number by the second:

Enter a number between 0 and 255: 100
Enter another number between 0 and 255: 8
100 divided by 8 is 12

Write exception handlers to catch any thrown errors:

Enter a number between 0 and 255: apples
Enter another number between 0 and 255: bananas
FormatException: Input string was not in a correct format.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.8.8