11.6. Exercises

  1. The following code fragment generates a compile error. What's the problem with the code?

    private int TestCode()
    {
        int j;
        int x;
    
        j = 4;
        if (j == 4)
            x = 5;
    
        return x;
    }
  2. The present value of a future asset is determined by the number of years into the future at which the asset will be received and the current market rate of interest. For example, if I promise to pay you $100 a year from now and the current interest rate is 5%, the present value of the asset is $95.23. Stated differently, placing $95.23 in the bank today at 5% interest would yield you $100 a year from now. The equation is:

    where p is the present value, a is the future amount, i is the interest rate, and y is the number of years into the future the asset is deferred. How would you code this formula and why?

  3. C# enables you to use code like the following, in which several variables are defined with a single statement:

    long number, val, len;

    What are the pros and cons of this type of variable definition statement?

  4. You've written a very long and complex program that compiles and executes, but produces an incorrect solution. You've tried to locate the error, but so far without success. What would you do to fix the program bug(s)?

  5. Most of my students are loath to place try-catch blocks in their code on their own. That is, unless I remind them to do so, I rarely see try-catch blocks in their assignments. If you were ...

Get Beginning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.